#!/bin/bash
#
# $Id:$
# Copyright (c) 2004 SUSE LINUX AG. All rights reserved.
# Authors: Jochen Rder <jro@suse.de>
# Desc:    setup and sync a you server by wget 
#

#
# directory for parameter file. each file must contain all needed values
# for the sync
#
SYNC_PARAM_DIR="/var/lib/YaST2/you/sync"

#
# directory for logfiles
#
SYNC_LOG="/var/lib/YaST2/you/synclog"

#
# directory for files containing the last status
#
SYNC_LASTRUN="/var/lib/YaST2/you/lastrun"

#
# binary to use for wget
#
SYNC_CMD="wget"

#
# default options for wget, allways added
# 
#SYNC_OPTS="-r --tries=3 --mirror --no-host-directories"
SYNC_OPTS="-r -N -c -l inf -t 3 --no-host-directories --reject=*.gif,*.jpg,*.png,*.html*"

#
# proxy options
#
# PROXY_OPTS="--proxy=on --proxy-user=user --proxy-passwd=password"

#
# destination path for the sync, this is also the install path for the
# client you. so you have to configure exactly this path as a directory
# entry in you httpd.conf
#
DEST_DIR="/var/lib/YaST2/you/mnt"

#
# simple config check
# 
[ ! -d $SYNC_PARAM_DIR ] && echo "not found: $SYNC_PARAM_DIR" && exit 1;
[ ! -d $SYNC_LOG ]       && mkdir -p $SYNC_LOG
[ ! -d $SYNC_LASTRUN ]   && mkdir -p $SYNC_LASTRUN

#
# reading sync configuration
#
for file in $SYNC_PARAM_DIR/*
do
   # break if there is no file at all
   test -f $file || break;

   CONFIGNAME=`basename $file`
   echo "processing config $CONFIGNAME" | logger -t youmirror -s 2>&1

   #
   # unset old values
   #
   unset PATH_DOWNLOAD
   unset PATH_EXCLUDE
   unset AUTH_USER
   unset AUTH_PASS
   unset WGET_OPTIONS
   unset CMD_PRE
   unset CMD_POST
   unset CUT_DIRS
   unset DOMAIL
   unset MAILTO
   
   # 
   # read values
   #
   . $file
   
   #
   # check if this configuration is active
   #
   test $DOSYNC != 1 && logger -t youmirror -s "skip config $CONFIGNAME" 2>&1 && continue 
   
   # 
   # check values
   #
   AUTH_OPTS=""
   test -z $PATH_DOWNLOAD && exit 2
   test -d $DEST_DIR ||(logger -t youmirror -s "not found: $DEST_DIR" 2>&1; exit 3)
   PATH_EXCLUDE=${PATH_EXCLUDE:+--exclude-directories=$PATH_EXCLUDE}
   AUTH_OPTS=${AUTH_USER:+--http-user=$AUTH_USER --http-passwd=$AUTH_PASS}
   FILE_OPTS="--output-file=$SYNC_LOG/$CONFIGNAME-`date +%C%y%m%d%H%M%S`"
   CUT_OPTS=${CUT_DIRS:+--cut-dirs=$CUT_DIRS}

   #
   # start pre command
   #
   test -x $(echo $CMD_PRE | cut -d" " -f1) && $CMD_PRE || logger -t youmirror -s "failed CMD_PRE: $CMD_PRE" 2>&1

   #
   # start wget 
   #
   (cd $DEST_DIR; $SYNC_CMD $SYNC_OPTS $WGET_OPTIONS $CUT_OPTS $PROXY_OPTS $PATH_DOWNLOAD $PATH_EXCLUDE $AUTH_OPTS $FILE_OPTS)
   echo "$SYNC_CMD $SYNC_OPTS $WGET_OPTIONS $CUT_OPTS $PROXY_OPTS $PATH_DOWNLOAD $PATH_EXCLUDE $AUTH_OPTS $FILE_OPTS" > $SYNC_LOG/.youmirror
   
   #
   # start post command
   #
   test -x $(echo "$CMD_POST" | cut -d" " -f1) && $CMD_POST || logger -t youmirror -s "failed CMD_POST: $CMD_POST" 2>&1

   #
   # report new files
   #
   cd $DEST_DIR
   TMP1=`mktemp you-newfiles.XXXXXXXXXX` || break; 

   #
   # there are 2 options to get a list of new packages
   # a) by file modification time only: "-mtime -1" for daily operation
   # b) by a timestamp file for the last run "-cnewer lastrun-$CONFIGNAME"
   #
   FINDOPTS="-cnewer $SYNC_LASTRUN/lastrun-$CONFIGNAME"
   #
   # we use the mtime method for the very first run
   #
   [ ! -f SYNC_LASTRUN/lastrun-$CONFIGNAME ] && FINDOPTS="-mtime -1"
   
   find . $FINDOPTS -type f -name "*.rpm" > $TMP1 && logger -t youmirror -s 2>&1 < $TMP1
  
   #
   # Mail new packages
   #
   if [ "$DOMAIL" == 1 ]
   then
      MAILTO=${MAILTO:-root}
      if [ `wc -l < $TMP1` -gt 0 ]
      then
        mail -s "[youmirror] files for config $CONFIGNAME" $MAILTO < $TMP1
      else
         echo "no new files for config $CONFIGNAME" | mail -s "[youmirror] files for config $CONFIGNAME" $MAILTO
      fi
   fi

   mv $TMP1 $SYNC_LASTRUN/lastrun-$CONFIGNAME
   logger -t youmirror -s "done config $CONFIGNAME" 2>&1
done

exit 0
#
