#!/bin/sh

#
# Copyright(C) 2007-2012 National Institute of Information and Communications Technology
#

export LANG=C

dir=/opt/local/share/extractopinion-1.2

. $dir/conf.sh

export EXOPLIB=$dir/lib

cmd=`basename $0`

usage() {
  echo "Usage: $cmd [options] [csvfiles]"
  echo "Options: -h, --help            Show this message"
  echo "         -d, --dictionary DIR  Specify the dictionary directory"
  echo "         -m, --model      BASE Specify the base path of model files"
  echo "         -t, --tmpdir     DIR  Specify the temporary directory"
  exit -1
}

csvfiles=*.csv
model=./model

while test -n "$1"; do
    case "$1" in
        -h|--help)       usage ;;
        -d|--dictionary) dictionary="$2"; shift 2 ;;
        -m|--model)      model="$2";      shift 2 ;;
        -t|--tmpdir)     TMPDIR="$2";     shift 2 ;;
        *)               csvfiles="$@";   break ;;
    esac
done

workdir=${TMPDIR}/extractopinion-$$
mkdir -p $workdir/tsv

for file in $csvfiles
do
  f=`basename $file .csv`
  /opt/local/bin/perl5.34 -I $dir/lib $dir/makemodel/csv/csv2tsv.pl $file > $workdir/tsv/$f.tsv
done

cat $workdir/tsv/*.tsv > $workdir/data.tsv
cat $workdir/data.tsv  > $workdir/data_pol.tsv

$dir/_train.sh $model $workdir/data.tsv $workdir/data.tsv $workdir/data.tsv $workdir/data_pol.tsv $workdir/data.tsv
rm $workdir/data.tsv $workdir/data_pol.tsv
rm -rf $workdir
