#!/bin/sh	# (only for syntax highlighting)

# version: 0.93		2005-02-15
#
## Configuration file for the wwwofflebook scripts
## Sections:
## (0) Common settings
## (1) wwwofflebook
## (2) wwwofflebook_ui
## (3) wwwofflebook_check

## ============= COMMON SECTION
# This section is needed for several wwwofflebook scripts
#
# wwwoffle daemon configuration-file directory
WWWOFFLE_DIR=/etc/wwwoffle 
# Path to the wwwoffle daemon config file
WWWOFFLE_CONF=$WWWOFFLE_DIR/wwwoffle.conf
# Path to the 'real' purge.conf file
WWWOFFLE_PURGE_CONF=$WWWOFFLE_DIR/purge.conf
# cache maintainer
user=michl
# Home directory where the  user/maintainer's .wwwoffle directory with 
# the outsourced purge.conf (and other wwwofflebook files) is located. 
# Usually  /home/<username>.
userhome=/home/$user
# Directory containing the purge.conf file:
userwwwoffle=$userhome/.wwwoffle
# Symbolic link pointing to outsourced $WWWOFFLE_PURGE_CONF: 
PURGE_CONF=$userwwwoffle/purge.conf
purge_conf=`basename $PURGE_CONF`	# derived
# Backup (no link)
PURGE_OLD=$PURGE_CONF-old
# temporary buffer files:
temp=/tmp/wwwofflebook.temp
list=/tmp/wwwofflebook_extracted_list
# Standard message to keep xterm window open 
quit_mesg="

________________ OK ! You can close this window now.  ________________

"
## ============= WWWOFFLEBOOK SECTION
# This section is needed for wwwofflebook only
#
# The browser type you use for bookmarking (i.e. the bookmark file type.)
# By now 3 experimantal handlers implemented: galeon, opera and mozilla.
# You also can try 'any' if you're using a different browser.
# You are welcome to add a proper function for your browser type below.
BROWSER=any
# standard name of the related bookmarks-extracting function below 
bookmark_extract=$BROWSER"_book"
# path to the main (last modified) bookmark file:
BOOKMARK_FILE=$userhome/.galeon/bookmarks.xbel
# Unique strings in purge.conf marking the dynamic list:
START="DYNAMIC BOOKMARK LIST BEGIN"
END="DYNAMIC BOOKMARK LIST END"

# BROWSER TYPE BOOKMARK FUNCTIONS for extracting domains of a bookmark file.
# You need not to edit these, normally. Just set BROWSER to one of the 
# available functions. If your browser isn't listed here, try to set 'any' 
# as browser type.  
#
# Actually most functions equal 'any' by now. It may be necessary to fine-tune
# something, however. And please extend this list by new browser types !
# The function name has to (literally) equal the BROWSER value specified above,
# plus an appendet '_book'. 
# All functions will be invoked like '$bookmark_extract $BOOKMARK_FILE',
# where $bookmark_extract is the function name, and $BOOKMARK_FILE is defined 
# on top of this section here.
# A browser function is expected to return a domain-list in purge.conf format 
# on standard out, which can be inserted into the existing purge file as dynamic
# section without further modifications.


function any_book ()		# generic, works for many vrowsers
{
cat $1 | tr -s "\"<>" "\n" | tr -d " " | grep "http://" | cut -d"/" -f3 | sort -u > $temp
echo -n ""
# Convert 'http://domain.name' -> '<http://domain.name/*> age = -1'
for i in `cat $temp`; do echo "<"http://$i"/*> age = -1"; done
rm -f $temp
}


function opera_book ()	# opera (.adr and .html)
{
cat $1 | tr -s "\"<>" "\n" | tr -d " " | grep "http://" | cut -d"/" -f3 | sort -u > $temp
echo -n ""
# Convert 'http://domain.name' -> '<http://domain.name/*> age = -1'
for i in `cat $temp`; do echo "<"http://$i"/*> age = -1"; done
rm -f $temp
}


