#!/bin/sh
# usbmount mount.d script to create partition label symlink in
# /var/run/usbmount/by-label/<label> pointing to respective mounth path
#
# Copyright (C) 2008 Heikki Hokkanen <hoxu@users.sf.net>
#
# License: BSD-2
#   This package is free software; the copyright holder gives unlimited
#   permission to copy and/or distribute it, with or without
#   modifications, as long as this notice is preserved.
# 
#   This package is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY, to the extent permitted by law; without
#   even the implied warranty of MERCHANTABILITY or FITNESS FOR A
#   PARTICULAR PURPOSE.
#
set -e

linkdir="/var/run/usbmount/by-label"

# non-existent dir -> exit
mkdir -p "$linkdir" || exit 0

# vol_id returns a "safe version of volume label suitable for use as filename"
label=`vol_id --label ${UM_DEVICE}`

# empty label -> exit
test -n "$label" || exit 0

# only allow some whitelisted characters in label, replace rest with '_'
label=`echo "${label}"|sed 's/[^A-Za-z0-9]/_/g'`

symlink="$linkdir/$label"
[ -h "$symlink" ] && rm -f "$symlink"
ln -sf "$UM_MOUNTPOINT" "$symlink"
