#!/bin/bash

PATH="/usr/local/bin:$PATH"

echo "------------------------"
echo "Daily bot starting"
date

DOADMINS="y"
DOHOPEFULS="n"
DOW=`date +"%a"`
if [ $DOW = "Sun" -o $DOW = "Wed" ]; then
  DOHOPEFULS="y"
fi
DOFEATUREDS="y"
DOFEATUREDL="y"
DOFEATUREDT="y"
DOCFD="y"

#--------------- Commonly used variables

THISMONTH=`date +%B`
THISYEAR=`date +%Y`
let LASTYEAR=$THISYEAR-1

function prevmonth () {
  case $1 in
    January) echo "December";;
    February) echo "January";;
    March) echo "February";;
    April) echo "March";;
    May) echo "April";;
    June) echo "May";;
    July) echo "June";;
    August) echo "July";;
    September) echo "August";;
    October) echo "September";;
    November) echo "October";;
    December) echo "November";;
  esac
}

LASTMONTH=`prevmonth $THISMONTH`
LASTMONTHYEAR=$THISYEAR
if [ $LASTMONTH = "December" ]; then
  let LASTMONTHYEAR=$LASTMONTHYEAR-1
fi

#--------------- List of administrators

if [ $DOADMINS = "y" ]; then
cd /Users/Rick/pywikipedia/Admins
  ./getadminactivity $*
cd ..


if [ `cat ./Admins/LA.active` -gt 500 ]; then
  for FILE in "A-F" "G-O" "P-Z" "Semi-active" Inactive; do
    python replace.py -always -content:./Admins/$FILE -page:Wikipedia:List_of_administrators/$FILE "-summary:Daily update"
  done
  python replace.py -always -content:./Admins/LA.new -page:Wikipedia:List_of_administrators "-summary:Daily update, `cat ./Admins/LA.active` active admins"
fi

fi

#--------------- List of administrators

if [ $DOHOPEFULS = "y" ]; then

cd /Users/Rick/pywikipedia/Adminhopefuls
  ./gethopefuls
cd ..

NEWFILE=./Adminhopefuls/tmp.wphopefuls
NLines=`cat $NEWFILE | wc -l`
NLines=`expr $NLines`
if [ "$NLines" -gt 1000 ]; then
  python replace.py -always -content:$NEWFILE -page:Wikipedia:List_of_administrator_hopefuls -summary:"Twice weekly update"
fi

fi

#--------------- Featured article nominations

if [ $DOFEATUREDS = "y" ]; then
cd ./Featureds
  rm -f ./FAnoms.$THISYEAR
  rm -f ./FAnoms.$LASTYEAR
  ./get_featureds 2>&1
  ./get_formers 2>&1

  ./newnoms
  cd ..
    for YEAR in $LASTYEAR $THISYEAR; do
      if [ -s "./Featureds/FAnoms.$YEAR" ]; then
        python replace.py -content:./Featureds/FAnoms.$YEAR -page:Wikipedia:Featured_articles_promoted_in_$YEAR "-summary:Add new promotions"
      fi
    done
  cd ./Featureds

  ./update_maindate 2>&1
  ./check_maindate 2>&1

  # wait for the cache to catch up
  sleep 60

  rm -f neverFA
  YEAR=2003
  while [ $YEAR -le $THISYEAR ]; do
    ./update_byyear $YEAR
    let YEAR=$YEAR+1
  done
  ./update_wbfan

cd ..

YEAR=2003
while [ $YEAR -le $THISYEAR ]; do
  python replace.py -always -content:./Featureds/FAnoms.$YEAR -page:Wikipedia:Featured_articles_promoted_in_$YEAR -summary:autoupdate
  let YEAR=$YEAR+1
done
python replace.py -always -content:./Featureds/WBFAN -page:Wikipedia:List_of_Wikipedians_by_featured_article_nominations -summary:autoupdate

fi

#--------------- Featured list nominations

if [ $DOFEATUREDL = "y" ]; then
cd ./FeaturedL
  rm -f ./FLnoms.$THISYEAR
  rm -f ./FLnoms.$LASTYEAR
  ./get_featureds 2>&1
  ./get_formers 2>&1

  ./newnoms
  cd ..
    for YEAR in $LASTYEAR $THISYEAR; do
      if [ -s "./FeaturedL/FLnoms.$YEAR" ]; then
        python replace.py -always -content:./FeaturedL/FLnoms.$YEAR -page:Wikipedia:Featured_lists_promoted_in_$YEAR "-summary:Add new promotions"
      fi
    done
  cd ./FeaturedL

  # wait for the cache to catch up
  sleep 60

  rm -f neverFA
  YEAR=2005
  while [ $YEAR -le $THISYEAR ]; do
    ./update_byyear $YEAR
    let YEAR=$YEAR+1
  done
  ./update_wbfln

cd ..

YEAR=2005
while [ $YEAR -le $THISYEAR ]; do
  python replace.py -always -content:./FeaturedL/FLnoms.$YEAR -page:Wikipedia:Featured_lists_promoted_in_$YEAR -summary:autoupdate
  let YEAR=$YEAR+1
done
python replace.py -always -content:./FeaturedL/WBFLN -page:Wikipedia:List_of_Wikipedians_by_featured_list_nominations -summary:autoupdate

fi

#--------------- Featured topic nominations

if [ $DOFEATUREDT = "y" ]; then
cd ./FeaturedT
  rm -f ./FTnoms.$THISYEAR
  rm -f ./FTnoms.$LASTYEAR
  ./get_featureds 2>&1
  ./get_formers 2>&1

  ./newnoms
  cd ..
    for YEAR in $LASTYEAR $THISYEAR; do
      if [ -s "./FeaturedT/FTnoms.$YEAR" ]; then
        python replace.py -always -content:./FeaturedT/FTnoms.$YEAR -page:Wikipedia:Featured_topics_promoted_in_$YEAR "-summary:Add new promotions"
      fi
    done
  cd ./FeaturedT

  # wait for the cache to catch up
  sleep 60

  rm -f neverFA
  YEAR=2006
  while [ $YEAR -le $THISYEAR ]; do
    ./update_byyear $YEAR
    let YEAR=$YEAR+1
  done
  ./update_wbftn

cd ..

YEAR=2006
while [ $YEAR -le $THISYEAR ]; do
  python replace.py -always -content:./FeaturedT/FTnoms.$YEAR -page:Wikipedia:Featured_topics_promoted_in_$YEAR -summary:autoupdate
  let YEAR=$YEAR+1
done
python replace.py -always -content:./FeaturedT/WBFTN -page:Wikipedia:List_of_Wikipedians_by_featured_topic_nominations -summary:autoupdate

fi

#--------------- CFD archives

if [ $DOCFD = "y" ]; then
# update CFD archive index
rm -f ./Cfd/$LASTMONTH
cd Cfd
  ./update_archive
cd ..
OLD=`cat Cfd/CFDarchives | wc -l`
NEW=`cat Cfd/CFDarchives.n | wc -l`
DIFF=`expr $NEW - $OLD`
if [ $DIFF -gt 1 ]; then
  python replace.py -always -content:./Cfd/CFDarchives.n -page:Wikipedia:Categories_for_discussion/Archive_debates -"summary:Add index table for $THISMONTH"
fi

if [ -s ./Cfd/$LASTMONTH ]; then
  python replace.py -always -content:./Cfd/$LASTMONTH -page:Wikipedia:Categories_for_discussion/Archive_debates/${LASTMONTHYEAR}_${LASTMONTH}_index -"summary:Add index for $LASTMONTH"
fi

fi

echo finished

echo