#!/bin/bash
#ver 0.34
#*** DEVICEFILE FORMAT ***
#DEVICE:LABEL
#Changelog
#v0.01 fixed LABELing
#v0.05 fixed device deletion from DEVICEFILE when a non-existent device was called to be Removed
#v0.10 fixed positioning
#v0.20 Added Restore functionality to clear ROX pinboard from old entries
#v0.25 Lockfile mechanism
#v0.30 Remove some hardcoded paths
#v0.31 Fix some grep commands
#v0.32 More fixes
#v0.33 new DEVICEFILE format
#v0.34 changed $HOME variable definition (http://www.void.gr/kargig/blog/2006/07/24/create-icons-on-rox-desktop-of-automounted-media-by-ivman/#comment-150305)
#TODO
# NEEDS CLEANING!!!
# D.M.: Use various icons for different labels, or better still, use
# device-local icons. Unfortunately my tries below don't work,
# obviously I miss something important'
#VARIABLES - REPLACE WITH YOUR OWN
[ -z "$HOME" ] && HOME="/home/`whoami`"
WHO=`whoami`
DEVICEFILE="$HOME/.ivman/mounted/devices"
DEVICEFILETEMP="$HOME/.ivman/mounted/devices_TEMP"
ROXFILE="$HOME/.config/rox.sourceforge.net/ROX-Filer/pb_Default"
ROXTEMP="$HOME/.config/rox.sourceforge.net/ROX-Filer/pb_pinboard_TEMP"
LOCKFILE="/tmp/rox-ivman-lock-$WHO.tmp"
# D.M.: adapt to different screen sizes and offsets from top or bottom
XLOCATION=-100
YLOCATION=50
XSEPARATION=70
YSEPARATION=70
XOFFSET=0
YOFFSET=2
#DON'T TOUCH BELOW IF YOU DON'T KNOW WHAT YOU ARE DOING!
scriptname=$0
if [ ! -d "$HOME" ]; then
exit
fi
if [ ! -f "$DEVICEFILE" ]; then
touch $DEVICEFILE
fi
if [ "$3" != "nowait" ]; then
timer=0
while [ "$timer" -lt 10 ]; do
if [ -e "$LOCKFILE" ]; then
sleep 1
echo "sleeping for 1 sec"
else
touch $LOCKFILE
break
fi
timer=$((timer + 1))
done
fi
ScreenWidth=$(xwininfo -root | awk -F: '/Width:/{print $2}')
ScreenHeight=$(xwininfo -root | awk -F: '/Height:/{print $2}')
if [ $XLOCATION -lt 0 ]; then
THEXI=$((ScreenWidth + $XLOCATION))
elif [ $XLOCATION -gt $ScreenWidth ]; then
THEXI=`echo "$ScreenWidth * $ScreenWidth / $XLOCATION" | bc -l`
else
THEXI=$XLOCATION
fi
NUMLINES=$(wc -l $DEVICEFILE | cut -d " " -f1)
echo "NUMLINES = $NUMLINES"
if [ "$1" == "Add" ]; then
echo "We are adding a device"
DEVICE=$(grep "$2" $DEVICEFILE)
if [ "$DEVICE" == "$2" ]; then #IF device already exists in DEVICEFILE -> exit
#maybe raise an xdialog ???
echo "Device is already mounted...exiting"
exit
else #IF device does not exist in DEVICEFILE
if [ "A$3" == "A" ]; then #IF the device has no label set the path as label
LABEL=$2
THEPATH=$2
else
LABEL=$3
THEPATH=$2
fi
if [ -x "$THEPATH/.DirIcon" ]; then
ICONFILE="$THEPATH/.DirIcon"
fi
DEVICECOUNT=$(grep -v "^$" $DEVICEFILE | wc -l | cut -d ":" -f1)
#DEVICECOUNT=$(grep -v "^$" $DEVICEFILE -n -m1 | awk '{ print substr($0, 1, 1) }')
echo "DEVICECOUNT = $DEVICECOUNT"
if [ "A$DEVICECOUNT" == "A" ]; then #IF no devices are present in DEVICEFILE
DEVICECOUNT=0
fi
# D.M.: THEPSI still starts at top, but allows for an Offset
if [ $DEVICECOUNT -lt $NUMLINES ]; then #IF number of devices < number of lines (there are empty spots)
spot=$(grep "^$" $DEVICEFILE -n -m 1 | awk '{ print substr($0, 1, 1) }')
echo "spot = $spot"
THEPSI=`echo "$((spot-1+YOFFSET)) * $YSEPARATION + $YLOCATION" | bc -l`
echo "THEPSI = $THEPSI"
head -n $((spot-1)) $DEVICEFILE >> $DEVICEFILETEMP
echo "$2:$3" >> $DEVICEFILETEMP
tail -n $((NUMLINES - spot)) $DEVICEFILE >> $DEVICEFILETEMP
mv $DEVICEFILETEMP $DEVICEFILE
else #IF number of devices = number of lines
echo "Numlines = devices, adding below the last one"
THEPSI=`echo "($((DEVICECOUNT + YOFFSET))) * $YSEPARATION + $YLOCATION" | bc -l`
echo "$2:$3" >> $DEVICEFILE
fi
fi
if [ $THEPSI -gt $ScreenHeight ]; then
THEPSI=`echo "$ScreenHeight * $ScreenHeight / $THEPSI" | bc -l`
elif [ $THEPSI -lt 0 ]; then
THEPSI=$((ScreenHeight + THEPSI))
fi
echo "$THEXI $THEPSI $ScreenWidth x $ScreenHeight"
#else #if removing a device
elif [ "$1" == "Remove" ]; then #if removing a device
DEVICE=$(grep "$2" $DEVICEFILE)
THELABELTOREMOVE=`grep "$2" $DEVICEFILE | cut -d":" -f2-`
THEPATHTOREMOVE=`grep "$2" $DEVICEFILE | cut -d":" -f1`
if [ "A$THELABELTOREMOVE" == "A" ]; then #If the device has no label
LABEL=$THEPATHTOREMOVE
else
LABEL=$THELABELTOREMOVE
fi
THEPATH=$THEPATHTOREMOVE
if [ "A$THEPATH" == "A" ]; then #If no such path exists in the DEVICEFILE
echo "Device with that path does not exist...exiting"
exit
fi
elif [ "$1" == "Restore" ]; then
echo "Restoring"
lastline=$(tail -n1 $ROXFILE)
if [ "$lastline" == "" ]; then
echo "Restore not needed. Pinboard is empty"
exit
fi
DEVCOUNT=$(grep -v "^$" $DEVICEFILE | wc -l | cut -d ":" -f1) #how many lines in DEVICEFILE are not empty (how many devices are leftover)
echo "DEVCOUNT = $DEVCOUNT"
count=0
while [ $count -lt $DEVCOUNT ]; do
NUMLINESROX=$(wc -l $ROXFILE | cut -d " " -f1) #how many lines rox pinboard file has
echo "NUMLINESROX = $NUMLINESROX"
dev=$(grep -v "^$" $DEVICEFILE -n -m1 | tr ":" " " | awk '{ print $2 }')
echo "dev = $dev"
spotrox=$(grep -n "$dev" $ROXFILE | awk '{ print substr($0, 1, 1) }')
if [ "A$spotrox" == "A" ]; then
spotrox=0
echo "spotrox = $spotrox"
else
head -n $((spotrox-1)) $ROXFILE > $ROXTEMP
tail -n $((NUMLINESROX-spotrox)) $ROXFILE >> $ROXTEMP
mv $ROXTEMP $ROXFILE
$scriptname Remove $dev nowait
fi
count=$((count + 1))
done
if [ "$NUMLINESROX" == "3" ]; then
echo "" > $ROXFILE
echo "" >> $ROXFILE
fi
fi
if [ "$1" == "Add" ] || [ "$1" == "Remove" ]; then #process only if Adding or Removing a device
APP_DIR=`dirname "$0"`
export APP_DIR
rox --RPC << EOF
$THEPATH$THEXI$THEPSI$ICONFILE
EOF
fi
if [ "$1" == "Remove" ]; then #remove entry from DEVICEFILE
spotr=$(grep -n "$THEPATH" $DEVICEFILE | awk '{ print substr($0, 1, 1) }')
head -n $((spotr - 1)) $DEVICEFILE >> $DEVICEFILETEMP
echo >> $DEVICEFILETEMP
tail -n $((NUMLINES-spotr)) $DEVICEFILE >> $DEVICEFILETEMP
mv $DEVICEFILETEMP $DEVICEFILE
fi
rm -f $LOCKFILE