WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: Mount drives from desktop icons via iDesk  (Read 447 times)

Offline Misalf

  • Hero Member
  • *****
  • Posts: 616
Mount drives from desktop icons via iDesk
« on: June 22, 2015, 12:26:59 PM »
This script creates desktop icon files for use with iDesk to (u)mount drives listed in /mnt.

If such a desktop icon is clicked to mount/unmount a drive, the script will be executed too, rebuilds the icon files and kills/starts (kinda restarts) idesk.
I haven't looked into udev rules to automatically run the script upon inserting a usb stick. Might do so at some point.

NOTE: The script has to be edited manually with proper png images for Floppy, CD, and HDD because there are no default ones that would fit.
(I copied some from gnome-icon-theme and have them backed up in my  ~/.local/share/icons  folder)

(idesk.tcz from 5.x repo seems to work in 6.x - needs confirmation though)

I have  idesk.tcz  loaded OnBoot and the script starts via  ~/.X.d/01-idesk-mount .
Code: [Select]
[ $(which idesk) ] && idesk-mount.sh >/dev/null 2>&1 &

idesk-mount.sh  resides in  ~/.local/bin .
Code: [Select]
#!/bin/sh
# idesk-mount.sh for Tiny Core Linux
# Creates desktop icon files for use with iDesk to u/mount devices listed in /mnt.
# (c) Misalf 2015
#

WIDTH=32
HEIGHT=32
GAP=48
X_POS=16
Y_POS=64

TCUSER=`cat /etc/sysconfig/tcuser`
ICON_PATH="/home/$TCUSER/.local/share/icons"
ICON_CD="$ICON_PATH/optical-unmounted.png"
ICON_CD_MNT="$ICON_PATH/optical.png"
ICON_FD="$ICON_PATH/floppy.png"
ICON_FD_MNT="$ICON_PATH/floppy-unmounted.png"
ICON_USB="$ICON_PATH/removable.png"
ICON_USB_MNT="$ICON_PATH/removable-unmounted.png"
ICON_HDD="$ICON_PATH/hdd-unmounted.png"
ICON_HDD_MNT="$ICON_PATH/hdd.png"
ICON_HDD_LINUX="$ICON_HDD"
ICON_HDD_LINUX_MNT="$ICON_HDD_MNT"
ICON_HDD_WIN="$ICON_HDD"
ICON_HDD_WIN_MNT="$ICON_HDD_MNT"

[ -z "$FILEMGR" ] && FILEMGR=xfe # See ~/.profile

for i in `ls /mnt`
do
ICONFILE="/home/$TCUSER/.idesktop/$i.lnk"

FSTYPE_TAB=`cat /etc/fstab | grep $i | awk '{printf "%s\n", $3}'`
if [ -z "$FSTYPE_TAB" ]; then
[ -e "$ICONFILE" ] && rm "$ICONFILE" && echo -e "$i \t-REMOVED-"
continue
fi

FSTYPE_MNT=`df -T | grep $i | awk '{printf "%s\n", $2}'`
if [ -n "$FSTYPE_MNT" ]; then
MOUNTCMD=umount
case $i in
sr*) ICON="$ICON_CD_MNT" ;;
fd*) ICON="$ICON_FD_MNT" ;;
*)
case $FSTYPE_MNT in
ext*) ICON="$ICON_HDD_LINUX_MNT" ;;
*fat|ntfs*) ICON="$ICON_HDD_WIN_MNT" ;;
fuseblk)
ICON="$ICON_HDD_WIN_MNT"
[ -n "$FSTYPE_TAB" ] && FSTYPE_MNT="$FSTYPE_TAB"
;;
*) ICON="$ICON_HDD_MNT" ;;
esac
;;
esac
FSTYPE=" - $FSTYPE_MNT"
else
MOUNTCMD=mount
case $i in
sr*) ICON="$ICON_CD" ;;
fd*) ICON="$ICON_FD" ;;
*)
case $FSTYPE_TAB in
ext*) ICON="$ICON_HDD_LINUX" ;;
*fat|ntfs*) ICON="$ICON_HDD_WIN" ;;
*) ICON="$ICON_HDD" ;;
esac
;;
esac
[ "$FSTYPE_TAB" == "auto" ] && FSTYPE="" || FSTYPE=" - $FSTYPE_TAB"
fi

