#!/bin/sh

DIR=`dirname $0`
STATION=`cat /opt/pos/etc/config.ini | grep "STATION_NUMBER" | cut -f2 -d=`
SAVED_HARDLOCK=`ls /opt/pos/etc/.hardlock.current 2> /dev/null | head -1 | cut -f1 -d.`

#
# this parameter is used to format all the inserted pens and
# nit them as empty hardlocks
# pay attention to this because it will init ALL the inserted pens in
# the machine
#

INIT_HARDLOCK=$1

echo 1 > /tmp/.stop_mount.txt

#udevadm control --stop-exec-queue

valid=0

if [ "$INIT_HARDLOCK" = "" ]; then
  if [ "$SAVED_HARDLOCK" = "" ]; then
    echo "Didn't find any saved hardlock in /opt/pos/etc/.hardlock.current, aborting..."
    exit
  fi
fi

for dev in `ls /dev/sd[bcdefghijkl] 2> /dev/null`; do
  echo "device: $dev"

  hardlock_id=`/opt/pos/common/bin/device_id ${dev} 1`
  partition_size=0

  if [ "$hardlock_id" != "" ]; then
    partition_size=`fdisk -l $dev | tail -1 | awk '{ print $5 }'`
  fi

  echo "device id: $hardlock_id ($partition_size)"

  if [ "$hardlock_id" = "" ]; then
    echo "couldn't read device id... skipping..."
    continue
  fi

  if [ "$INIT_HARDLOCK" = "" ]; then
    # makes sure we don't format the wrong pen
    if [ "$hardlock_id" != "$SAVED_HARDLOCK" ]; then
      echo "Saved hardlock $SAVED_HARDLOCK is different than $hardlock_id, skipping..."
      continue
    fi
  else

    if [ "$partition_size" = "1.9G" ]; then
      ans="y"
    else
      echo "format? [y/n]"
      read ans
    fi

    if [ "$ans" != "y" ]; then
      echo "skipping..."
      continue
    fi
  fi

  IS_MOUNTED=`mount | grep $dev`

  for mnt in `mount | grep $dev | awk '{ print $1 }'`; do
    umount $mnt > /dev/null 2>&1
  done

  if [ "$IS_MOUNTED" != "" ]; then
    umount "${dev}" > /dev/null 2>&1
    umount "${dev}1" > /dev/null 2>&1
    sleep 1
  else
    device=`echo $dev | cut -f3 -d/`

    if [ -d "/media/usbhd-${device}1" ]; then
      umount "${dev}1" > /dev/null 2>&1
      rm -rf /media/usbhd-${device}1
    fi
  fi

  wipefs -a ${dev}

  #echo ,,8e | sfdisk ${dev} > /dev/null 2>&1
  echo ,,8e | sfdisk --force ${dev}

#  sfdisk $dev > /dev/null 2>&1 <<EOF
#0
#,
#;
#;
#EOF

  echo "### sfdisk result: $?"

  if [ $? -eq 0 ]; then
    echo "*** sfdisk success on $dev"
    sleep 1

    valid=1
    sfdisk --change-id $dev 1 c > /dev/null 2>&1
    #sfdisk --change-id $dev 1 c
    echo "*** create partition result: $?"
    sleep 1
    umount ${dev}1 2> /dev/null
    #mkfs.vfat -n POS "${dev}1" > /dev/null 2>&1
    mkfs.vfat -n POS "${dev}1"
    echo "*** vfat file system creation result: $?"
    sync

    if [ -d "/media/pen.$$" ]; then
      rm -rf /media/pen.$$
    fi

    mkdir /media/pen.$$
    mount ${dev}1 /media/pen.$$

    if [ $? -eq 0 ]; then
      sleep 2

      hardlock_id=`/opt/pos/common/bin/device_id /media/pen.$$`
      echo $hardlock_id > /media/pen.$$/hardlock.txt

      mkdir /media/pen.$$/pos
      
      if [ "$INIT_HARDLOCK" = "" ]; then
        if [ "$hardlock_id" = "$SAVED_HARDLOCK" ]; then
          echo "found a backup of the hardlock file... will copy it to ${dev}1"

          if [ -f "/opt/pos/etc/.hardlock.current/$SAVED_HARDLOCK.hardlock" ]; then
            cp /opt/pos/etc/.hardlock.current/$SAVED_HARDLOCK.hardlock /media/pen.$$/pos/hardlock
          fi

          if [ -f "/opt/pos/etc/.hardlock.current/$SAVED_HARDLOCK.lic_validation.ini" ]; then
            cp /opt/pos/etc/.hardlock.current/$SAVED_HARDLOCK.lic_validation.ini /media/pen.$$/pos/lic_validation.ini
          fi
        fi
      fi

      sync

      sleep 1
      umount ${dev}1 2> /dev/null
      rm -rf /media/pen.$$ 2> /dev/null

      #device=`echo $dev | cut -f3 -d/`

      #if [ ! -d "/media/usbhd-${device}1" ]; then
      #  mkdir /media/usbhd-${device}1
      #fi

      #mount /dev/${device}1 /media/usbhd-${device}1
    fi
  else
    echo "Error running sfdisk..."
  fi
done

#udevadm control --start-exec-queue
#udevadm control --reload-rules && udevadm trigger

if [ $valid -eq 0 ]; then
  echo "!! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!"
  echo "!! No hardlock found or hardlock is damaged... !!"
  echo "!! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!"
else
  echo "Success. Unplug and plug the PEN again..."
fi

if [ -d "/media/pen.$$" ]; then
  rm -rf /media/pen.$$
fi

rm -f /tmp/.stop_mount.txt

