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

## wwwofflebook [-c <path-to-config>]
## 
## This script extracts http Domains from a Browsers' bookmark file, and inserts
## them into purge.conf, which is the outsourced purge section of wwwoflle.conf.
## Backup of the previous version to purge.conf-old.
## 
## Configuration is done through an external file (wwwofflebook.conf).
## The pathname of this file can be specified as commandline option -c <path>.
## If not specified, ~/.wwwoffle/wwwofflebook.conf will be the default.
##
## purge.conf and purge.conf-old must be writable for the specified user.
## I preferred not to grant the user write access to /etc/wwwoflle completely.
## My workaround:
##  /etc/wwwoffle/purge.conf is symlinked to this one user's (newly to create )
##  ~/.wwwoffle directory. This user is the maintainer of the (i.e. systemwide) 
## purge.conf, and needs to belong to the wwwoffle-daemon group ('proxy' on Debian)
## to be able to write to the purge.conf and proceed the wwwoffle -purge command.
## Also, you need to set the permissions of /etc/wwwoffle/purge.conf to 
## rw-rw-r (octal 664) for proxy:proxy. 
##
## Writes temporary files /tmp (with cleaning up). 
##
## Invocation examples:
## xterm -e "wwwofflebook -h"
## wwwofflebook -c ~/.wwwoffle/wwwofflebook.conf
## 0 23  * * 7 root wwwofflebook # cron.d entry for Sunday at 23.00
##
## ToDo:
## Remove doubles that already exist in 'static section' from the 
## 'dynamic' section in purge.conf (not critical).
##
## Licence: Released under GNU GPL version 2 or higher. Report bugs to: michelle vernon <wwm@gmx.ch>.

config=~/.wwwoffle/wwwofflebook.conf

## commandline overwrites
 
case $1 in 	# Regarding the first option only.
      -h|--help) less $0; exit 0;;
    -c|--config) config=$2;;
	     "") break;;	# use defaults
	      *) echo "$0: Unknwon option \"$1\". Try -h."; exit 1;;
esac

## configuration
source $config

## general functions
exchange ()    	# exchange <targetfile> <exchangefile> <start> <end>

# Exchange a section of targetfile between the marker strings $START and $END 
# with exchangefile's content, plus a frame of two "#".
# $START and $END must NOT be equal, nor occuring more than one time.
# Output on standard out.

{

    target=$1
    exchange=$2
    start=$3
    end=$4

# Lines matching <start> counted from the top
# to know the lines that can be cutted off from the head
    head=`cat $target| grep -n "$start" | cut -f1 -d":"`

# Lines matching <end> counted from the end
# to know the lines that can be cutted off from the end
    tail=`tac $target | grep -n "$end" | cut -f1 -d":"`

# Put stuff together
    head $target -n$head
    echo "#"
    cat $exchange
    echo "#"
    tail $target -n$tail
}


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

# check files

if [ ! -L $PURGE_CONF ]; then 
  echo -en "`basename $0` error: Symbolic link $PURGE_CONF not found !"; exit 6
fi

for file in $PURGE_CONF $PURGE_OLD $list $temp; do

  [ ! -e $file ] && touch $file 2> /dev/null 
  if [ ! -w $file ]; then
  echo "`basename $0` error: $file is not writable. 
Maybe it exists as a relict from a different user ?"; exit 6
  fi

done

if ! test -r $BOOKMARK_FILE; then echo "$BOOKMARK_FILE not found or not readable !";  exit 6; fi

# Get all http domains from Bookmark file by calling a browser-type specific 
# function, depending on $BROWSER. 
# $BROWSER and available functions are configurated in the config file.
$BROWSER $BOOKMARK_FILE > $list

## XXX DEBUG:
#clear; echo -e "EXTRACTED LIST:\n `cat $list`" > $temp
#echo -e "\nCommandline:\n exchange $PURGE_CONF $list \"$START\" \"$END\"\n" >> $temp
#echo -e  "\nUPDATED PURGE CONF:\n" >> $temp
#exchange $PURGE_CONF $list "$START" "$END" >> $temp
#cat  $PURGE_CONF >> $temp
#less $temp; exit 0
## :XXX DEBUG

# Backup purge.conf
cp -f $PURGE_CONF $PURGE_OLD	# create a local file, do not copy the symlink

# Update purge.conf (can't pipe back directly)
exchange $PURGE_CONF $list "$START" "$END" > $temp
cat $temp > $PURGE_CONF	# can't simply move for the link thing.

# Cleanup (disable for debugging)
rm -f $list $temp
