#!/bin/ash
#
#	This script sorts modules according to their popularity.
#	It can be run whenever you like, if your site has the
#	traffic accounting activated.
#	However, if your site does not have a very high trafic,
#	it is better never to run it, in order to preserve the
#	popularity register files coming with the wims distribution
#	which are based on the statistics of the popular wims home server.
#	

# base directory.
WIMS_HOME=`dirname $0`/..
cd $WIMS_HOME
WIMS_HOME=`pwd`

if [ ! -f log/account/00access.act ]; then
 echo Site accounting not available.
 exit
fi

# bonus for new modules
bonus=1000

# start and end date
start=`date -d '12 months ago' '+%Y%m%d'`
end=`date -d '1 day ago' '+%Y%m%d'`
endd=`date -d '1 day ago' '+%j'`
endy=`date -d '1 day ago' '+%Y'`
endt=`date -d '31 Dec' '+%j'`

cd log/account
stated=`ls [EHUGR]* tool* contrib* | grep '@'`

rm -f $WIMS_HOME/tmp/pop* &>/dev/null
for m in $stated incoming
do
 echo $m
 mm=`echo $m | sed 's/\.[a-z][a-z]$/.??/g'`
 cat $mm 2>/dev/null | sort -n >$WIMS_HOME/tmp/m
 access=`awk 'BEGIN {a=0;};
	$1>'$start' && $1<'$end' {a=a+$2;};
	END {print a};' $WIMS_HOME/tmp/m`
 beg=`awk '{print $1; exit}' $WIMS_HOME/tmp/m`
 if [ -z $beg ]; then
  access=0
  laps=2
 else
  if [ $beg -lt $start ]; then beg=$start; fi
  begd=`date -d $beg '+%j'`
  begy=`date -d $beg '+%Y'`
  begt=`date -d "31 Dec $begy" '+%j'`
  laps=$(( 365*($endy - $begy) + ($endd - $begd) + $begt - 365 + 1 ))
 fi
 pop=$(( (10*$access+$bonus)/$laps))
 echo $m:$m $pop >>$WIMS_HOME/tmp/pop1
 echo $m:$pop >>$WIMS_HOME/tmp/pop2
done

cd $WIMS_HOME
tr @ / <tmp/pop1 >tmp/popp
tr @ / <tmp/pop2 >tmp/popv
sort -n -r -k2 <tmp/popp >tmp/pop1.sorted
bin/dicsort tmp/popp
bin/dicsort tmp/popv

mv tmp/pop1.sorted public_html/bases/site/lists/popular
mv tmp/popp.sorted public_html/bases/site/pop/dic
mv tmp/popv.sorted public_html/bases/site/popular
rm -f tmp/pop* tmp/m &>/dev/null

