#!/bin/sh
#
# MOBAC startscript
#
#---As mobac insists in having application, cache and data all at one single place,
#---we need to do some light-weight installation at program start time.
#---You can set the environment variable MOBAC_HOME to set a custom home for mobac:
mobacdir="${MOBAC_HOME:-$HOME/MOBAC}"
mkdir -vp -- "$mobacdir" || exit 1
cd -- "$mobacdir"
echo "Executing mobac in directory $mobacdir."
echo "You may change location by setting the environment variable MOBAC_HOME."

dfile="$mobacdir/Mobile_Atlas_Creator.jar"
sfile="/usr/share/mobac/Mobile_Atlas_Creator.jar"
if [ -L "$dfile" ]; then
  rm -- "$dfile"
fi
if [ -f "$dfile" ] && [ "$sfile" -nt "$dfile" ]; then
  rm -- "$dfile"
fi
install -p "$sfile" "$dfile"

dfile="$mobacdir/MapEvaluator.jar"
sfile="/usr/share/mobac/MapEvaluator.jar"
if [ ! -L "$dfile" ]; then
  ln -srf -- "$sfile" "$dfile"
fi

ln -srf -- "/usr/share/mobac/world.map" .
ln -srf -- "/usr/share/mobac/README.HTM" .
#---We create a physical mapsources directory, so we can add custom entries if desired.
#---Content of the installation mapsources is symlinked to the user mapsources directory.
if [ ! -d "$mobacdir/mapsources" ]; then
  mkdir -p -- "$mobacdir/mapsources"
fi
if [ -d "$mobacdir/mapsources" ] && [ ! -L "$mobacdir/mapsources" ]; then
  for item in "/usr/share/mobac/mapsources/"*; do
    ln -srf -- "$item" "$mobacdir/mapsources/"
  done
fi
#---Delete stale symlinks:
for item in "$mobacdir/"* "$mobacdir/mapsources/"*; do
  if [ ! -e "$item" ] && [ -L "$item" ]; then
    rm -- "$item"
  fi
done

java -Xms64m -Xmx1200M -jar "./Mobile_Atlas_Creator.jar" "$@"
