#! /bin/sh

# Do we need to run this with -f ? The clock might be wrong,
# but forcing it slows the install...
gtk-update-icon-cache -f /usr/share/icons/hicolor

oldversion="$2"
if [ -z "$oldversion" ]; then
  if [ -f /usr/bin/maemo-select-menu-location ]; then
    maemo-select-menu-location osm2go.desktop
  fi
fi

# there are two problems with the following approach:
# a) diablo/chinook units don't have these env variables
#    accessible to the app installer. We thus check for
#    /media/mmc2 explicitely but avoid this on scratchbox
# b) the installation may be done by the root user which
#    would result in an installation in roots home dir
#    if everything else fails

# determine path to use
if [ "$INTERNAL_MMC_MOUNTPOINT" != "" ]; then
  echo "MMC-PATH: INTERNAL_MMC_MOUNTPOINT env"
  MMC_PATH=$INTERNAL_MMC_MOUNTPOINT
elif [  "$_SBOX_DIR" == "" ] && [ -d /media/mmc2 ]; then
  echo "MMC-PATH: non-sbox explicit /media/mmc2"
  MMC_PATH=/media/mmc2
elif [ "$HOME" != "" ]; then
  echo "MMC-PATH: HOME env"
  MMC_PATH=$HOME
else
  echo "MMC-PATH: /tmp"
  MMC_PATH=/tmp
fi

# hide directory if we are working within a users home dir
if [ "`echo $MMC_PATH | cut -d / -f-2`" == "/home" ]; then
  MMC_PATH=$MMC_PATH/.osm2go
else
  MMC_PATH=$MMC_PATH/osm2go
fi

echo "Using data location at $MMC_PATH"

if [ -d $MMC_PATH ]; then
  echo "$MMC_PATH already exists, doing nothing."
else
  echo "$MMC_PATH doesn't exist yet. Installing demo project."
  mkdir $MMC_PATH
  mv /usr/share/osm2go/demo $MMC_PATH
fi

exit 0
