#!/bin/bash

if [ $# -ne 5 ] ; then
  echo "Usage: run ALG DATADIR STEPS DELTA WORKDIR"
  exit
fi
  
ALG=$1
DATADIR=$2
STEPS=$3
DELTA=$4
WORKDIR=$5
STARTDIR=`pwd`
if [ $SEMIBOUND_DIR ] ; then
  BASEDIR=$SEMIBOUND_DIR
else
  echo "SEMIBOUND_DIR not defined!"
  exit
fi

if [ ! -d $DATADIR ] ; then 
  echo "Directory $DATADIR does not exist!"
  exit
fi
cd $DATADIR
DATADIR=`pwd`
cd $STARTDIR

# copy the data to workdir
if [ -e $WORKDIR ] ; then
  echo "Directory $WORKDIR already exists!"
  exit
fi
mkdir -p $WORKDIR
cd $WORKDIR
WORKDIR=`pwd`

cp $DATADIR/* .

# online specific stuff starts here
$BASEDIR/online/gen_samples $STEPS
cd $BASEDIR

# use 0 as the default classifier
perl -ne 'print "0\n"' $WORKDIR/labeled_1 > $WORKDIR/labeled_1_preds
cp $WORKDIR/labeled_1_preds $WORKDIR/labeled_all_preds
perl -ne 'print "0\n"' $WORKDIR/unlabeled > $WORKDIR/unlabeled_0_preds
touch $WORKDIR/train_0
cd algs/$ALG
for i in `seq 1 $STEPS`; do
  cat $WORKDIR/train_$(($i-1)) $WORKDIR/labeled_$i > $WORKDIR/train_$i
  rm $WORKDIR/train_$(($i-1))
  train $WORKDIR/train_$i
  if [ $i -lt $STEPS ] ; then
    predict $WORKDIR/labeled_$(($i+1)) $WORKDIR/train_${i} $WORKDIR/labeled_$(($i+1))_preds
    cat $WORKDIR/labeled_$(($i+1))_preds >> $WORKDIR/labeled_all_preds
  fi
  predict $WORKDIR/unlabeled $WORKDIR/train_${i} $WORKDIR/unlabeled_${i}_preds
done;
cd $BASEDIR

for i in $WORKDIR/*_preds ; do
  algs/$ALG/labels $i > $WORKDIR/tmp
  mv $WORKDIR/tmp $i;
done;

algs/$ALG/labels $WORKDIR/labeled > $WORKDIR/tmp
cumerr=`errors $WORKDIR/tmp $WORKDIR/labeled_all_preds`
labeled_cases=`lines $WORKDIR/labeled_all_preds`
DELTASHARE=`perl -e "print $DELTA / 2"`
bound_for_rand=`online/bound $cumerr $labeled_cases $DELTASHARE`

# compute the distance from the randomized to the final hypothesis
ensemble `ls $WORKDIR/unlabeled_*_preds | grep -v $WORKDIR/unlabeled_${STEPS}_preds` > $WORKDIR/unlabeled_ensemble
random_predict $WORKDIR/unlabeled_ensemble > $WORKDIR/unlabeled_ensemble_preds 
errors=`errors $WORKDIR/unlabeled_${STEPS}_preds $WORKDIR/unlabeled_ensemble_preds`
cases=`lines $WORKDIR/unlabeled_${STEPS}_preds`
DELTASHARE=`perl -e "print $DELTA / 2"`
bound_dist=`upper_bound $DELTASHARE $cases $errors`

# output the bound
perl -e "print $bound_for_rand + $bound_dist" > $WORKDIR/results
echo "" $bound_for_rand $bound_dist >> $WORKDIR/results
cat $WORKDIR/results
GLOBIGNORE=$WORKDIR/results
rm $WORKDIR/*
