#!/bin/sh

# change to the folder in which the script is located
cd -- "$(dirname "$0")"
CWD=${PWD}
cd ..
CED=${PWD}
cd "${CWD}"
echo 
echo "-------------------------------------------------------------------------------"
echo "This file creates script-files to run"
echo "'Spana and DataBase (Chemical Equilibrium Diagrams)'"
echo " "
echo "Note that these scripts will be hard-coded to the present"
echo "location of the Chemical Equilibrium Diagrams software:"
echo "    '$CED'"
echo " "
echo "If you move the Chemical Equilibrium Diagrams folder"
echo "you must again run 'create_scripts'."
echo " "
echo "See also file 'Readme_MacOS.txt'."
echo "-------------------------------------------------------------------------------"
echo 
_JAR_="$( cd "$(dirname "$(readlink -f "$0")")/.."; pwd )"
DIR="$HOME/Desktop"
echo "folder where 'Chemical Equilibrium Diagrams' is located:"
echo "   '$_JAR_'"
echo "folders where the \"script\" files will be created:"
echo "   '$DIR'"
read -p "press Enter to continue ..."
OLD_PATH="$PWD"

echo " "
if [ -e "/usr/bin/java" ]
then
  echo "Note: java is already installed."
else
  echo "Note: java is probably NOT installed."
  echo "Please download and install java from https://www.java.com"
  read -p "press Enter to continue ..."
fi

APPNAME=Spana

cd "${CWD}"

## ----- Create the ".command" script
if [ -a "${CED}/${APPNAME}.command" ]
    then
    #echo --- removing "${CED}/${APPNAME}.command"
    rm "${CED}/${APPNAME}.command"
fi
echo '#!/bin/bash' > "${CED}/${APPNAME}.command"
echo >> "${CED}/${APPNAME}.command"
echo "cd -- \"$CED\""  >> "${CED}/${APPNAME}.command"
echo "java -jar \"$APPNAME.jar\" &" >> "${CED}/${APPNAME}.command"
chmod +x "${CED}/${APPNAME}.command"
mv "${CED}/${APPNAME}.command" "$DIR/${APPNAME}.command"

echo "---- made script ${APPNAME}.command"

APPNAME=DataBase

cd "${CWD}"
## ----- Create the ".command" script
if [ -a "${CED}/${APPNAME}.command" ]
	then
	#echo --- removing "${CED}/${APPNAME}.command"
	rm "${CED}/${APPNAME}.command"
fi
echo '#!/bin/bash' > "${CED}/${APPNAME}.command"
echo >> "${CED}/${APPNAME}.command"
echo "cd -- \"$CED\""  >> "${CED}/${APPNAME}.command"
echo "java -jar \"$APPNAME.jar\" &" >> "${CED}/${APPNAME}.command"
chmod +x "${CED}/${APPNAME}.command"
mv "${CED}/${APPNAME}.command" "$DIR/${APPNAME}.command"

echo "---- made ${APPNAME}.command scripts"

cd "$OLD_PATH"
OLD_PATH=
DIR=
_JAR_=
echo "All done."
echo " "
echo "Double-click on either"
echo "  'Spana.command'  or  'DataBase.command'"
echo "in the Desktop to run the corresponding program."
echo " "
echo "You can move these scripts to any folder you like using"
echo "Finder.  You can also change their icons with Finder"
echo "(see file 'Readme_MacOS.txt')."
echo " "
read -p "Press Enter to continue ..."
