#! /bin/bash

if [ "$1" == "" ]; then
    cat <<EOF
 ** usage ./run.sh <Date> [<Regfile> [Time]]
     - Registration will begin immediately
     - Date is given in MM/DD format.
     - Registration file can given otherwise a fresh one
         will be created
     - Time can be given, as long as the Reg file is too,
         otherwise "11:00:05 PM" will be the start time
EOF
    exit 1;
fi

start=$1
stamp=`date +"%m-%d-%Y_%H_%M"`
regfile="registration_${stamp}.reg"
if [ "$2" != "" ]; then
    regfile=$2
fi
touch $regfile

time="11:00:05 PM";
if [ "$3" != "" ]; then
    time=$3
fi

dirname=`echo $start | sed 's|/|_|g'`
dirname="past_competitions/$dirname"

cat <<EOF
  * * * * * * * * * * * * * * * * * * * * *
  *   Running the Admin for Competitions
  *   Config:
  *     DATE = $start
  *     TIME = $time
  *      DIR = $dirname
  *      REG = $regfile
  * * * * * * * * * * * * * * * * * * * * *

EOF

cp files/group_config.txt files/config.txt && \
    mkdir -p group_1 && \
    java -Xss10M -jar ../admin.jar --savereg --partial "$start/2009 $time EST" $regfile --dontwait \
    --exceptions:EXCEPT_GRP.txt --nostatus && \
    mv *.log final_results* files/history/history* EXCEPT* group_1 && \
    \
    cp files/tourn_config.txt files/config.txt && \
    mkdir -p tournament_1 && \
    java -Xss10M -jar ../admin.jar --savereg --relative --partial --tournament 20 $regfile --dontwait \
    --exceptions:EXCEPT_TRN.txt --nostatus && \
    mv *.log final_results* tournament*.txt files/history/history* EXCEPT* tournament_1 && \
    mkdir -p $dirname && \
    mv group_1 tournament_1 $regfile $dirname && \
    echo "*********** ALL DONE *************"

