#!/bin/sh
############################################################################
#                                                                          #
# Copyright (c) 2012, 2013, 2014, 2016, 2019, 2020.                        #
#  JXCirrus.                                                               #
#                                                                          #
# THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE.                             #
#                                                                          #
# No part of this source code may be copied, compiled, or included within  #
# any other applications, without the written permission of the owner      #
# (stated above).   All Rights Reserved.                                   #
#                                                                          #
############################################################################
#@Summary  Installs JXCirrusPrayer on Linux.
############################################################################

exe=JXCirrusPrayer
product="JXCirrus Prayer"
script=$0
scriptOnly=`basename $0`
scriptDir=`dirname $0`
oldDir=`pwd`
cd $scriptDir
scriptDir=`pwd`
cd $oldDir
if [ "$scriptDir" = "" ]
then
    scriptDir=.
fi
productShort=`echo $product | sed -e "s/ //g"`

usage () {
    echo "Usage: $scriptOnly [TargetDir]"
    echo
    echo "   TargetDir = The directory to install $product to."
}

while [ $# -gt 0 ]
do
    if [ $1 = "-h" ]
    then
        usage
        exit 0
    else
        targetDir=$1
    fi
    shift
done

echo "==============================================================================="
echo "Welcome to the $product installer..."
echo " "

if [ `uname -p` != "x86_64" ]
then
    echo "Unfortunately, $product is no longer able to support 32-bit"
    echo "Linux systems, and has moved fully to 64-bit."
    echo ""
    echo "Please contact support on info@jxcirrus.com if you have concerns."
    exit 1
fi

if [ "$targetDir" = "" ]
then
    echo "Please enter the directory you want to install to."
    echo " (or press Enter to install to [$HOME/$productShort])"
    echo
    read targetDir
    if [ "$targetDir" = "" ]
    then
        targetDir=$HOME/$productShort
    fi
fi

mkdir -p $targetDir
if [ -d $targetDir ]
then
    echo "Installing to $targetDir"
else
    echo "ERROR: Unable to create directory $targetDir"
    exit 1
fi
if [ -w $targetDir ]
then
    echo
else
    echo "ERROR: Unable to write to directory $targetDir"
    exit 1
fi

cd $scriptDir

echo "Press Enter to view the License Agreement"
read ans
echo "------------------------------------------------------------------------"
cat eula.txt
echo "------------------------------------------------------------------------"
while true
do
    echo "Type \"A\" to Accept or \"D\" to Decline"
    read ans
    case $ans in
        [Aa]*)
            break
            ;;
        [Dd]*)
            echo "Aborting installation."
            exit 0
            ;;
    esac
done

echo "Installing..."
if [ "$targetDir" != "." ]
then
    cp -r * $targetDir
fi

cd $targetDir
runScript=$targetDir/run_${exe}.sh
echo "Installation of $product is now complete."
echo " NOTE: The full path to the executable is:"
echo " "
echo "   $runScript"
echo " "
echo "Would you like to run $product now [Y/n]?"
read ans
case $ans in
    [Nn]*)
        ;;
    *)
        $runScript
        ;;
esac
