#!/bin/sh

# figure out install paths

# Looking for java from path 

echo "---------------------------------------------------------"
echo " sxQT Installer v3_0_1"
echo ""
echo "Please choose a Java virtual machine to run this program."
echo "(These virtual machines were found on your PATH)"
echo "---------------------------------------------------------"
getjavas='BEGIN {FS=":"}{ for(i=1;i<=NF;i++) print $i; }'

# Printing list of javas and jres
javac=1
for file in `echo $PATH | awk "$getjavas"`
     do
        if [ -x $file/java -a ! -d $file/java ]
        then
            echo $javac: $file/java
            eval part$javac="$file/java"
            javac=`expr $javac + 1`
        fi

        if [ -x $file/jre -a ! -d $file/jre ]
        then
            echo $javac: $file/jre
            eval part$javac="$file/jre"
            javac=`expr $javac + 1`
        fi
     done
echo "$javac:  Exit."

# If no VMs are found in path
if [ $javac -eq 1 ]
then
    echo "You don't seem to have java in your path."
    echo "Please install java first."
    exit
fi
        
echo "Please enter your selection (default 1):  "
read sel
if [ "$sel" = "" ] ; then
  sel=1
fi
#  Checking for validity
if [ $sel -eq $javac ] 
then 
        exit 
elif [ $sel -lt 1 -o $sel -ge $javac ]
then
        echo "Invalid Selection:  This program will terminate."
        exit
fi

eval seljava="\$part$sel"

$seljava -version > sxinsttmp 2>&1
version=`cat sxinsttmp | grep version`
rm -f sxinsttmp
echo "selected $seljava -- $version"
echo "If this is less than version 1.2, sxQT will not work."
echo "Continue? [yes]/no"
read answer
if [ "$answer" = "n" -o "$answer" = "N" -o "$answer" = "no" -o "$answer" = "NO" -o "$answer" = "No" ] ; then
 exit
fi

echo " "
echo "Please enter your installation path: (default /usr/local/bin)"
read instpath

if [ "$instpath" = "" ] ; then
  instpath="/usr/local/bin"
fi

if [ -d $instpath ] ; then
  touch $instpath/sxQT > /dev/null 2>&1
  if [ $? -ne 0 ] ; then
    echo "Can't write to $instpath"
    exit
  fi
  rm -f $instpath/sxQT
  echo "Installing to $instpath"
else
  echo "Directory $instpath does not exist. Create? [yes/no]"
  read answer
  if [ "$answer" = "y" -o "$answer" = "Y" -o "$answer" = "yes" -o "$answer" = "YES" -o "$answer" = "Yes" ] ; then
    echo "Creating directory $instpath"
    mkdir $instpath
  else
    echo "Installation aborted"
    exit
  fi
fi

# this decodes the sxQT.jar file into the current directory
uudecode $0
mv sxQT.jar $instpath

# create the sxQT executable
echo "#!/bin/sh" > $instpath/sxQT
echo "#--------------------------" >> $instpath/sxQT
echo "#" >> $instpath/sxQT
echo "exec $seljava -jar $instpath/sxQT.jar \"\$@\"" >> $instpath/sxQT

chmod 755 $instpath/sxQT
echo ""
echo ""
echo "done. Please make sure that $instpath is in your path."
echo "You can run sxQT by issuing it from the command line."

# exit. data of the sxQT.jar follows below.
exit