function galeon_book ()	# galeon 1.3.15 (.xbel format version 2) 
# Detect xbel version (experimental):
# version_number=`grep "<galeon_xbel_version>" $BOOKMARK_FILE | tr "<>" "\n" | grep "[0-9]"`
{
cat $1 | tr -s "\"<>" "\n" | tr -d " " | grep "http://" | cut -d"/" -f3 | sort -u > $temp
echo -n ""
# Convert 'http://domain.name' -> '<http://domain.name/*> age = -1'
for i in `cat $temp`; do echo "<"http://$i"/*> age = -1"; done
rm -f $temp
}


function mozilla_book () # mozilla 5.0 (debian 1.6.5) html bookmark file
{
cat $1 | tr -s "\"<>" "\n" | tr -d " " | grep "http://" | cut -d"/" -f3 | sort -u > $temp
echo -n ""
# Convert 'http://domain.name' -> '<http://domain.name/*> age = -1'
for i in `cat $temp`; do echo "<"http://$i"/*> age = -1"; done
rm -f $temp
}


function elinks_book ()	# elinks 0.93 (for original 'links' try Browser 'any')
{
cat $1 | tr -s "\t " "\n" | tr -d " " | grep "http://" | cut -d"/" -f3 | sort -u > $temp
echo -n ""
# Convert 'http://domain.name' -> '<http://domain.name/*> age = -1'
for i in `cat $temp`; do echo "<"http://$i"/*> age = -1"; done
rm -f $temp
}


## ============= WWWOFFLEBOOK_CHECK SECTION
# This section is needed for wwwofflebook_check only.
user_dir_perm=770	# for ~/.wwwoffle: rwxrwx---  (user.proxy)
user_files_perm=644	# for all files in ~/.wwwoffle: rw-r--r-- (user.user)
wwwoffle_purge_conf_perm=664 # for /etc/wwwoffle/purge.conf: rw-rw-r--  (proxy.proxy)

## ============= WWWOFFLEBOOK_UI SECTION
# This section is needed for wwwofflebook_ui only.
#
# invoke wwwoffle-purge by default ?
do_purge=yes
purge_command="wwwoffle -purge -c /etc/wwwoffle/wwwoffle.conf"

# logfile + backup for output of wwwoffle-purge
log=$userwwwoffle/wwwofflebook.log
log_old=$log-old

# The 'wwwoffle.conf' default age, used for user_info and brush-up of
# purge output. If there's a problem, you can set it also directly like: 
# default_age=10
if [ ! -r $WWWOFFLE_PURGE_CONF ]; then echo "wwwofflebook.conf: Can't read $WWWOFFLE_PURGE_CONF !
If it's missing, please create one. If it exists, make sure it's readable for 
the superuser 'root' and also for the group 'proxy'. 
If you think it's fixed, run (as root) the script 'wwwofflebook_check'.
If it still doesn't work, check the line starting with '\$default_age=' 
in the wwwofflebook.conf, and report it as bug.";  exit 6
fi
default_age=`grep '^age' $WWWOFFLE_PURGE_CONF | cut -d" " -f3`

# headline of bookmark diff between new and old purge.conf
diff_header="New entries:               NEW PURGE.CONF CREATED               Removed entries:
------------                                                    ----------------"

# message for interactive mode
# (a german version can be found in the wwwofflebook library)
user_info="_______________________ wwwofflebook ______________________________

[  What you see now is a text shown by a pager program. 
You can navigate with the up/down arrow keys and the page up/down keys, 
search with a '/' followed by the serach term, and quit with 'q'. ]

The wwwofflebook script extracts http domains from your browser bookmark file.
The http domain of a bookmark is the first part of the address, beginning 
after the protocol prefix 'http://'. wwwofflebook adds these domains to the 
wwwoffle daemons configuration with the flag to never purge them at all. 
The pages of these sites will always be offline available from the cache. 
A site will be retained this way until you remove the sites' browser bookmarks.

Example:
  
  All pages that start with 'www.xml.org' are retained provided there is
  any bookmark entry of the domain 'www.xml.org'.
  
  However, pages downloadled from 'www.xml.com' will be deleted at their 
  default expiring age, if there is no 'www.xml.com' bookmark entry.
  
  To preserve pages from the latter site also, you need to bookmark at least
  one page of this site.

  If you remove all 'www.xml.org' bookmarks and run wwwofflebook afterwards,
  this site will be enabled for purging at the default age again. 