echo "table Icon" > "$ICONFILE"
FSSIZE_FULL=`df -h | grep $i | awk '{printf "%s", $2}'`
FSSIZE_USED=`df -h | grep $i | awk '{printf "%s", $3}'`
if [ "$MOUNTCMD" == "umount" ]; then
echo "  Caption: [$i]" >> "$ICONFILE"
if [ "$FSSIZE_FULL" == "$FSSIZE_USED" ]; then
FSSIZE=" - ${FSSIZE_FULL}"
else
FSSIZE=" - ${FSSIZE_USED}/${FSSIZE_FULL}"
fi
else
echo "  Caption:  $i " >> "$ICONFILE"
FSSIZE=""
fi
echo "  ToolTip.Caption:  /mnt/$i${FSSIZE}${FSTYPE}" >> "$ICONFILE"
echo "  Icon: $ICON" >> "$ICONFILE"
echo "  Width: $WIDTH" >> "$ICONFILE"
echo "  Height: $HEIGHT" >> "$ICONFILE"
echo "  X: $X_POS" >> "$ICONFILE"
echo "  Y: $Y_POS" >> "$ICONFILE"
echo "  Command[0]: sudo $MOUNTCMD /dev/$i ; `basename $0`" >> "$ICONFILE"
[ -n "$FILEMGR" ] && echo "  Command[1]: $FILEMGR /mnt/$i" >> "$ICONFILE"
echo "end" >> "$ICONFILE"

X_POS=$((X_POS+$GAP))
done


killall idesk >/dev/null 2>&1
idesk >/dev/null 2>&1 &



