#! /bin/sh

# Home directory of wims.
cd `dirname $0`/..
WIMS_HOME=`pwd`

today=`date '+%Y%m%d'`
weekday=`date '+%w'`
thisyear=`date '+%Y'`

# Forum management
cd log/forums
./.build-index
cd $WIMS_HOME

if [ "$site_accounting" = "yes" ]; then bin/account.sh; fi
sleep 10
cd $WIMS_HOME

# TeX pictures and fonts since 90 days: atime can no longer be used.
find public_html/w/instex -name '*.gif' -mtime +90 -exec rm '{}' 2>/dev/null \;
find public_html/w/instex -type d -empty -exec rm '{}' 2>/dev/null \;
find public_html/w/texgif -name '*.font' -mtime +90 -exec rm '{}' 2>/dev/null \;
# Clean tmp directory
find tmp -maxdepth 1 -type f -mtime +2 -exec rm '{}' 2>/dev/null \;
find tmp/forall -mindepth 1 -mtime +2 -exec rm '{}' 2>/dev/null \;
mkdir -p tmp/forall
chmod a+rwx tmp/forall

# Any expired classes?
cd log/classes
for cls in [0-9]*
do
 exp=`awk -F'=' '$1=="!set class_expiration" {print $2; exit}' $cls/.def`
 if [ -z "$exp" ] || [ "$exp" -lt "$today" ]; then
  email=`awk -F'=' '$1=="!set class_email" {print $2; exit}' $cls/.def`
  inst=`awk -F'=' '$1=="!set class_institution" {print $2; exit}' $cls/.def`
  desc=`awk -F'=' '$1=="!set class_description" {print $2; exit}' $cls/.def`
  host=`hostname`
  mail $w_mail_opt -s "WIMS virtual class expired" "$email" <<@

You have created a virtual class

	$inst, $desc

(code $cls) on the site $host. This class is now closed,
because it has reached its expiration date.

The complete registry of this class (user list and activity history) 
is stored in an archive file, which can be sent to you upon request
to the webmaster of this site.

--- WIMS (WWW Interactive Multipurpose Server)
    http://$host/

@
  echo "$today housekeep class-expire $cls: $email" >>$WIMS_HOME/log/mail.log
  rm -R $cls/.parmreg >/dev/null 2>&1
  if [ -d $WIMS_HOME/log/ccaccount/$cls ]; then
   tar -cf $cls/account.tar -C $WIMS_HOME/log ccaccount/$cls 2>/dev/null
   rm -R $WIMS_HOME/log/ccaccount/$cls 2>/dev/null
  fi
  mkdir -p $WIMS_HOME/backup/oldclasses/$thisyear
  tar -czf $WIMS_HOME/backup/oldclasses/$thisyear/$cls.tgz $cls
  rm -R $cls >/dev/null 2>&1
  # clean resource sharing links
  find . -maxdepth 2 -type l -lname '*/$cls/*' -exec rm '{}' \;
 fi
done

# Clean up remaining classes.
cd $WIMS_HOME/log/classes
find . -path '*/.parmreg/*' -type f -exec rm -f '{}' \;
for l in `find . -name examlog -type d`; do
 for d in $l/*; do
   list=`ls -dt $d/* | tail +$examlog_lim2`
   if [ -n "$list" ]; then echo rm -Rf $list; fi
 done
done
# Sheet expiration management. This should be activated during summer vacation.

for s in `find . -path '*/sheets/.sheets' -type f`; do
# quick test : any sheet to expire now ?
   test=`awk 'BEGIN {a=0};
	a==1 {a=0; if($1<'$today' && b==1) {print "Z"; exit}}
	substr($1,1,1)==":" {a=1;b=substr($1,2,1)};' $s`
# if test then change status code to 2 for sheets to expire
# in temporary file, then overwrite old file with new one
   if [ ! -z "$test" ]; then
    awk 'BEGIN {a=0; v=0};
	a==1 {a=0; if($1>='$today' || v>1) {print t} else {print ":2"};}
	substr($1,1,1)!=":" {print};
	substr($1,1,1)==":" {a=1; t=$1; v=substr(t,2)};' $s >.sheets
    mv -f .sheets $s
   fi
done

# Build class index file
./.build-index

cd $WIMS_HOME/log

if [ -d ccaccount ]; then
 for c in `cd ccaccount; ls -d [0-9]* 2>/dev/null`; do
  if [ ! -d classes/$c ]; then rm -rf ccaccount/$c; fi
 done
fi

# possible local housekeeping script
cd $WIMS_HOME
[ -x log/housekeep.local ] && log/housekeep.local

