#!/bin/sh
export JH_VERSION=1.1.855
JH_HOMEDIR=~
JH_DESKTOP=
JH_BREAKINSTALL=
JH_KEEPCONFIG=

JH_PRNT_BOLD=`tput bold`
JH_PRNT_NORM=`tput sgr0`

while true; do
JH_INSTALLDIR=
for a in `seq 72`; do echo -n -; done
echo ""
echo "Please enter the directory to install ${JH_PRNT_BOLD}Jackhammer $JH_VERSION (32-bit)${JH_PRNT_NORM}:"
echo "(the directory is relative to your home directory: \"$JH_HOMEDIR\")"
read JH_INSTALLDIR
if [ -z "$JH_INSTALLDIR" ]; then
	JH_INSTALLDIR="jackhammer"
fi
export JH_INSTALLDIR="$JH_HOMEDIR/$JH_INSTALLDIR"
for a in `seq 72`; do echo -n -; done
echo ""
echo "Installing ${JH_PRNT_BOLD}Jackhammer $JH_VERSION${JH_PRNT_NORM} to: \"$JH_INSTALLDIR\""
while true; do
    read -p "Do you wish to continue? (y/n) " yn
    case $yn in
        [Yy]* ) break ;;
        [Nn]* ) JH_BREAKINSTALL=1 
                break
                ;;
        * ) echo "Please answer yes or no." ;;
    esac
done

if ! [ -z "$JH_BREAKINSTALL" ]; then
    break
fi

if ! [ -d $JH_INSTALLDIR ]; 
then
	mkdir $JH_INSTALLDIR
fi
if ! [ -d $JH_INSTALLDIR ]; 
then
	echo 'Failed to create a directory. Please try another install path.'
	continue
fi
break
done

if ! [ -z "$JH_BREAKINSTALL" ]; then
    echo "Bye."
    return
fi

if [ -f "$JH_INSTALLDIR/VDKGameCfg.ini" ]; then
while true; do
    read -p "Do you want to keep your config files? (y/n) " yn
    case $yn in
        [Nn]* ) break ;;
        [Yy]* ) JH_KEEPCONFIG=1 
                break
                ;;
        * ) echo "Please answer yes or no." ;;
    esac
done
if ! [ -z "$JH_KEEPCONFIG" ]; then
    rm "VDKGameCfg.ini"
    rm "VDKRunCfg.ini"
    rm "VDKSettings.ini"
    rm "VDKLayout.dat"
fi
fi

echo "Copying files..."

cp -r * $JH_INSTALLDIR
rm "$JH_INSTALLDIR/install.sh"

echo "Generating desktop file..."
JH_ESCAPED_INSTALLDIR=$(echo $JH_INSTALLDIR | sed -e 's/\//\\\//g')
sed -i "s/{JH_INSTALLDIR}/$JH_ESCAPED_INSTALLDIR/g" $JH_INSTALLDIR/jackhammer.desktop
sed -i "s/{JH_VERSION}/$JH_VERSION/g" $JH_INSTALLDIR/jackhammer.desktop

for a in `seq 72`; do echo -n -; done
echo ""
echo "All done. Thank you for installing Jackhammer."
return
