#!/bin/sh -e

# The clock might be wrong and we know that we need to update the icon
# cache so we just force it.

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 enigma.desktop
  fi
fi

echo "Environment:"
env

# determine path to use
if [ "`mount | grep /home/user`" != "" ]; then
  echo "MMC-PATH: mount /home/user"
  MMC_PATH=/home/user
elif [ "`mount | grep /media/mmc2`" != "" ]; then
  echo "MMC-PATH: mount /media/mmc2"
  MMC_PATH=/media/mmc2
fi

if [ "$MMC_PATH" != "" ]; then 
  # hide directory if we are working within a users home dir
  if [ "`echo $MMC_PATH | cut -d / -f-2`" == "/home" ]; then
    # .enigma may already be used by the app itself
    MMC_DIR=.enigma-datadir
  else
    MMC_DIR=enigma
  fi

  echo " memcard location: $MMC_PATH"

  # make sure that there's enough free disk space
  NEED=13000
  FREE=`df $MMC_PATH | grep -vE '^Filesystem' | awk '{ print $4 }'`
  if [ $FREE -ge $NEED ]; then
    if maemo-confirm-text "Memory usage" /usr/share/doc/enigma/install_memcard.txt; then
      echo "Moving data files from /usr/share to $MMC_PATH/$MMC_DIR"
      mv /usr/share/enigma $MMC_PATH/$MMC_DIR
    fi
  fi
else
  echo "No memcard path found!"
fi

exit 0
