#!/bin/bash

if [ $# -ne 5 ] ; then
  echo "Usage: run ALG DATADIR FRAC DELTA WORKDIR"
  exit
fi
  
ALG=$1
DATADIR=$2
FRAC=$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/* .

# ignore the unlabeled data, split the labeled into a train and test set
mv labeled tmp
$BASEDIR/split $FRAC tmp
mv labeled train
mv unlabeled test
rm tmp

# train and test
cd $BASEDIR/algs/$ALG
train $WORKDIR/train
predict $WORKDIR/test $WORKDIR/train $WORKDIR/test_preds
cd $BASEDIR

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

# compute the test set bound
algs/$ALG/labels $WORKDIR/test > $WORKDIR/tmp
errors=`errors $WORKDIR/tmp $WORKDIR/test_preds`
cases=`lines $WORKDIR/tmp`
bound=`upper_bound $DELTA $cases $errors`
rm $WORKDIR/tmp;

echo $bound `perl -e "print $errors / $cases"` > $WORKDIR/results
cat $WORKDIR/results
GLOBIGNORE=$WORKDIR/results
rm $WORKDIR/*
