#!/bin/sh
# Script/function to add desktop icons for all partitions of a certain device
# device name (without "/dev/") passed as argument
## goes from TOP RIGHT down (and left)
# part of the "hotpup" daemon scripts
## yet another dubious Dougal script for Puppy Linux, April 2007
## now adding icons to globicons...
## Latest update: August 27th


# (possible) TODO: DONE
# check if cdrom or dvd: dvd+rw-mediainfo /dev/$ADEV
# check cd type (cdrominfo): "Audio" for audio-cd icon, "Data" for normal
## TODO: what about audio-cds? they can't be mounted!

[ "$1" ] || exit
ADEV="$1"

# dir we're in (for knowing where icons are)
APPDIR="`dirname $0`"
[ "$APPDIR" = "." ] && APPDIR="`pwd`"
# files used
PINFILE=/root/Choices/ROX-Filer/PuppyPin
#PINFILE=$HOME/.config/rox.sourceforge.net/ROX-Filer/pb_Default
FSTAB=/etc/fstab

# icons for different media (for adding to globicons
CDICON=$APPDIR/icons/gnome-dev-cdrom.png
#AUDIOICON=$APPDIR/icons/gnome-dev-cdrom-audio.png
DVDICON=$APPDIR/icons/gnome-dev-dvd.png
HDICON=$APPDIR/icons/gnome-dev-harddisk.png
FLOPPYICON=$APPDIR/icons/gnome-dev-floppy.png
USBICON=$APPDIR/icons/usbpendrive48.png
ZIPICON=$APPDIR/icons/gnome-dev-zipdisk.png
CFICON=$APPDIR/icons/gnome-dev-media-cf.png

