#!/bin/sh
#
# Install cron job
# Copyright 2005-2006 Florian Daeumling <jam@qv90.de>, Jaballa Software
# Licensed under the GNU GPL. For full terms see the file COPYING.
#

# terminate script in case of error
set -e

#
# get testing status
#
function get_test() {
 echo ""
 echo "# Enable test mode?"
 echo "#  - don't actually delete any file"
 echo "#  - show messages in syslog AND on stderr"
 read -p "# (y/N)> " mode
 if [ "$mode" == "" -o "$mode" == "n" -o "$mode" == "N" ]; then
  testing=0
 else
  testing=1
 fi 
}

#
# get verbose level
#
function get_logging() {
 until [ "$logging" != "" ]; do
  echo ""
  echo "# Select logging level"
  echo "#  0-Quite mode"
  echo "#  1-Start of user processing (default)"
  echo "#  2-and details of user mail directory processing"
  read -p "# (0-2)> " logging
  case "$logging" in
  0|1|2)
   ;;
   
  *)
   logging="1"
   ;;
  esac
 done
}

#
# get data directory
#
function get_data_dir() {
 # get parameter from user
 tmp="/var/www/sm-1.4.6"
 echo ""
 echo "# Enter full path to SquirrelMail base directory"
 read -p "# (default: $tmp)> " sm_path
 if [ "$sm_path" == "" ]; then
  sm_path=$tmp
 fi
 sm_conf=`echo $sm_path | sed -e "s/\/*$//g"`"/config/config.php"
 if [ ! -e $sm_conf ]; then
  echo "+++ Can't find '$sm_conf'"
  exit 1
 fi
 data_dir=`grep data_dir $sm_conf | cut -d\' -f2`"expire/"
 if [ "$data_dir" == "" ]; then
  echo "+++ Can't extract variable 'data_dir' from $sm_conf"
  exit 1
 fi
}

#
# get cron directory
#
function get_cron_path () {
 tmp="/etc/cron.daily"
 echo ""
 echo "# Enter full path to daily cronjob directory"
 echo "# (for more information - 'man 5 contab')"
 read -p "# (default: $tmp)> " cron_path
 if [ "$cron_path" == "" ]; then
  cron_path=$tmp
 fi
 cron_path=`echo $cron_path | sed -e "s/\/*$//g"`"/"
 if [ ! -d $cron_path ]; then
  echo "+++ Daily cron directory $cron_path not found"
  exit 1
 fi
 cron_path=$cron_path"mail-expire"
}

#
# get locate mode
#
function get_mode() {
 until [ "$locate" != "" ]; do
  echo ""
  echo "# Do you want support for virtual user?"
  echo "# - for more information about this topic read INSTALL step 4)"
  read -p "# (y/N)> " mode
  if [ "$mode" == "" -o "$mode" == "n" -o "$mode" == "N" ]; then
   locate="0"
   tmp="/home/<SM_USER>/Maildir/"
   echo ""
   echo "# Enter users default mail directory name"
   echo "# <SM_USER> will be replaced at run time"
   read -p "# (default: $tmp)> " mail_dir
   if [ "$mail_dir" == "" ]; then
    mail_dir=$tmp
   fi
   mail_dir=`echo $mail_dir | sed -e "s/\/*$//g"`
  else
   echo ""
   echo "# Select command to find <<SM_USER>>.expire"
   echo "# (for more information about this topic read INSTALL)"
   echo "#  1- use 'locate' command"
   echo "#  2- use 'find' command"
   read -p "# (1/2)> " locate
   case "$locate" in
   1|2)
    ;;
  
   *)
    locate=""
    ;;
   esac
  fi
 done
 if [ "$locate" == "1" -o "$locate" == "2" ]; then
  tmp="/home/"
  echo ""
  echo "# Enter user base directory (where to start search)"
  read -p "# (default: $tmp)> " mail_dir
  if [ "$mail_dir" == "" ]; then
   mail_dir=$tmp
  fi
  mail_dir=`echo $mail_dir | sed -e "s/\/*$//g"`"/"
 fi
}

#
# get host
#
function get_host() {
 echo ""
 echo "# Enter host name (where mail directories are located)"
 tmp="localhost"
 read -p "# (default: $tmp)> " host
 if [ "$host" == "" ]; then
  host=$tmp
 fi
}

# get parameter
if [ "$1" != "" ]; then
 # parameter provided
 testing=$1
 logging=$2
 data_dir=$3
 locate=$4
 mail_dir=$5
 host=$6
 cron_path=$7
else
 get_test
 get_logging
 get_data_dir
 get_cron_path
 get_mode
 get_host
fi

sed -e 's<<TESTING>>'"$testing"'g'     	\
    -e 's<<LOGGING>>'"$logging"'g'	  	\
    -e 's<<DATA_DIR>>'"$data_dir"'g'   	\
    -e 's<<LOCATE>>'"$locate"'g' 	  	\
    -e 's<<MAIL_DIR>>'"$mail_dir"'g'   	\
    -e 's<<HOSTNAME>>'"$host"'g'   		\./mail-expire > $cron_path
chown root $cron_path
chmod u+x $cron_path

echo "Daily cron job mail-expire installed to $cron_path."


