#!/bin/sh

##########################################################
#
# mounter - script to handle nfs/smb mounting
#
# Usage:
# 
# ./mounter SERVER SHARE [ USERNAME PASSWORD ]
# 
# To set a new nas point just argue the server 
# > ./mounter 192.168.10.38 /foo/bar
# >
# Adding a share will immediately connect it.
#
# No arguments shows current nfs/smb mounts
# > ./mounter
# > nas0 192.168.10.38 /foo/bar
# > nas1 192.168.10.39 /share/media/current
# 
# A nasX may be argued for it's mount point 
# > ./mounter nas0
# > nas0 foo /bar/share
#
# Removing a share
# > ./mounter nas0 remove
# or 
# > ./mounter nas0 r
#  
# Shares may be disconnected and reconnected, but usage
# of connect and disconnect is very alpha.
# > ./mounter nas
#########################################################

shopt -s -o nounset

declare -r FSTAB=/etc/fstab
declare -rx SCRIPT=${0##*/}
declare -r MOUNT_DIR="/media"
#declare -r APP_DIR="/var/media"
declare -r APP_DIR=${MEDIAPATH%%[:|,]*}   # strip left to either the comma or colona
declare -r USER_HOME="/home/adtec"
declare NAS="NAS"
DEBUG=N
#echo $MEDIAPATH
#exit
declare NEXT_NAS

function debug ()
{
   if [ "$DEBUG" == "Y" ]; then
      echo "$1"
   fi
}



#
#   Called with:   
#   NASx foo /bar [ user pass ]
#
function do_mount () 
{
   declare NFS_REMOTE_MNT="${2#//*}:/${3#/*}"  # just one way to ensure leading slash, strip it and then add.
   declare SMB_REMOTE_MNT="//${2#//*}/${3#/*}"  # just one way to ensure leading slash, strip it and then add.
   #echo $NFS_REMOTE_MNT, $SMB_REMOTE_MNT
   declare LOCAL_MNT_PATH="${MOUNT_DIR}/${1}"
   debug $LOCAL_MNT_PATH
   if [ ! -e "${LOCAL_MNT_PATH}" ]; then
     chmod 644 $MOUNT_DIR
     mkdir -p "${LOCAL_MNT_PATH}" #&>/dev/null  # make the mount point
     chmod 444 $MOUNT_DIR
   fi

   if [ $# -eq 3 ]; then  # try nfs
      mount $NFS_REMOTE_MNT $LOCAL_MNT_PATH -t nfs -o nolock
      if [ $? -eq 0 ]; then 
         declare ENTRY=$(printf "%s %s nfs nolock 0 0" "$NFS_REMOTE_MNT" "$LOCAL_MNT_PATH")   
      else # try cifs
         mount $SMB_REMOTE_MNT $LOCAL_MNT_PATH -t cifs -o guest
         if [ $? -eq 0 ]; then
            declare ENTRY=$(printf "%s %s cifs guest 0 0" "$SMB_REMOTE_MNT" "$LOCAL_MNT_PATH")   
         else
            exit 2
         fi
      fi
   else 
      mount $SMB_REMOTE_MNT $LOCAL_MNT_PATH -t cifs -o username=${4},password=${5}
      if [ $? -eq 0 ]; then
         declare ENTRY=$(printf "%s %s cifs username=%s,password=%s 0 0" "$SMB_REMOTE_MNT" "$LOCAL_MNT_PATH" "$4" "$5")   
      else
         exit 2
      fi
   fi     
      
   #echo $ENTRY

   declare LAST=`sed -n '$p' $FSTAB`  #test if newline at end of fstab
   if [ ${#LAST} -ne 0 ]; then
      ENTRY="\n${ENTRY}"
   fi
   echo -e "${ENTRY}" >> $FSTAB

   #if [ $? -eq 0 ]; then  # make some links
   #for HOMEDIR in `find /home/ -maxdepth 1 -mindepth 1 -type d`; do
   #rm -f "${MOUNT_DIR}/${1}"
   #ln -sf "${LOCAL_MNT_PATH}" "${HOMEDIR}/${1}" 
   #    debug "ln -sf ${LOCAL_MNT_PATH} ${USER_HOME}/${1}"
   #done
   #ln -sf "${LOCAL_MNT_PATH}" "${APP_DIR}/${1}" &>/dev/null
   #debug "ln -sf ${LOCAL_MNT_PATH} ${APP_DIR}/${1}"
   #else
      # mount failed, what to do?  delete fstab entry?  
   #   exit 5
   #fi
}    

rm_home_link ()
{
   for HOMEDIR in `find /home/ -maxdepth 1 -mindepth 1 -type d`; do
       rm -f "${HOMEDIR}/${1}"
   done
}

find_free_nas ()
{
   local CNT=0
   local EXIST
   while true; do
      EXIST=`grep -c ${NAS}${CNT} ${FSTAB}`
      if [ "$EXIST" -eq 0 ]; then # no match
         NEXT_NAS=${NAS}${CNT}
         break
      else
         let CNT=$CNT+1
      fi
   done
}

usage ()
{
echo
echo "mounter - script to handle nfs/smb mounting"
echo ""
echo "Usage:"
echo " "
echo "   ./mounter SERVER SHARE [ USERNAME PASSWORD ]"
echo " "
echo "   To set a new nas point just argue the server "
echo "   > ./mounter 192.168.10.38 /foo/bar"
echo "   >"
echo "   Adding a share will immediately connect it."
echo 
echo "   No arguments shows current nfs/smb mounts"
echo "   > ./mounter"
echo "   > nas0 192.168.10.38 /foo/bar"
echo "   > nas1 192.168.10.39 /share/media/current"
echo ""
echo "   A nasX may be argued for it's mount point "
echo "   > ./mounter nas0"
echo "   > nas0 foo /bar/share"
echo
echo "   Removing a share"
echo "   > ./mounter nas0 remove"
echo "   or "
echo "   > ./mounter nas0 r"
echo " "
echo "   Shares may be disconnected and reconnected, but usage"
echo "   of connect and disconnect is very alpha."
echo "   > ./mounter nas"
echo

}

# main routine

if [ ! -e $FSTAB ]; then 
   touch $FSTAB
fi

case $# in 

   0)
      #show mounts
      sed '/NAS/s/^\([^ \t]*\) \/media\/\([^ \t]*\).*$/\2 \1/' $FSTAB
      #cat $FSTAB | cut -f1-3 -d" "        
   ;;
   1)
      case $1 in
      NAS*)
         # should be a NASx, show that mount
         sed '/NAS/s/^\([^ \t]*\) \/media\/\([^ \t]*\).*$/\2 \1/' $FSTAB
         #grep $1 $FSTAB | cut -f1-3 -d" " 
         
      ;;
      help)
         usage
      ;;  
      *)
         exit 2
      esac
   ;;
   2|4) 
      case $2 in 
         "connect"|"c")
            mount ${MOUNT_DIR}/${1}
            debug "mount ${MOUNT_DIR}/${1}"
         ;;
         "disconnect"|"d")
            umount -f ${MOUNT_DIR}/${1} &>/dev/null
            debug "umount -f ${MOUNT_DIR}/${1}"
         ;;
         "remove"|"r")
            $0 $1 d   # disconnect
            sed -i "/${1}/d" $FSTAB        
            debug "sed -i /${1}/d $FSTAB"        
			   #for HOMEDIR in `find /home/ -maxdepth 1 -mindepth 1 -type d`; do
			   #    rm -f "${HOMEDIR}/${1}"
			   #done
			   chmod 644 $MOUNT_DIR
            rm -rf ${MOUNT_DIR}/${1}
            #debug "rm -rf ${APP_DIR}/${1}"
         ;;
         *)
         find_free_nas
         do_mount $NEXT_NAS $*
      esac
   ;;
   *)
      exit 2  # wrong number of args
esac
