#!/bin/bash

# Version: 0.93    2005-02-15

## wwwofflebook_setup -c <path-to-wwwofflebook.conf>
##
## The script must be able to read wwwofflebook.conf to properly configure.
## Should be invoked with sufficient permissions (usually: root) to fix things in /etc/wwwoffle.
##
## Licence: Released under GNU GPL version 2 or higher. Report bugs to: michelle vernon <wwm@gmx.ch>.
##
## TODO:
## Sources wwwofflebook.conf which reads ~/.wwwoffle/purge.conf to get the actual default purge age.
## This is a bug, because wwwofflebook_check should be able to fix the symlink even if it doesn't 
## exist. Possible solutions:
## - Query wwwoffled about default age
## - disable the wwwofflebook.conf query when invoked by wwwofflebook_check
## - ?


clear

# ========== CONFIG

# I want it totally clear that you have to configure the config file manually, 
# so there's no default, but i'll suck you with this:
if [ ! -f "$2" ]; then 
    echo "Please specify the wwwofflebook configuration file like this:
    wwwofflebook_check  -c /home/michelle/.wwwoffle/wwwofflebook.conf"; exit 6
fi
config=$2; source $2 	# need the var 'config' later

function check_perm ()	# check_perm <target> <owner> <group> <perm>
# Check file or directory <target> for the provided features. 
# perm must be octal, owner and group plain names (not ID). 

{
target=$1
owner=$2
group=$3
perm=$4

find $target -prune -printf "%m %u.%g $target"; echo

if [ -z "`find $target -user $owner -group $group `" ] || [ -z "`find $target -perm $perm`" ];

    then  
	echo "--> Not ok ! Expected would be:"
	printf "$perm $owner:$group $target"

	if prompt "\nFix now ? " yes; then 
	    chown $owner:$group $target;
	    chmod $perm $target;
	    echo "--> OK, fixed.";
	else echo "--> OK, leaving it as is.";
	fi

#    else echo "--> OK.";
fi

}

# ========== MAIN
prompt "\nThis is a little wwwofflebook configuration test.
It checks some featrues like permissions and ownership of files,
and offers some limnited fixing capabilities. However, it does NOT setup a
valid wwwofflebook configuration ! You will have to follow the instructions
given in the README to make wwwofflebook work.

You will be asked some questions now.
You can answer with 'y' (yes) or 'n' (no). 
You can also just press <enter> to accept the proposed default.
 
Continue now ?" yes || exit 0; 

# ---------- Check #1: user and group
clear
daemon_group=`getval $WWWOFFLE_CONF "run-gid"`
daemon_id=`cat /etc/group | grep ^$daemon_group | cut -d":" -f3`
user_id=`cat /etc/group | grep ^$user | cut -d":" -f3`

# XXX DEBUG:
#echo "
#conf: $2
#user: $user  ID: $user_id 
#daemon_group:  $daemon_group   ID: $daemon_id
#"
# :XXX DEBUG


echo -en "Here we go...\n
#1 ========== User and group\n
The configured maintainer is: $user (user ID $user_id.)
The wwwoffle daemon group is '$daemon_group' (group ID $daemon_id.)
Is $user member of the configured wwwoffle daemon group '$daemon_group' ? "

if groups $user | grep -q $daemon_group; 
    then echo Yes.
    else echo "No.";
	if prompt "\nAdd $user to '$daemon_group' ?" yes;
	    then adduser $user $daemon_group;
	    echo "A currently logged in user needs to login again to let this work."
	fi
fi
prompt "\nContinue ?" yes || exit 0; 

# ---------- Check #2: /home/user/.wwwoffle directory

echo -e "\n#2 ========== Checking permissions\n
Format: octal permissions  owner.group  filename  
-------------------------------------------------\n"

# I'd rather make it echo | ( while read ) than a function,
# but then read -p in prompt () won't work. 
check_perm $WWWOFFLE_PURGE_CONF $daemon_group $daemon_group $wwwoffle_purge_conf_perm
check_perm $userwwwoffle $user $daemon_group $user_dir_perm

echo -e "\nChecking files in $userwwwoffle:"

for file in `find $userwwwoffle -type f`; 
    do check_perm $file $user $user $user_files_perm
done

prompt "\nOK, done. Continue ?" yes || exit 0; 

# ---------- Check #3: link purge.conf -->  /etc/wwwoffle directory
clear
echo -e "\n#3 ========== Checking symbolic link to $purge_conf\n
Full link name: $PURGE_CONF\nPointing to:    $WWWOFFLE_PURGE_CONF"

if [ -z "`find $userwwwoffle -name $purge_conf -lname $WWWOFFLE_PURGE_CONF`" ]; then
  echo -en "Link not found or link does not point to correct target !";
  prompt "Fix  ?" yes && ln -s -vif $WWWOFFLE_PURGE_CONF $PURGE_CONF
fi

echo -e "\nok."

# ---------- Check #4: Test the browser function
cat << EOF

#3 ========== Testing bookmark extraction

According to $config, you configured:
browser type = $BROWSER
bookmark file = $BOOKMARK_FILE

You can choose to feed the content of the bookmark file to the extracting 
function '$bookmark_extract' now. You can check the output with a pager then.
Note that this test will leave your purge.conf untouched.
EOF

if prompt "\nTest now ?" yes; then 
    $bookmark_extract $BOOKMARK_FILE | pager
    read -n1 -p "
If this didn't work correctly for your browser type, please send a report 
and attach your wwwofflebook.conf and a sample bookmark file :)"
fi

# ========== FINISH
clear; 
cat << EOF 

#5 ========== What to do next

To enable the wwwofflebook mechanism, please edit the file 
$WWWOFFLE_CONF, and replace the 'purge' section ... (quote:)

purge
{
(URL's)
}

... completely by this entry:

purge
[
purge.conf
]

If you have already some URLs configured, you can copy them 
into the new purge.conf 'PERSONAL' section.

For more information please read the wwwofflebook README.

$quit_mesg
EOF

read -n1 a

