#!/bin/sh
# Version: 0.92   2004-11-1

## wwwofflebook_ui [--interactive] [--silent] [--config configuration-file]
## 
## A textbased userinterface for the tool 'wwwofflebook', with few options.
##
## This script calls wwwofflebook and afterwards 'wwwoffle-purge' (by default).
## wwwofflebook extracts http domains from a browser bookmark file and inserts
## them into a file called purge.conf, which is the outsourced 'purge' section
## of wwwoflle.conf.
##
## Options:
## The path to the configuration file can be specified with the option -c <path>.
## If not specified, it will be assumed to be ~/.wwwoffle/wwwofflebook.config.
## 
## Commandline options are order-insensitive and can be shortened 
## like -i, -s, -c, but they always need to be seperated.
## You can choose between three levels of 'noise': 
## (1) The default output is a diff between purge.conf old/new, and also the 
## wwwoffle-purge logfile content, but no prompts. It just runs through.
## Only the purge output, no diff, is logged to the logfile.
## (2) --interactive: Remind the user about things and prompt if really do 
## purge now, to give her the chance to make up for missing bookmarks. 
## This option is the only commandline switch to disable purging.
## In this mode, the diff and the purge log will be shown via pager (less). 
## (3) --silent: No diff, no terminal output, no questions. 
## In this mode, the purge output will be written to $log (as defined in the 
## config file) only, but error messages still occur on standard out.
## 
## Installation:
## - purge.conf and the backup *-old must be writable for the user whose browser 
##   will be considered. This user in effect will be the maintainer of the systemwide 
##   purge.conf. To perform the wwwoffle -purge, she needs to join the wwwoffle
##   daemon group ('proxy' on Debian). To avoid permitting this user general write
##   access to /etc/wwwoflle, there's this workaround: The ~/.wwwoffle/purge.conf
##   of the user is a symlink pointing to /etc/wwwoffle/purge.conf. 
## - The 'wwwofflebook' scripts must be installed in the user's path 
##   (for example, /usr/local/bin).
## 
## Invocation examples:
##  xterm -e "wwwofflebook_ui --interactive"
##  0 23  * * 7 root wwwofflebook_ui --silent  # cron.d entry for Sunday 23.00
##
## Licence: Released under GNU GPL version 2 or higher. 
## Report bugs to: michelle vernon <wwm@gmx.ch>.
##
## Bugs:
## (1) (non-critical) Dynamic entries moved to the static section (instead of copied) 
## will be shown as 'new' in next diff since diff compares the browser bookmarks 
## to the dynamic section only. Workaround: Copy instead of move entries.
##
## For further information see the README__wwwofflebook.

## ============== CONFIG
# Default config (overriden by commandline):
config=~/.wwwoffle/wwwofflebook.conf
#
alias show=cat		# Default output mode. With '-i' replaced by pager
unset silent interactive
# more environment settings in wwwofflebook.conf ('general' section)

# get commandline
n=0 # disable positional parameter shifting for first while loop 
while shift $n && [ $1 ]; do

n=1	

if [ `echo $1 | grep '^-'` ]; 
    then  # set option flags:
	    case $1 in
		   -h | --help) less $0; exit 0;;
        	 -c | --config) shift; config=$1;;
    	    -i | --interactive) interactive=true; alias show=pager;;
	         -s | --silent) silent=true;;
			     *) echo "$0: Unknown option $1"; exit 0;;
	    esac
    else  echo "Unknown string parameter: $1"
fi

done

# XXX DEBUG:
#clear; echo "
#i: $interactive
#s: $silent
#c: $config
#u: $user
#"; read a
#exit 0
# :XXX DEBUG

# Include configuration file
source $config

## ============== MAIN

clear

if [ ! $silent ]; then 
    echo -e "$user_info" | show
    if [ $interactive ]; then
    clear; 
    prompt  "\nYou will be asked some questions you can answer with 'y' (yes) or 'n' (no). 
 Just press <enter> to accept the proposed default.\n
 Do you want to refresh the purge.conf file now ?" yes || exit 0
    fi    
fi

# do the bookmark thing. wwwofflebook needs to be in user's path.
wwwofflebook -c $config || exit 6

# option silent || show diff
if [ ! $silent ]; then
    echo "$diff_header"> $temp
    diff -bBiw --expand-tabs -y --width=79 --suppress-common-lines $PURGE_CONF $PURGE_OLD >> $temp
    show $temp
fi

#  option interactive && ask purge
if [ $interactive ]; then
clear; prompt  "\nShall the cache be cleaned up now ?" yes || do_purge=no    
fi

#  option purge
if [ "$do_purge" = "yes" ]; then

    # backup logfile and create a new one with a little header
    [ -f $log ] && mv -f $log $log_old
    echo "
    Date: `date`
    Cache maintainer: $user
    Configuration file: $config
    Script location: $0
    Proceeding: '$purge_command'
    Default expiring age: $default_age days.
    " > $log

    [ ! $silent ] &&  echo "wwwoffle is cleaning up the cache now - please wait ...."

    # brush up purge output
    $purge_command | sed 's/;//g' \
| sed 's/Not Purged/Hold\t  /g' \
| sed "s/Purged (`echo $default_age` days)/DEFAULT \t/g" \
| sed 's/Purged (/Expire:\t /g' \
| sed 's/ days)/\t/g'  1>> $log
    # If you don't like it (or it doesn't work) just make it:
    # $purge_command 1>> $log
    # Note that sorting would need to seperate the URL part from header and footer.
    # function 'extract' is prepared in wwwoffle.conf, but not yet implemented.
fi

##  ============== FINISH

# cleanup
rm -f $temp

# Show purge log and quit properly. 
# The point here is to prevent xterm windows from beeing closed too early

[ $silent ] && exit 0

if [  $do_purge = "yes" ]; then 
    echo -e "$quit_mesg" >> $log
    show $log
    [ ! $interactive ] && read -n1 a;
    
else read -p "$quit_mesg" -n1 a

fi

exit 0
