#!/bin/bash
# Modified on 8/7/2006 to wait before searching in /proc/partitions
#          added beep sound after successfull adding entry to fstab
# Modified on 5/30/2007 to only beep when unique ID found
# Modified on 6/27/2007 to use uniquely named temp files based on device
#          exit if in halt or shutdown
#
function cleanup()
{
  rm -rf /tmp/_FSTAB_HMC_
  exit 1
}
function Beep()
{
    times=$1
    while [ $times -gt 0 ]
    do
      echo -en "\007" > /dev/console
      times=$((times-1))
      sleep $times
    done
    #echo -en "\007" > /dev/console
    #sleep 1
    #echo -en "\007" > /dev/console
}
function findID()
{
   /usr/sbin/hwinfo --usb > /tmp/_FSTAB_HMC_/hwinfo.$entry.out 2>&1
   device=$1   

   mydevice=${device%%[0-9]}
   #echo "findID: mydevice is $mydevice" >>/tmp/hmcupdatefstab.log
   found=0
   while read line ; do
     #echo "findID: line is: $line" >>/tmp/hmcupdatefstab.log
     case "$line" in
	 *Unique\ ID:*)
  	    UNIQUEID="${line##*Unique ID: }"
            #echo "findID: uniqueID is $UNIQUEID" >>/tmp/hmcupdatefstab.log
		;;
	 *Device\ File:*)
	    devicefile="${line##*Device File: }"
	    devicefile=`echo $devicefile | cut -d" " -f1`
            #echo "findID: devicefile is $devicefile " >>/tmp/hmcupdatefstab.log
	    if [ "$mydevice" == "$devicefile" ]; then
		   found=1
		   break
	    fi
	    ;;
    esac
  done < /tmp/_FSTAB_HMC_/hwinfo.$entry.out
  rm /tmp/_FSTAB_HMC_/hwinfo.$entry.out
  return $found
}
trap "cleanup" 1 2 3 4 5 11 15
logger -t hmcupdatefstab "Called with $* on `date`"

currlevel=`runlevel  | cut -d' ' -f2`
if [[ "$currlevel" = "6" || "$currlevel" = "0" ]]; then
     logger -t hmcupdatefstab "exit due to runlevel $currlevel"
     exit 0
fi

entry=`basename $1`
if [ "$entry" == "" ]; then
   exit 1
fi

logger -t hmcupdatefstab "entry is >>>$entry<<<"

if [ ! -d /tmp/_FSTAB_HMC_ ]; then
   mkdir /tmp/_FSTAB_HMC_
fi
UNIQUEID=""
option=$2
if [ "$option" == "add" ]; then
    sleep 5
    for i in 1 2 3 4 5 6 7 8 9; do
       cat /proc/partitions | grep -q "$entry$i"
       if [ $? -eq 0 ]; then
          cleanup
       fi
    done
    if [ ! -f /media/$entry ]; then
         mkdir /media/$entry
    fi
    currlevel=`runlevel  | cut -d' ' -f2`
    if [[ "$currlevel" = "6" || "$currlevel" = "0" ]]; then
       logger -t hmcupdatefstab "exit due to runlevel $currlevel"
       exit 0
    fi
    grep -q "^$1" /etc/fstab
    if [ $? -ne 0 ]; then
	findID $1
        if [ $? -eq 1 ]; then
          logger -t hmcupdatefstab "add entry /media/$entry with ID $UNIQUEID"
          echo "$1    /media/$entry  auto     rw,flush,noauto,user,exec,owner 0 0    #HOTPLUG $UNIQUEID" >> /etc/fstab
    	  Beep 3
	else
          logger -t hmcupdatefstab "add entry /media/$entry with unknown ID"
          echo "$1    /media/$entry  auto     rw,flush,noauto,user,exec,owner 0 0    #HOTPLUG id unknown" >> /etc/fstab
	fi
    else
        sed -e "/^\/dev\/$entry/d" /etc/fstab > /tmp/_FSTAB_HMC_/fstab.mod.$entry
	findID $1
        if [ $? -eq 1 ]; then
           echo "$1    /media/$entry  auto     rw,flush,noauto,user,exec,owner 0 0     #HOTPLUG $UNIQUEID" >> /tmp/_FSTAB_HMC_/fstab.mod.$entry
           if [ `filesize /tmp/_FSTAB_HMC_/fstab.mod.$entry` -ne 0 ]; then
              logger -t hmcupdatefstab "replace entry /media/$entry with ID $UNIQUEID"
              mv /tmp/_FSTAB_HMC_/fstab.mod.$entry /etc/fstab           
              Beep 3
           fi
        else
           echo "$1    /media/$entry  auto     rw,flush,noauto,user,exec,owner 0 0     #HOTPLUG id unknown" >> /tmp/_FSTAB_HMC_/fstab.mod.$entry
           if [ `filesize /tmp/_FSTAB_HMC_/fstab.mod.$entry` -ne 0 ]; then
              logger -t hmcupdatefstab "replace entry /media/$entry with ID unknown"
              mv /tmp/_FSTAB_HMC_/fstab.mod.$entry /etc/fstab
           fi
       fi
    fi
fi
currlevel=`runlevel  | cut -d' ' -f2`
if [[ "$currlevel" = "6" || "$currlevel" = "0" ]]; then
     logger -t hmcupdatefstab "exit due to runlevel $currlevel"
     exit 0
fi
if [ "$option" == "remove" ]; then
      umount -l /media/$entry 2>/dev/null
      grep -q "^$1" /etc/fstab
      if [ $? -eq 0 ]; then
        sed -e "/^\/dev\/$entry/d" /etc/fstab > /tmp/_FSTAB_HMC_/fstab.mod.$entry
	if [ `filesize /tmp/_FSTAB_HMC_/fstab.mod.$entry` -ne 0 ]; then
           logger -t hmcupdatefstab "remove entry /media/$entry."
           mv /tmp/_FSTAB_HMC_/fstab.mod.$entry /etc/fstab
	fi
      fi
      if [ -d /media/$entry ]; then
	 rmdir /media/$entry
      fi
fi
rm -rf /tmp/_FSTAB_HMC_
exit 0      