Because you invoked wwwofflebook in interactive mode, you will be prompted for 
every step now. First, the bookmark domains will be extracted and inserted into
the wwwoffle daemons 'purge.conf' configuration file. A backup of the previous 
version of this file named 'purge.conf-old' will be created automatically. 
You will be shown the differences between these two files, and you may decide 
to quit wwwofflebook at this point e.g. to edit the browser bookmarks, and then 
run this script again. 
However, if you answer 'yes' to the prompt, the next step will be to ask the 
wwwoffle daemon to clean the cache according to the new purge.conf file. 
The domains cached by wwwoffle that couldn't be found in any of your browser 
bookmarks still will only be deleted, however, if they outdated the default 
purge age. This age is specified at the end of the purge.conf file ('age = xy').
Your purge file is located at: $PURGE_CONF. 
You can define your own retention times for sites that should be handled 
differently by editing the personal section of the 'purge.conf' file with a 
standard text editor. The entries on top, in the 'personal section', will first
match when wwwoffle compares the address domains, and thus will be preferred 
before equal entries in the generated 'dynamic section' below. 
For this reason, doubles below in the dynamic section doesn't matter.
The provided original example file illustrates the available options. 

The last step is the wwwoffle daemons purge log will be shown to you through 
the pager. The first column describes the expiring limit, using these keywords:

-------------------------------------------------------------------------------
'DEFAULT' means the site will be purged according to the default age (see below).
This ususally means there was no browser-bookmark entry for that domain.
You should pay special attention to those sites.

'Expire' means a domain will be purged after the time given by the next column.
Such domains are those of the 'personal section' (see below).
The measurement of time depends on your wwwoffle configuration.
Usually it refers to the creation (or modification) time of the pages.

'Hold' means the site will never be purged. 
Usually those are your browser bookmark domains.

The last two columns tell the size the domain is using up on the harddisk,
and, in case it was purged, the deleted amount (both in kilobyte).
------------------------------------------------------------------------------

wwwofflebook writes the complete purge log to the logfile: 
$log
so that you can examine it at any time later.

For further information, please read the README__wwwofflebook.
"

## ============= GENERAL FUNCTIONS AND ALIASES
shopt -s expand_aliases # Enable aliases
alias pager='less -Cq -m -Pm"                                press (q) to exit pager              Line %lb of %L "'

function prompt () # prompt <question> <default=yes/no>
		   # Ask (y/n). Returns true if readline = default or <enter>.
		   # Handle any other input as default, too ->  anything else
		   # than then the alternative of proposed default returns true.
{
    default=`echo $2 | tr -d e,s,o` 		# reduce to y/n
    opponent=`echo "yn" | tr -d $default`	# this is what we need
    echo -en " $1 ($default) "; read a;		# better than read -p
    [ "$a" != "$opponent" ] && return 0 	# True  
    return 1 					# False
}


function getval ()	# getval <config-file> <value titel>
		# Read the requested value from a comfiguration file.
		# option-value pairs must be of the format title = value. 
		# Blanks and any control-chars like tabs are squeezed.
{
grep ".*$2.*" $1 | grep -v "^#" | tr -d " [:cntrl:] " | cut -d "=" -f2
}


function extract () # extract <sourcefile> <startstring> <endstring> [--imply-marker]
		    # This is a future extension, not used by now.
		    # Extract a section of sourcefile between the 2 marker strings.
		    # Output on standard out.
		    # START and END must not be equal, or occur more than one time.
		    # It is possible to include regexpr in the marker strings.
		    # like '^' matching the beginning of the line.
		    # Options: --imply-marker ... Include markers in the output.
{
    source=$1	# e.g. wwwoffle.conf
    start=$2	# e.g. "LIST BEGIN"
    end=$3	# e.g. "LIST END"
    option=$4

    # number of lines matching <end> counted from the top
    head=`cat $source | grep -n "$end" | cut -f1 -d":" `
    [ "$option" = "--imply-marker" ] || head=$(( $head - 1 ))

    # from this piece, count the number of lines 
    # matching <start> counted from the bottom
    tail=`head -n$head $source | tac | grep -n "$start" | cut -f1 -d":"`
    [ "$option" = "--imply-marker" ] || tail=$(( $tail - 1 ))

    # put together
    head $source -n$head | tail -n$tail | less

}