The script is quite basic currently as my coding skills are not the greatest. But it certainly fits my own needs and reminds me a lot of Puppy Linux. (: However, I wrote it from scratch so don't expect anything too fancy.

This is my ~/.ideskrc (but you can safely use your own).
Code: [Select]
table Config
  FontName: terminus
  FontSize: 9
  FontColor: #615E5B
  ToolTip.FontSize: 10
  ToolTip.FontName: terminus
  ToolTip.ForeColor: #FFB000
  ToolTip.BackColor: #262624
  ToolTip.CaptionOnHover: true
  ToolTip.CaptionPlacement: Right
  Locked: true
  Transparency: 50
  Shadow: true
  ShadowColor: #0D0D0C
  ShadowX: 1
  ShadowY: 1
  Bold: false
  ClickDelay: 400
  IconSnap: false
  SnapWidth: 16
  SnapHeight: 16
  SnapOrigin: center
  SnapShadow: true
  SnapShadowTrans: 160
  CaptionOnHover: false
  CaptionPlacement: bottom
  FillStyle: FillInvert
  Background.Delay: 0
  Background.Source: None
  Background.File: None
  Background.Mode: Stretch
  Background.Color: #262624
  CursorOver: ul_angle
end

table Actions
  Lock: control middle singleClk
  Reload: middle doubleClk
  Drag: left hold
  EndDrag: left singleClk
  Execute[0]: left doubleClk
  Execute[1]: right doubleClk
end



Any recommendation very welcome.
« Last Edit: June 22, 2015, 12:32:43 PM by Misalf »

Offline Misalf

  • Hero Member
  • *****
  • Posts: 616
Re: Mount drives from desktop icons via iDesk
« Reply #1 on: June 23, 2015, 03:55:45 AM »
As for the udev rules, got it working partially. The script will run for any USB device connected though (mouse etc). KERNEL=="sd*" doesn't seem to be used as I thought.

/etc/udev/rules.d/99-idesk-mount.rules
Code: [Select]
SUBSYSTEM=="usb", ACTION=="add", \
ENV{DEVTYPE}=="usb_device", RUN+="/lib/udev/idesk-tcuser add &"

SUBSYSTEM=="usb", ACTION=="remove", \
ENV{DEVTYPE}=="usb_device", RUN+="/lib/udev/idesk-tcuser remove &"


/lib/udev/idesk-tcuser
Code: [Select]
#!/bin/sh
export DISPLAY=:0.0
TCUSER=`cat /etc/sysconfig/tcuser`
[ "$1" == "add" ] && SLEEP="5" || SLEEP="0.1"
/bin/su -c "sleep $SLEEP; /home/$TCUSER/.local/bin/idesk-mount.sh" "$TCUSER" &



Offline Rich

  • TinyCore Moderator
  • Hero Member
  • *****
  • Posts: 4582
Re: Mount drives from desktop icons via iDesk
« Reply #2 on: June 23, 2015, 06:37:09 AM »
Hi Misalf
Go to the search page and search for  subsystem , you'll get about 30 results. Check the results that list  subsystem== , they
may provide some useful information about  udev  usage.

Offline Misalf

  • Hero Member
  • *****
  • Posts: 616
Re: Mount drives from desktop icons via iDesk
« Reply #3 on: June 23, 2015, 08:07:11 AM »
Thanks Rich.
subsystem==block  works better than  subsystem==usb .

Also, when idesk was executed via udev, it was running as root I think. Icons were not click-able.

This should work:

/etc/udev/rules.d/99-idesk-mount.rules
Code: [Select]
ACTION=="add|remove", SUBSYSTEM=="block", KERNEL=="sd[a-z]", \
RUN+="/bin/su -c '/lib/udev/idesk-tcuser &'"


/lib/udev/idesk-tcuser
Code: [Select]
#!/bin/sh
TCUSER=`/bin/cat /etc/sysconfig/tcuser`
/bin/su -c "/bin/sleep 1 ; DISPLAY=:0.0 /home/$TCUSER/.local/bin/idesk-mount.sh" - "$TCUSER" &


« Last Edit: June 23, 2015, 08:34:12 AM by Misalf »

Offline Misalf

  • Hero Member
  • *****
  • Posts: 616
Re: Mount drives from desktop icons via iDesk
« Reply #4 on: June 23, 2015, 08:35:54 AM »
I made a mistake in above script ; corrected.

Offline Misalf

  • Hero Member
  • *****
  • Posts: 616
Re: Mount drives from desktop icons via iDesk
« Reply #5 on: June 25, 2015, 05:55:35 AM »
Some improvements to the script:
Each drive is displayed on its own row, along with its partitions.
Removable drives (usb) use a dedicated icon image.
Mounted drives use icon images suffixed with "-mounted".
Size, file system type and label are displayed in tool tips.

idesk-mount.sh
Code: [Select]
#!/bin/sh
# idesk-mount.sh for Tiny Core Linux
# Creates icon files for iDesk to u/mount devices listed in /mnt.
# (c) Misalf 2015
#

WIDTH=32
HEIGHT=32
X_GAP=48
Y_GAP=64
X_POS_INIT=16
X_POS="$X_POS_INIT"
Y_POS_INIT=64
Y_POS="$Y_POS_INIT"

TCUSER=$(cat /etc/sysconfig/tcuser)
ICON_PATH="/home/$TCUSER/.local/share/icons"
ICON_CD="$ICON_PATH/optical.png"
ICON_FD="$ICON_PATH/floppy.png"
ICON_USB="$ICON_PATH/removable.png"
ICON_HDD="$ICON_PATH/hdd.png"
ICON_HDD_LINUX="$ICON_PATH/hdd-linux.png"
ICON_HDD_DOS="$ICON_PATH/hdd-dos.png"
ICON_HDD_WIN="$ICON_PATH/hdd-win.png"

[ -z "$FILEMGR" ] && FILEMGR=xfe # See ~/.profile


f_icon_newline() {
if [ -n "$2" ]; then
if [ "$1" != "$2" ]; then
Y_POS=$((Y_POS+Y_GAP))
[ "$X_POS" -gt "$X_POS_INIT" ] && X_POS=$X_POS_INIT
fi
fi
}

f_icon_image() {
[ "$MOUNTED" = "-mounted" ] && DEVFS="$FSTYPE_MNT" || DEVFS="$FSTYPE_TAB"
case $i in
sr*)
ICON="${ICON_CD/.png/$MOUNTED.png}"
f_icon_newline "${i:1:1}" "${PREV_DEV:1:1}"
;;
fd*) ICON="${ICON_FD/.png/$MOUNTED.png}" ;;
*)
case $DEVFS in
ext*) ICON="${ICON_HDD_LINUX/.png/$MOUNTED.png}" ;;
*fat*|msdos) ICON="${ICON_HDD_DOS/.png/$MOUNTED.png}" ;;
ntfs*) ICON="${ICON_HDD_WIN/.png/$MOUNTED.png}" ;;
fuseblk)
ICON="${ICON_HDD_WIN/.png/$MOUNTED.png}"
[ -n "$FSTYPE_TAB" ] && FSTYPE_MNT="$FSTYPE_TAB"
;;
*) ICON="${ICON_HDD/.png/$MOUNTED.png}" ;;
esac
REMOVABLE=$(cat /sys/block/"${i:0:3}"/removable)
if [ "$REMOVABLE" = "1" ]; then
ICON="${ICON_USB/.png/$MOUNTED.png}"
fi
f_icon_newline "${i:2:1}" "${PREV_DEV:2:1}"
;;
esac
}


