#! /bin/sh
### BEGIN INIT INFO
# Provides:          mount_data
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Mount or unmount disk with label data
# Description:       Used to mount or unmount a external USB disk
#                    labeled data, to be used as init.d script
#                    Mounting also triggeres a file system check e2fsck
#                    Recomm: force e2fsck evenry time by tune2fs -c 1 /dev/sda1
### END INIT INFO

# to be part of my auto-mount in init.d
# runlevel 2-5 (2=default, 0=halt, 6=shutdown)
# install using update-rc.d mount_data defaults 91

PATH=/sbin:/usr/sbin:/bin:/usr/bin

do_start() {
    if [ -L /dev/disk/by-label/data ]
    then
      if mount | grep "/mnt/data" >/dev/null 2>&1
      then
        echo "already mounted" >/dev/null 2>&1
      else
        e2fsck -p /dev/disk/by-label/data
        mount /dev/disk/by-label/data
        beep -l 50 -f 20 -n -l 100 -f 40
      fi
    fi
}

# unmount data drive if mounted (and indicate by sound)
do_stop() {
    if mount | grep "/mnt/data" >/dev/null 2>&1
    then
      sync
      beep -l 50 -f 40 -n -l 50 -f 20
      THISDRIVE=`mount | grep "/mnt/data" | awk '/sd/{print $1}'`
#     echo "Halting data disk $THISDRIVE"
      if [ -n $THISDRIVE ]
      then
        umount $THISDRIVE >/dev/null 2>&1
      fi
    fi
}

case "$1" in
  start)
    do_start
	;;
  stop)
    do_stop
	;;
  restart|force-reload)
    do_stop
    do_start
	;;
  *)
	echo "Usage: mount_data {start|stop|restart|force-reload}" >&2
	exit 3
	;;
esac

# end of script

