#!/bin/bash

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

# use the (labeled) unlabeled data as a test set
mv labeled train
mv unlabeled test

# 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/*
