#!/bin/sh
#env > /tmp/env.$$

# set the mountpoint name according to partition or device name
mount_point=$ID_FS_LABEL
if [ -z $mount_point ]; then
    mount_point=${DEVNAME##*/}
fi

# if a plugdev group exist, retrieve it's gid set & it as owner of mountpoint
plugdev_gid="$(grep plugdev /etc/group|cut -f3 -d:)"
if [ -z $plugdev_gid ]; then
    gid=''
else
    chown root:plugdev /media/$mount_point
    gid=",gid=$plugdev_gid"
fi

# create the mountpoint directory in /media/ (if not empty)
if [ -n $mount_point ]; then
    mkdir -p /media/$mount_point
    # other options (breaks POSIX): noatime,nodiratime,nosuid,nodev
    mount -t $ID_FS_TYPE \
      -o rw \
      $DEVNAME /media/$mount_point
fi

# now handle samba mounts
if [ -f /media/$mount_point/.share ]
then
  SHARENAME=$mount_point
  cat > /etc/samba/include.d/$mount_point.conf <<EOF
[$SHARENAME]
   comment = $ID_TYPE $ID_MODEL $ID_SERIAL_SHORT
   path = /media/$mount_point
   read only = no
   write list = @users
EOF
 
  /etc/samba/mkinclude
  /etc/init.d/samba reload
fi