## code from fixpuppypin, to get the extreme coordinates (to match screen size)
if [ "`readlink /usr/X11R7/bin/X`" = "Xorg" ];then
 SCREEN=`xrandr | grep -F '*' | tr -s " " | cut -d' ' -f2,4`
 SCREENX=${SCREEN% *} ; SCREENY=${SCREEN#* }
else
 if [ -f /etc/videomode ];then #used by Xvesa.
  SCREEN=`cat /etc/videomode | cut -f 2 -d " " | cut -f1,2 -d "x"`
  SCREENX=${SCREEN%x*} ; SCREENY=${SCREEN#*x}
 fi
fi
# make sure we got something...
[ "$SCREENX" ] && [ "$SCREENY" ] || exit

## params used:
Y0=32 ; let X0=SCREENX-96 ; XSPACE=64 ; YSPACE=64 ; let MAXY=SCREENY-64
MYX="$X0" ; MYY="$Y0"
# list of all mountpoints in fstab matching ADEV (but not those commented out)
MNTPNTS="`grep -F "/dev/$ADEV" $FSTAB | grep -F -v '#' | tr '\t' ' ' | tr -s ' '| cut -d' ' -f2`"
# list of all mountpoints currently linked on desktop
#CRRNTLINKS="`grep -E '>/mnt/[a-z]?[[:alnum:]]+<' $PINFILE | cut -d'>' -f2 | cut -d'<' -f1 | tr '\n' ' '` " # note space at end
# coordinates of all current icons in bottom part of screen
# (could remove the [][][] and check all, but don't expect to get very high)
## Add X>1000 and exclude X=1xx
PINICONS="`grep -F 'icon' $PINFILE | cut -d'<' -f2 | cut -d'"' -f2,4| tr '"' ' ' | grep '^[14-9][0-9][0-9]' | grep -v '^1[0-9][0-9] ' | sort -n -t ' '`"

## SOAP code for icon addition:
ADDHEAD='<?xml version="1.0"?>
<env:Envelope xmlns:env="http://www.w3.org/2001/12/soap-envelope">
 <env:Body xmlns="http://rox.sourceforge.net/SOAP/ROX-Filer">
  <PinboardAdd>'

ADDTAIL='  </PinboardAdd>
 </env:Body>
</env:Envelope>'

ICONHEAD='<?xml version="1.0"?>
<env:Envelope xmlns:env="http://www.w3.org/2001/12/soap-envelope">
 <env:Body xmlns="http://rox.sourceforge.net/SOAP/ROX-Filer">
  <SetIcon>'

ICONTAIL='  </SetIcon>
 </env:Body>
</env:Envelope>'

########################### define functions ###########################
##  function finds coordinates of unused spot on desktop, starting from 
##+ MYX MYY (either base (above) or last one we added+spacing) and moving right
##+ (when we reach the right end of the screen -- move up and back left end) 
##  I use two loops: the inner goes through existing icons and if we bump
##+ into one and change our coordinates -- restart it with "continue 2"
setcoordinates(){
while true
do
	while read X Y
	do
		[ -z "$X" -o -z "$Y" ] && continue
		[ $MYY -gt $MAXY ] && MYY="$Y0" && let MYX=MYX-XSPACE && continue 2
		let DIFFX=MYX-X
		[ $DIFFX -lt 0 ] && let DIFFX=0-DIFFX
		let DIFFY=MYY-Y
		[ $DIFFY -lt 0 ] && let DIFFY=0-DIFFY
		if [ $DIFFX -lt $XSPACE -a $DIFFY -lt $YSPACE ] ; then
			# we're on top of an existing icon -- move and start over
			let MYY=MYY+YSPACE 
			continue 2 # need to start inner loop over again
		fi
	done <<_BLA
	$(echo "$PINICONS")
_BLA
	break # this happens when the inner loop has completed (=got coordinate)
done
} #setcoordinates

# function to check if cdrom in drive is audio or data (or none)
# device (without /dev/) passed as parameter
#check_audio_cd(){
#if [ "`/usr/lib/mut/bin/cdrominfo /dev/$1 -p | cut -d'|' -f8`" = "Audio" ]; then
#return 0
#else
#return 1
#fi
#}

# function to check dvd drive for type of contents: audio cd, cd, dvd
# sets the MYICON param
check_dvd_drive(){
if dvd+rw-mediainfo /dev/$1 >/dev/null 2>&1 ; then MYICON="$DVDICON"
#elif check_audio_cd $1 ; then MYICON="$AUDIOICON" 
else MYICON="$CDICON" 
fi 
}

# function to add globicons entry for each partition
# params passed: mountpoint, icon ($MYICON)
add_glob_icon(){
echo -e "$ICONHEAD\n   <Path>$1</Path>\n   <Icon>$2</Icon>\n$ICONTAIL" | rox -R
}

# function that adds links to pinboard
addtopinboard(){ # params: path, name, x, y
echo -e "$ADDHEAD\n   <Path>$1</Path>\n   <X>$3</X>\n   <Y>$4</Y>\n   <Label>$2</Label>\n$ADDTAIL" | rox -R
}
########################################################################

## go through existing mountpoint links from pinboard, remove if needed
#for ALINK in $CRRNTLINKS
#do
  #case "$MNTPNTS" in *"$ALINK"*) continue ;; esac # no need to remove
  ## if we got this far -- need to remove
  #echo -e "$REMOVEHEAD\n   <Path>$ALINK</Path>\n$REMOVETAIL" | rox -R
#done

## find what kind of device this is, set MYICON to the right one
case "$ADEV" in
 cdrom) #if check_audio_cd $ADEV ; then MYICON="$AUDIOICON" 
        #else 
        MYICON="$CDICON" 
        #fi
        ;;
 dvd) check_dvd_drive $ADEV
      ;;
 floppy|fd0) MYICON="$FLOPPYICON" ;;
 hd*) # ide device
   case "`cat /proc/ide/$ADEV/media`" in
    cdrom) # ide cdrom
      case "`cat /proc/ide/$ADEV/model`" in
        *[Dd][Vv][Dd]*) check_dvd_drive $ADEV ;;
        *) #if check_audio_cd $ADEV ; then MYICON="$AUDIOICON" 
           #else 
           MYICON="$CDICON" 
           #fi 
           ;;
      esac
      ;;
    disk) MYICON="$HDICON" ;;
    floppy) MYICON="$ZIPICON" ;;
   esac
   ;;
 sr*) # usb cdrom
   case "`cat /sys/block/$ADEV/device/model`" in
     *[Dd][Vv][Dd]*) check_dvd_drive $ADEV ;;
     *) #if check_audio_cd $ADEV ; then MYICON="$AUDIOICON" 
        #else 
        MYICON="$CDICON" 
        #fi 
        ;;
   esac
   ;;
 sd*) # scsi drives
   # first see if SATA/SCSI HD
   if [ "`cat /sys/block/$ADEV/removable`" = "0" ] ; then # normal hd, or usb
     MYICON="$HDICON"
   else #removable device
     MYICON="$USBICON" # lets assume it's a pendrive...
   fi
   ;;
 mmc*) MYICON="$CFICON"
   ;;   
 ub[a-f]*) MYICON="$USBICON"
   ;;
esac

## go through the mountpoints (from fstab), see if we need to add anything
for APNT in $MNTPNTS
do
	[ "$APNT" = "/" ] && continue  #full HD install
	#case "$CRRNTLINKS" in *"$APNT "*) continue ;; esac # entry exists -- skip
	[ -d "$APNT" ] || mkdir $APNT
	NAME=${APNT#/mnt/}
	# add to globicons
	add_glob_icon "$APNT" "$MYICON"
	# get coordinates for desktop icon
	setcoordinates
	# add desktop icon
	addtopinboard "$APNT" "$NAME" "$MYX" "$MYY"
	let MYY=MYY+YSPACE # this is for next time setcoordinates is run
done

exit
