#!/bin/csh
#----A shell script for starting Prolog and doing TPTP2X conversions.
#----tptp2X must be configured before using this.

#----Get directories information
echo -n "Absolute name of TPTP2X directory      : "
set TPTP2XDirectory=$<

#----Check that tptp2X.config exists (rough check of the directory)
if (!(-f $TPTP2XDirectory/tptp2X.config)) then
    echo "Cannot find the tptp2X.config file there."
    exit
    endif

#----Get the TPTP directory from tptpread
set PrologFact=`grep "^tptp_directory(" $TPTP2XDirectory/tptp2X.config`
set TPTPDirectory=`expr match $PrologFact "tptp_directory('\(.*\)'). *"`

#----Check that the Problems directory is there
if (!(-d $TPTPDirectory/Problems)) then
    echo "Cannot find the Problems directory."
    echo "Check the tptp_directory fact in tptp2X.config"
    exit
    endif

#----Get Prolog name
echo -n "Name of your Prolog interpreter        : "
set PrologInterpreter=$<

#----Get the required information from the user
echo    "Names of files to convert, relative to $TPTPDirectory/Problems "
echo -n "    (e.g., S??/*)                      : "
set FilesToConvert=$<
echo    "Tranformations to apply"
echo -n "    (e.g., [clr,magic])                : "
set Transformations=$<
echo    "Formats to convert to "
echo -n "    (e.g., setheo)                     : "
set FormatToConvertTo=$<
echo    "Absolute name of output directory"
echo -n "    (e.g., ~/ATP/TPTPProblems)         : "
set OutputDirectory=$<

# echo "TPTP2X directory      = $TPTP2XDirectory"
# echo "TPTP directory        = $TPTPDirectory"
# echo "Prolog interpreter    = $PrologInterpreter"
# echo "Files to convert      = $FilesToConvert"
# echo "Transformations       = $Transformations"
# echo "Format to convert to  = $FormatToConvertTo"
# echo "Output directory      = $OutputDirectory"

#----Make an input file for Prolog
echo "compile(tptp2X)." >$TPTPDirectory/PrologScript

cd $TPTPDirectory/Problems
#----Look for the files from this directory
foreach FilePath ($FilesToConvert)
    set Domain=`dirname $FilePath`
    if (!(-d $OutputDirectory/$Domain)) then
        echo Making the directory $OutputDirectory/$Domain
        mkdir $OutputDirectory/$Domain
        endif
    echo "tptp2X('$TPTPDirectory/Problems/$FilePath',$Transformations,$FormatToConvertTo,'$OutputDirectory/$Domain')." >>$TPTPDirectory/PrologScript
    end
echo "halt." >>$TPTPDirectory/PrologScript

#----Invoke the Prolog interpreter with this input
cd $TPTP2XDirectory
$PrologInterpreter <$TPTPDirectory/PrologScript

rm $TPTPDirectory/PrologScript
