#!/bin/csh
# 
# File:    distribute_score
# Purpose: copies html to a staging area, then to the web dir and
#    any other location/machine needed.  Also invokes rotate_summary
# Author:  pc2@ecs.csus.edu
# Revised: Sat May 14 13:28:29 PDT 2005
# 
# $Id: distribute_score 1984 2009-12-09 02:40:47Z boudreat $
# 

# CUSTOMIZE HERE
set pc2_dir="/usr/pc2"
set start_dir="/home/scoreboard"
set start_dir="/root/contest/board"
set html_dir="$start_dir/html"
set stage_dir="$html_dir/staging"
set web_dir="/var/www/html"
# set web_dir to blank to disable (eg we are not running a web server here)
# set web_dir=""
set sum_dir="$web_dir/summary"
set rotate_script="$pc2_dir/samps/web/rotate_summary"
# set rotate_script to blank to disable (requires web_dir to be set)
# set rotate_script=""

# END CUSTOMIZATION for now, look for CUSTOMIZE HERE down below

if (! -d $html_dir) then
  echo "$html_dir does not exist, has pc2board been started yet ?"
  echo
  exit 99
endif

if (! -d $stage_dir) then
  echo "Creating $stage_dir"
  mkdir $stage_dir
  if (! -d $stage_dir) then
    echo "Could not create $stage_dir, check permissions of $html_dir"
    echo
    exit 96
  endif
endif

if ("x$web_dir" != "x") then
  if (! -d $web_dir) then
    echo "$web_dir does not exist, is httpd installed ?"
    echo
    exit 98
  endif

  if (! -d $sum_dir) then
    echo "Creating $sum_dir"
    mkdir $sum_dir
    if (! -d $sum_dir) then
      echo "Could not create $sum_dir, check permissions of $web_dir"
      echo
      exit 97
    endif
  endif
  cd $sum_dir
endif

if (! -f "$html_dir/summary.html") then
  echo "$html_dir/summary.html does not exist."
  echo "Has pc2board been started yet ?"
  echo
  exit 99
endif

# ignore error warning about it not being aliased
unalias cp > /dev/null
unalias rm > /dev/null
while(1)
  echo -n Copying html files at `date`
  cp $html_dir/*.html $stage_dir
  # we echo a . after each step
  echo -n .
  foreach i (`ls -1 $stage_dir`)
    # ensure html files are complete
    grep -qi '</html>' $stage_dir/$i
    if ($status ==  0) then
       # good
    else
       rm $stage_dir/$i
    endif
  end
  echo -n .
  if ("x$web_dir" != "x") then
    cp $stage_dir/*.html $web_dir
    echo -n .
    cp $web_dir/summary.html $sum_dir
    echo -n .
    if ("x$rotate_script" != "x") then
      # summary
      (cd $sum_dir;$rotate_script >& /dev/null)
      echo -n .
    endif
  endif
# CUSTOMIZE HERE
# to spectator scoreboard
#  scp -q $pc2_dir/pc2export.dat @score1:/home/team0/board
#  echo -n .
# to a remote web server
# use stage_dir here, as the html_dir may be in the process
# of being written to
#  (cd $stage_dir;scp -q summary.html scoreboard@127.0.0.2:upload)
#  echo -n .
#  (cd $stage_dir;scp -q *.html pc2@130.86.76.240:/var/www/html/pacnw09)
#  echo -n .
#
  #
# END CUSTOMIZATION
  echo done.
  sleep 30
end
