#!/bin/csh
#--------------------------------------------------------------------------
clear
echo "--------------------------------------------------------------------"
echo "                This is the tptp1T installation"
echo ""
echo "Press return to accept the default values in []s"
echo "--------------------------------------------------------------------"
#--------------------------------------------------------------------------
#----TPTP directory
#----Look for being in or below TPTP-v*
set TPTPDirectory=`expr "$cwd" : "\(.*TPTP-v[0-9\.]*\).*"`
#----Look for being below TPTP (for me)
if ("$TPTPDirectory" == "") then
    set TPTPDirectory=`expr "$cwd" : "\(.*TPTP\)\/.*"`
    endif
#----Look for being in TPTP (for me)
if ("$TPTPDirectory" == "") then
    set TPTPDirectory=`expr "$cwd" : "\(.*TPTP\)"`
    endif
#----Look for being above TPTP-v* (for installation)
if ("$TPTPDirectory" == "") then
    set LSOutput=`ls -d TPTP-v?.?.?`
    if ("$LSOutput" != "" && (-d "$LSOutput")) then
        set TPTPDirectory="$cwd/"`expr "$LSOutput" : "\(.*TPTP-v[0-9\.]*\).*"`
        endif
    endif

#----Now ask the user
set LegalResponse=no
echo "The TPTP directory must contain the Scripts directory."
while ($LegalResponse != yes)
    echo -n "TPTP directory        [$TPTPDirectory] : "
    set Response=$<
    if ($Response == "") then
        set Response=$TPTPDirectory
        endif
#----Check for non empty response
    if ("$Response" != "") then
#----Check that the TPTP directory exists
        if (!(-d $Response)) then
            echo "Cannot find the TPTP directory."
            continue
            endif
        set ScriptsDirectory=$Response/Scripts
#----Check that the Scripts directory exists
        if (!(-d $ScriptsDirectory)) then
            echo "Cannot find the Scripts directory."
            continue
            endif
#----Check that tptp1T exists (rough check of the directory)
        if (!(-f $ScriptsDirectory/tptp1T)) then
            echo "Cannot find the tptp1T file."
            continue
            endif
        set LegalResponse=yes
        set TPTPDirectory=$Response
        endif
    end
echo "--------------------------------------------------------------------"
#--------------------------------------------------------------------------
#----Perl
set Perl=perl
set Version="version 5|v5"
set PerlPath=`which $Perl`

set LegalResponse=no
while ($LegalResponse != yes)
    echo -n "perl version 5        [$PerlPath] : "
    set Response=$<
    if ($Response == "") then
        set Response=$PerlPath
        endif
#----Check for non empty response
    if ("$Response" != "") then
#----Check that perl can be found
        set AbsolutePath=`which $Response`
#----Have to do this in two steps so -x check lives
        if ("$AbsolutePath" =~ /*not*) then
            set AbsolutePath=very_unlikely_file_name
            endif
        if ("$AbsolutePath" !~ /*) then
            set AbsolutePath=very_unlikely_file_name
            endif
        if (-x $AbsolutePath) then
#----Check that it's perl version 5.something
            set VersionLine = `$AbsolutePath -v | egrep "$Version"`
            if ("$VersionLine" != "") then
                set LegalResponse=yes
                set PerlPath=$AbsolutePath
            else
                echo "$PerlPath is not perl version 5"
                endif
        else
            echo $Response does not lead to an executable.
            endif
        endif
    end
#--------------------------------------------------------------------------
#----Install tptp1T: 
echo Installing tptp1T
#----Have to use ? as the separator as there are /s in the substitutions
sed -e "1s?.*?#\!$PerlPath?" -e "/^my .TPTPDirectory =/s?= .*?= '$TPTPDirectory';?" $ScriptsDirectory/tptp1T >$ScriptsDirectory/tptp1T.new

#----Move the new file into place
# mv $ScriptsDirectory/tptp1T $ScriptsDirectory/tptp1T.old
mv -f $ScriptsDirectory/tptp1T.new $ScriptsDirectory/tptp1T

#----Set the mode to executable
chmod 755 $ScriptsDirectory/tptp1T
#--------------------------------------------------------------------------
echo "--------------------------------------------------------------------"
echo "                tptp1T installation complete"
echo "--------------------------------------------------------------------"
#--------------------------------------------------------------------------
