#!/bin/sh
# ====================[ pmount.sh                          ]====================
#                     [ Time-stamp: "2009-04-18 19:29:36 leycec" ]
#
# --------------------( SYNOPSIS                           )--------------------
# This script leverages "pmount," an external userspace utility for mounting
# volumes via and for non-root users rather than root, to allow automounting of
# volumes from udev. udev calls this script on connection of mass storage
# devices from within the "/etc/udev/rules.d/65-persistent-storage-pmount.rules"
# file. 
#
# --------------------( SYNOPSIS                           )--------------------
# This script has read-only access to all udev environment variables available
# at the time the above ruleset is read. These environment variables conform
# to mass storage device properties, as echoed by the following command:
#
#   # Echo all udev properties for the mass storage device at "/dev/sda".
#   udevinfo -a -p $(udevinfo -q path -n /dev/sda)
#
# --------------------( SEE ALSO                           )--------------------
# * "UDEV." Gentoo Wiki. (New).
#   http://en.gentoo-wiki.com/wiki/Autofs#UDEV
# * "UDEV." Gentoo Wiki. (Old).
#   http://www.gentoo-wiki.info/UDEV

# ....................{ UDEV                               }....................
# To automount only USB devices, add:
#    -a "$ID_BUS" = "usb"
#logger "pmount: automounting..."
[ "$ACTION" = "add" -a "$DEVTYPE" = "partition" ] || exit 0

# Mount devices by human-readable label.
DEVICE="/dev/disk/by-label/$ID_FS_LABEL_ENC"

# Do not mount if already mounted.
grep -q "^$DEVICE"  /etc/mtab  && exit 0
grep -q "^$DEVICE"  /etc/fstab && exit 0
grep -q "^$DEVNAME" /etc/fstab && exit 0
grep -q "^label:$ID_FS_LABEL_ENC" /etc/fstab && exit 0

# Derive a human-readable label from this device, replacing whitespace with
# underscores if necessary.
LABEL=$(echo "$ID_FS_LABEL_ENC" | sed 's/ /_/g')
if [ -z "$LABEL" ]; then
  LABEL="generic_volume"
fi

# ....................{ PMOUNT                             }....................
LOG_PREFIX="pmount: $DEVICE"
LOG_MESSAGE="$LOG_PREFIX to \\media\\$LABEL..."
logger $LOG_MESSAGE

# Supply the following mount options by default, regardless of filesystem type:
#
# * --umask 007. Do not allow other users to read or write this file system.
# * --noatime.   Do not update inode access times on this file system.
#
# For a full list of all generic mount options, see "man pmount".
PMOUNT_OPT="--umask 007 --noatime"
PMOUNT_OUTPUT=$(pmount $PMOUNT_OPT "$DEVICE" "$LABEL" 2>&1)

if [ $? -eq 0 ]; then
  logger "${LOG_MESSAGE}done"
else
  logger "${LOG_MESSAGE}failed! (${PMOUNT_OUTPUT})"
fi

exit 0

# ....................{ HAL =obsolete                      }....................
# Mount devices to unused mount directories.
#MNTDIR="$LABEL"
#MNTDIR_NUM=0
#while grep -q " $MNTDIR " /etc/mtab; do
#   MNTDIR_NUM=$(expr $MNTDIR_NUM + 1)
#   MNTDIR="$LABEL-$MNTDIR_NUM"
#done

#case $ID_FS_TYPE in
#  ext3) ;;
#esac

#DEVICE="$1"
#GID=`grep plugdev /etc/group | cut -d: -f 3`
#
#if [ "$DEVICE" = "" ] ; then exit 1 ; fi
#
## wait a moment till' hal has information about the device
#sleep 2
#
#HAL_UDI=`hal-find-by-property --key block.device --string "$DEVICE"`
#
#function get_hal_label {
#    CUR_UDI=$1
#    LABEL=""
#    COUNTER=0;
#    while [ -z "$LABEL" -a $COUNTER -lt 4 ]; do
#	LABEL=`hal-get-property --key volume.label --udi "$CUR_UDI" 2>/dev/null`
#	if [ -z "$LABEL" ]; then
#	    LABEL=`hal-get-property --key storage.serial --udi "$CUR_UDI" 2>/dev/null`
#	fi
#	CUR_UDI=`hal-get-property --key info.parent --udi "$CUR_UDI" 2>/dev/null`
#	let COUNTER=COUNTER+1
#    done
#    
#    if [ -z $LABEL ]; then
#	LABEL=${DEVICE##/dev/}
#    fi
#}
#
#function get_hal_removable {
#    CUR_UDI=$1
#    REMOVABLE=""
#    COUNTER=0
#    while [ -z "$REMOVABLE" -a $COUNTER -lt 4 ]; do
#	REMOVABLE=`hal-get-property --key storage.removable --udi "$CUR_UDI" 2>/dev/null`
#	if [ -z "$REMOVABLE" ]; then
#	    BUS=`hal-get-property --key storage.bus --udi "$CUR_UDI" 2>/dev/null`
#	    if [ "$BUS" = "usb" ]; then
#		REMOVABLE="true"
#	    fi
#	fi
#	CUR_UDI=`hal-get-property --key info.parent --udi "$CUR_UDI" 2>/dev/null`
#	let COUNTER=COUNTER+1
#    done
#    
#    if [ -z "$REMOVABLE" ]; then
#	logger "assuming $HAL_UDI is removable for safety reasons"
#	REMOVABLE="true"
#    fi
#}
#
#if [ -z "$HAL_UDI" ]; then # can't find device in hal db
#    logger "hald didn't know about $DEVICE"
#    LABEL=${DEVICE##/dev/}
#else # device found in hal db.
#    get_hal_label "$HAL_UDI"
#    get_hal_removable "$HAL_UDI"
#fi

# --------------------( COPYRIGHT AND LICENSE              )--------------------
# The information below applies to everything in this distribution,
# except where noted.
#              
# Copyleft 2009 by B.w.Curry.
#   
#   http://www.raiazome.com
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
