#!/bin/sh -e

if grep -q /home/user/MyDocs /proc/mounts; then
# FAT partition mounted
  mkdir -p /home/user/MyDocs/wagic
  mkdir -p /home/user/MyDocs/wagic/Res
# Size needed on the FAT partition
  dirSize=`du -s /home/user/wagic/Res | awk '{print $1}'`
# Size available on the FAT partition
  avaSize=`df | grep /dev/mmcblk0p1 | awk '{print $4}'`
  if [ $avaSize -gt $dirSize ] ; then
    if ! [ -e /home/user/MyDocs/wagic/Res/sets/primitives/mtg.txt ]; then
      echo "Copying resources on the FAT partition because no file present on FAT partition"
      cp -rf /home/user/wagic/Res/* /home/user/MyDocs/wagic/Res
    elif [ /home/user/wagic/Res/sets/primitives/mtg.txt -nt /home/user/MyDocs/wagic/Res/sets/primitives/mtg.txt ]; then
      echo "Copying resources on the FAT partition because files present on FAT partition are older"
      cp -rf /home/user/wagic/Res/* /home/user/MyDocs/wagic/Res
    else
      echo "Files already up to date"
    fi
  else
    echo "Not enough space on FAT partition"
  fi
fi

/opt/wagic/wagic
exit 0