for i in /mnt/*
do
i="$(echo "$i" | sed 's/\/mnt\///g')"
ICONFILE="/home/$TCUSER/.idesktop/$i.lnk"

FSTYPE_TAB=$(grep "$i" /etc/fstab | awk '{printf "%s\n", $3}')
if [ -z "$FSTYPE_TAB" ]; then
[ -e "$ICONFILE" ] && rm "$ICONFILE" && echo -e "$i \t-REMOVED-"
continue
fi

LABEL="\"$(blkid -s LABEL -o value /dev/"$i")\""
FSSIZE_FULL=$(df -h | grep "$i" | awk '{printf "%s", $2}')
FSSIZE_USED=$(df -h | grep "$i" | awk '{printf "%s", $3}')

FSTYPE_MNT=$(df -T | grep "$i" | awk '{printf "%s\n", $2}')
if [ -n "$FSTYPE_MNT" ]; then
MOUNTED="-mounted"
CAPTION="[$i]"
MOUNTCMD="umount"
f_icon_image
FSTYPE=" ($FSTYPE_MNT)"
if [ "$FSSIZE_FULL" = "$FSSIZE_USED" ]; then
FSSIZE=" - ${FSSIZE_FULL}"
else
FSSIZE=" - ${FSSIZE_USED}/${FSSIZE_FULL}"
fi
else
MOUNTED=""
CAPTION=" $i "
MOUNTCMD="mount"
f_icon_image
[ "$FSTYPE_TAB" = "auto" ] && FSTYPE="" || FSTYPE=" ($FSTYPE_TAB)"
FSSIZE=""
fi

{
echo "table Icon"
echo "  Caption: $CAPTION"
echo "  ToolTip.Caption:  $i${FSSIZE}${FSTYPE} ${LABEL}"
echo "  Icon: $ICON"
echo "  Width: $WIDTH"
echo "  Height: $HEIGHT"
echo "  X: $X_POS"
echo "  Y: $Y_POS"
echo "  Command[0]: sudo $MOUNTCMD /dev/$i ; $(basename "$0")"
[ -n "$FILEMGR" ] && echo "  Command[1]: $FILEMGR /mnt/$i"
echo "end"
} > "$ICONFILE"

PREV_DEV="$i"
X_POS=$((X_POS+X_GAP))
done

killall idesk >/dev/null 2>&1
/usr/local/bin/idesk >/dev/null 2>&1 &



Offline Misalf

  • Hero Member
  • *****
  • Posts: 616
Re: Mount drives from desktop icons via iDesk
« Reply #6 on: July 06, 2015, 11:06:29 AM »
I have attached 32x32 sized .png images which I made (edited) to reflect Floppy, HDD, CD, USB, file system, mounted/not-mounted and the Tiny Core backup device. Based on gnome, Mint-X and the TC forums favicon. (: I have mine in  ~/.local/share/icons/ .

- /etc/sysconfig/backup_device  gets read to indicate the Tiny Core drive. This hasn't been thought through very well yet.

- If ImageMagic is loaded, the script now creates .png images from .ico files (/tmp/idesk-mount/*) for removable media (CD, USB drives) if there exists an AUTORUN.INF file.

- Instead of naming icon config files in  ~/.idesktop/  just by the device names, they are now prefixed with  idesk-mount- , so they can safely be deleted by the script and rewritten. Otherwise, drives which are not attached anymore would still be displayed after reboot if a backup was made while they were attached.

Code: [Select]
#!/bin/sh
# idesk-mount.sh for Tiny Core Linux
# Creates icon files for iDesk to u/mount devices listed in /mnt.
# (c) Misalf 2015
#

WIDTH=32
HEIGHT=32
X_POS_INIT=16
X_POS="$X_POS_INIT"
Y_POS_INIT=64
Y_POS="$Y_POS_INIT"
#X_GAP=48
X_GAP=$((WIDTH+X_POS_INIT))
#Y_GAP=64
Y_GAP=$((HEIGHT+X_POS_INIT+X_POS_INIT))

ICON_PATH="$HOME/.local/share/icons"
ICON_BG="$ICON_PATH/idesk-mount-bg-mounted.png"
ICON_CD="$ICON_PATH/optical.png"
ICON_FD="$ICON_PATH/floppy.png"
ICON_HDD="$ICON_PATH/hdd.png"
ICON_HDD_LINUX="$ICON_PATH/hdd-linux.png"
ICON_HDD_DOS="$ICON_PATH/hdd-dos.png"
ICON_HDD_WIN="$ICON_PATH/hdd-win.png"

# FILEMGR See ~/.profile
if [ -z "$FILEMGR" ]; then
if [ "$(which xfe)" ]; then
FILEMGR="xfe"
elif [ "$(which pcmanfm)" ]; then
FILEMGR="pcmanfm"
elif [ "$(which mc)" ]; then
FILEMGR="mc"
elif [ "$(which urxvt)" ]; then
FILEMGR="urxvt -cd"
elif  [ "$(which aterm)" ]; then
# FILEMGR="aterm -cd" # Nope.
FILEMGR="(cd /mnt; aterm) &"
fi
fi

[ -d "$HOME/.idesktop" ] || mkdir -p "$HOME/.idesktop"
rm -f "$HOME"/.idesktop/idesk-mount-*.lnk 2>/dev/null

BACKUP_DEVICE=$(cat /etc/sysconfig/backup_device)
BACKUP_DEVICE="${BACKUP_DEVICE%%/*}"


f_icon_newline() {
if [ -n "$2" ]; then
if [ "$1" != "$2" ]; then
Y_POS=$((Y_POS+Y_GAP))
[ "$X_POS" -gt "$X_POS_INIT" ] && X_POS=$X_POS_INIT
fi
fi
}

f_icon_autorun() {
# iDesk cannot use image files in .ico format
# so we  convert  them to PNG via ImageMagick, if available.
if [ "$(which convert)" ]; then
if [ "$(ls /mnt/"$i"/autorun.inf 2>/dev/null)" ]; then
ICONICO="$(grep -i icon /mnt/"$i"/autorun.inf | sed -r 's/[i|I]con=//g' | sed 's/\\/\//g' | dos2unix)"
if [ -e "/mnt/$i/$ICONICO" ]; then
[ -d "/tmp/idesk-mount" ] || mkdir -p "/tmp/idesk-mount"
ICONTMP="/tmp/idesk-mount/$i.png"

ICOINDEX=$(convert -identify "/mnt/$i/$ICONICO" /dev/null | grep "$WIDTH"x"$HEIGHT" | awk '{printf "%s", $1}')
if [ -n "$ICOINDEX" ]; then
ICOINDEX="${ICOINDEX#\/mnt\/$i\/$ICONICO[}"
ICOINDEX="${ICOINDEX/]}"
else
# TODO: Need to check for alternate (higher) resolution image inside .ico file.
# For now just 'guessing'.
ICOINDEX=3
fi
[ -n "$1" ] || ICON="$ICON_BG"
convert "/mnt/$i/$ICONICO[$ICOINDEX]" -geometry "$WIDTH"x"$HEIGHT" "$ICONTMP" && \
convert -composite "$ICON" "$ICONTMP" "$ICONTMP" && \
[ -e "$ICONTMP" ] && ICON="$ICONTMP"
fi
fi
fi
}

f_icon_image() {
[ "$MOUNTED" = "-mounted" ] && DEVFS="$FSTYPE_MNT" || DEVFS="$FSTYPE_TAB"
case $i in
sr*)
ICON="${ICON_CD/.png/$MOUNTED.png}"
f_icon_autorun "$ICON"
f_icon_newline "${i:1:1}" "${PREV_DEV:1:1}"
;;
fd*) ICON="${ICON_FD/.png/$MOUNTED.png}" ;;
*)
case $DEVFS in
ext*) ICON="${ICON_HDD_LINUX/.png/$MOUNTED.png}" ;;
*fat*|msdos) ICON="${ICON_HDD_DOS/.png/$MOUNTED.png}" ;;
ntfs*) ICON="${ICON_HDD_WIN/.png/$MOUNTED.png}" ;;
fuseblk)
ICON="${ICON_HDD_WIN/.png/$MOUNTED.png}"
[ -n "$FSTYPE_TAB" ] && FSTYPE_MNT="$FSTYPE_TAB"
;;
*) ICON="${ICON_HDD/.png/$MOUNTED.png}" ;;
esac
REMOVABLE=$(cat /sys/block/"${i:0:3}"/removable)
if [ "$REMOVABLE" = "1" ]; then
ICON="${ICON/hdd-/removable-}"
f_icon_autorun
fi
[ "$BACKUP_DEVICE" = "$i" ] && ICON="${ICON/.png/-tinycore.png}"
f_icon_newline "${i:2:1}" "${PREV_DEV:2:1}"
;;
esac
}

f_icon_file() {
{
echo "table Icon"
echo "  Caption: $CAPTION"
echo "  ToolTip.Caption:  $i${FSSIZE}${FSTYPE} ${LABEL}"
echo "  Icon: $1"
echo "  Width: $WIDTH"
echo "  Height: $HEIGHT"
echo "  X: $X_POS"
echo "  Y: $Y_POS"
echo "  Command[0]: sudo $MOUNTCMD /dev/$i ; $(basename "$0")"
[ -n "$FILEMGR" ] && \
echo "  Command[1]: $FILEMGR /mnt/$i"
echo "end"
} > "$ICONFILE"
}


for i in /mnt/*
do
i="$(echo "$i" | sed 's/\/mnt\///g')"
ICONFILE="$HOME/.idesktop/idesk-mount-$i.lnk"

FSTYPE_TAB=$(grep "$i" /etc/fstab | awk '{printf "%s\n", $3}')
[ -z "$FSTYPE_TAB" ] && continue

LABEL="\"$(blkid -s LABEL -o value /dev/"$i")\""
FSSIZE_FULL=$(df -h | grep "$i" | awk '{printf "%s", $2}')
FSSIZE_USED=$(df -h | grep "$i" | awk '{printf "%s", $3}')

FSTYPE_MNT=$(df -T | grep "$i" | awk '{printf "%s\n", $2}')
if [ -n "$FSTYPE_MNT" ]; then
MOUNTED="-mounted"
CAPTION="[$i]"
MOUNTCMD="umount"
f_icon_image
FSTYPE=" ($FSTYPE_MNT)"
if [ "$FSSIZE_FULL" = "$FSSIZE_USED" ]; then
FSSIZE=" - ${FSSIZE_FULL}"
else
FSSIZE=" - ${FSSIZE_USED}/${FSSIZE_FULL}"
fi
else
MOUNTED=""
CAPTION=" $i "
MOUNTCMD="mount"
f_icon_image
[ "$FSTYPE_TAB" = "auto" ] && FSTYPE="" || FSTYPE=" ($FSTYPE_TAB)"
FSSIZE=""
fi

f_icon_file "$ICON"

PREV_DEV="$i"
X_POS=$((X_POS+X_GAP))
done

killall idesk >/dev/null 2>&1
/usr/local/bin/idesk >/dev/null 2>&1 &


Offline Misalf

  • Hero Member
  • *****
  • Posts: 616
Re: Mount drives from desktop icons via iDesk
« Reply #7 on: July 07, 2015, 04:10:33 AM »
A little late but here is a screenshot.
The icons at the top row are from wbar.
The folders at the bottom are custom iDesk icons, not effected by the script.


- Now checking  /etc/sysconfig/tcedir  instead of  /etc/sysconfig/backup_device  to indicate the "Tiny Core"-drive.
- Now using  nohup  to start idesk. The webs have suggested this, but I don't really understand what it does. I only know wbar is also started with 'no hang up', so that's a good thing for backgrounded programms?
- Converting .ico to .png was missing a parameter to get the correct resolution.

Code: [Select]
#!/bin/sh
# idesk-mount.sh for Tiny Core Linux
# Creates icon files for iDesk to u/mount devices listed in /mnt.
# (copyleft) Misalf 2015
#


WIDTH=32
HEIGHT=32
X_POS_INIT=16
X_POS="$X_POS_INIT"
Y_POS_INIT=64
Y_POS="$Y_POS_INIT"
#X_GAP=48
X_GAP=$((WIDTH+X_POS_INIT))
#Y_GAP=64
Y_GAP=$((HEIGHT+X_POS_INIT+X_POS_INIT))

ICON_PATH="$HOME/.local/share/icons"
ICON_BG="$ICON_PATH/idesk-mount-bg-mounted.png"
ICON_CD="$ICON_PATH/optical.png"
ICON_FD="$ICON_PATH/floppy.png"
ICON_HDD="$ICON_PATH/hdd.png"
ICON_HDD_LINUX="$ICON_PATH/hdd-linux.png"
ICON_HDD_DOS="$ICON_PATH/hdd-dos.png"
ICON_HDD_WIN="$ICON_PATH/hdd-win.png"

TCE_DIR="$(readlink /etc/sysconfig/tcedir)"
TCE_DIR="${TCE_DIR#/mnt/}"
TCE_DIR="${TCE_DIR%%/*}"

# FILEMGR See ~/.profile
if [ -z "$FILEMGR" ]; then
if [ "$(which xfe)" ]; then
FILEMGR="xfe"
elif [ "$(which pcmanfm)" ]; then
FILEMGR="pcmanfm"
elif [ "$(which mc)" ]; then
FILEMGR="xterm -e mc"
elif [ "$(which urxvt)" ]; then
FILEMGR="urxvt -cd"
elif  [ "$(which aterm)" ]; then
# FILEMGR="aterm -cd" # Nope.
FILEMGR="(cd /mnt; aterm) &"
fi
fi


f_icon_newline() {
if [ -n "$2" ]; then
if [ "$1" != "$2" ]; then
Y_POS=$((Y_POS+Y_GAP))
[ "$X_POS" -gt "$X_POS_INIT" ] && X_POS=$X_POS_INIT
fi
fi
}

f_icon_autorun() {
# iDesk cannot use image files in .ico format
# so we  convert  them to PNG via ImageMagick, if available.
if [ "$(which convert)" ]; then
if [ "$(ls /mnt/"$i"/autorun.inf 2>/dev/null)" ]; then
ICONICO="$(grep -i icon /mnt/"$i"/autorun.inf | sed -r 's/[i|I]con=//g' | sed 's/\\/\//g' | dos2unix)"
if [ -e "/mnt/$i/$ICONICO" ]; then
ICOINDEX="$(convert -identify "/mnt/$i/$ICONICO" /dev/null | grep "$WIDTH"x"$HEIGHT" | grep 32-bit | awk '{printf "%s", $1}')"
[ -n "$ICOINDEX" ] &&  ICOINDEX="${ICOINDEX#\/mnt\/$i\/$ICONICO}" || \
# TODO: Need to check for alternate (higher) resolution image inside .ico file. For now just 'guessing'.
ICOINDEX="[3]"
[ -n "$1" ] || ICON="$ICON_BG"
[ -d "/tmp/idesk-mount" ] || mkdir -p "/tmp/idesk-mount"
ICONTMP="/tmp/idesk-mount/$i.png"
convert -geometry "$WIDTH"x"$HEIGHT" "/mnt/$i/${ICONICO}${ICOINDEX}" "$ICONTMP" && \
convert -geometry "$WIDTH"x"$HEIGHT" -composite "$ICON" "$ICONTMP" "$ICONTMP" && \
[ -e "$ICONTMP" ] && ICON="$ICONTMP"
fi
fi
fi
}

f_icon_image() {
[ "$MOUNTED" = "-mounted" ] && DEVFS="$FSTYPE_MNT" || DEVFS="$FSTYPE_TAB"
case $i in
sr*)
ICON="${ICON_CD/.png/$MOUNTED.png}"
f_icon_autorun "dummy"
f_icon_newline "${i:1:1}" "${PREV_DEV:1:1}"
;;
fd*) ICON="${ICON_FD/.png/$MOUNTED.png}" ;;
*)
case $DEVFS in
ext*) ICON="${ICON_HDD_LINUX/.png/$MOUNTED.png}" ;;
*fat*|msdos) ICON="${ICON_HDD_DOS/.png/$MOUNTED.png}" ;;
ntfs*) ICON="${ICON_HDD_WIN/.png/$MOUNTED.png}" ;;
fuseblk)
ICON="${ICON_HDD_WIN/.png/$MOUNTED.png}"
[ -n "$FSTYPE_TAB" ] && FSTYPE_MNT="$FSTYPE_TAB"
;;
*) ICON="${ICON_HDD/.png/$MOUNTED.png}" ;;
esac
REMOVABLE=$(cat /sys/block/"${i:0:3}"/removable)
if [ "$REMOVABLE" = "1" ]; then
ICON="${ICON/hdd-/removable-}"
f_icon_autorun
fi
[ "$TCE_DIR" = "$i" ] && ICON="${ICON/.png/-tinycore.png}"
f_icon_newline "${i:2:1}" "${PREV_DEV:2:1}"
;;
esac
}

f_icon_file() {
{
echo "table Icon"
echo "  Caption: $CAPTION"
echo "  ToolTip.Caption:  $i${FSSIZE}${FSTYPE} ${LABEL}"
echo "  Icon: $1"
echo "  Width: $WIDTH"
echo "  Height: $HEIGHT"
echo "  X: $X_POS"
echo "  Y: $Y_POS"
echo "  Command[0]: sudo $MOUNTCMD /dev/$i ; $(basename "$0")"
[ -n "$FILEMGR" ] && \
echo "  Command[1]: $FILEMGR /mnt/$i"
echo "end"
} > "$ICONFILE"
}


[ -d "$HOME/.idesktop" ] || mkdir -p "$HOME/.idesktop"
rm -f "$HOME"/.idesktop/idesk-mount-*.lnk 2>/dev/null

for i in /mnt/*
do
i="$(echo "$i" | sed 's/\/mnt\///g')"
ICONFILE="$HOME/.idesktop/idesk-mount-$i.lnk"

FSTYPE_TAB=$(grep "$i" /etc/fstab | awk '{printf "%s\n", $3}')
[ -z "$FSTYPE_TAB" ] && continue

LABEL="\"$(blkid -s LABEL -o value /dev/"$i")\""
FSSIZE_FULL=$(df -h | grep "$i" | awk '{printf "%s", $2}')
FSSIZE_USED=$(df -h | grep "$i" | awk '{printf "%s", $3}')

FSTYPE_MNT=$(df -T | grep "$i" | awk '{printf "%s\n", $2}')
if [ -n "$FSTYPE_MNT" ]; then
MOUNTED="-mounted"
CAPTION="[$i]"
MOUNTCMD="umount"
f_icon_image
FSTYPE=" ($FSTYPE_MNT)"
if [ "$FSSIZE_FULL" = "$FSSIZE_USED" ]; then
FSSIZE=" - ${FSSIZE_FULL}"
else
FSSIZE=" - ${FSSIZE_USED}/${FSSIZE_FULL}"
fi
else
MOUNTED=""
CAPTION=" $i "
MOUNTCMD="mount"
f_icon_image
[ "$FSTYPE_TAB" = "auto" ] && FSTYPE="" || FSTYPE=" ($FSTYPE_TAB)"
FSSIZE=""
fi

f_icon_file "$ICON"

PREV_DEV="$i"
X_POS=$((X_POS+X_GAP))
done

killall idesk >/dev/null 2>&1
nohup /usr/local/bin/idesk >/dev/null 2>&1 &