#!/bin/ash
#20220207 see /root/.jwmrc-tray, also /usr/local/ui/jwm
# called from /root/.xinitrc, /usr/local/ui/jwm/rescan-drives-menu,
# /usr/local/ui/unmount-all, /usr/local/pup_event/frontend_funcs, frontend_change,
# /bin/mount, umount
#20220218 fix if this script gets called while it is still running.
#20220218 write to /root/.jwmrc-drives within this script.
#20220327 optical and floppy drives weren't showing.

#/usr/local/pup_event/pup_event_frontend_d will call frontend_change if drive
#added or removed. frontend_change will call function create_icon_func() in
#frontend_funcs, which creates /root/.pup_event/drive_* folder. 
#create_appinfo_func() in frontend_funcs has been edited to create 'info' file.

export TEXTDOMAIN=ui-jwm
export OUTPUT_CHARSET=UTF-8

. /etc/rc.d/PUPSTATE

#20220218 worried this script might get called while it is still running...
CNT=0
while [ $CNT -lt 10 ];do
 NUM="$(pidof -o ${$} generate-drives-menu)"
 [ "${NUM}" == "" ] && break
 sleep 0.2
 CNT=$(($CNT+1))
done

UMNTmsg="$(gettext 'Unmount')"
UMNTALLmsg="$(gettext 'Unmount all')"
RESCANmsg="$(gettext 'Rescan drives')"

echo '<JWM>
<RootMenu label="Partitions" labeled="false" height="24" onroot="2">' > /root/.jwmrc-drives

MNTPTS="$(grep '^/dev/' /proc/mounts | grep -vE "^/dev/loop|^/dev/ram|^/dev/${WKG_DEV} " | cut -f 2 -d ' ' | grep -v '^/$')"

#20220217 maybe this is useful?
echo " <Program label=\"${RESCANmsg}\">/usr/local/ui/jwm/rescan-drives-menu</Program>" >> /root/.jwmrc-drives

if [ "$MNTPTS" ];then
   echo " <Program label=\"${UMNTALLmsg}\">/usr/local/ui/unmount-all</Program>" >> /root/.jwmrc-drives
fi

for aF in `find /root/.pup_event -mindepth 1 -maxdepth 1 -name 'drive_*' | sort -V`
do
 if [ -f ${aF}/info ];then
. ${aF}/info
  #ex:
  #PE_INFO='Filesystem: vfat Size: 639MB'
  #PE_DRV_CATEGORY='usbdrv'
  #PE_DRV_DESCRIPTION='SanDisk Extreme '
  #PE_ONEDRVNAME='sdb1'
  #PE_FSTYPE='vfat'
  #PE_DRV_NAME='sdb'
  #PE_SIZE='639MB'
  if [ "$PE_ONEDRVNAME" == "$PE_DRV_NAME" ];then
   #20220327 may be optical or floppy...
   case "$PE_ONEDRVNAME" in
    sr*|fd*) true ;;
    *) continue ;; #not a partition
   esac
  fi
  case "$PE_DRV_CATEGORY" in
   usbdrv)  DRVICON="usbdrv48.png" ;;
   card)    DRVICON="card48.png" ;;
   floppy)  DRVICON="floppy48.png" ;;
   optical) DRVICON="optical48.png" ;;
   *)       DRVICON="drive48.png" ;;
  esac

  MNTD=0
  grep -q "^/dev/${PE_ONEDRVNAME} " /proc/mounts
  if [ $? -eq 0 ];then
   if [ "${PE_ONEDRVNAME}" == "${WKG_DEV}" ];then
    DRVICON="${DRVICON/48/_mntd_boot48}"
   else
    MNTD=1
    DRVICON="${DRVICON/48/_mntd48}"
   fi
  fi

  #menu entry...
  #note: nvme partitions have PE_INFO='Full name: nvme0n1p1 Filesystem: ext4 Size: 2.6GB'
  # but $PE_ONEDRVNAME is the full name.
  echo " <Program label=\"${PE_ONEDRVNAME}\" tooltip=\"${PE_INFO/*Filesystem/Filesystem}\" icon=\"${DRVICON}\">/root/.pup_event/drive_${PE_ONEDRVNAME}/AppRun ${PE_DRV_CATEGORY} ${PE_FSTYPE}</Program>" >> /root/.jwmrc-drives
  if [ $MNTD -eq 1 ];then
   #echo " <Program label=\"${UMNTmsg}\" icon=\"invisible48.png\">umount /dev/${PE_ONEDRVNAME}</Program>"
   MNTPT="$(grep "^/dev/${PE_ONEDRVNAME} " /proc/mounts | cut -f 2 -d ' ')"
   echo " <Program label=\"${UMNTmsg}\" icon=\"invisible48.png\">/usr/local/ui/unmount-part ${MNTPT}</Program>" >> /root/.jwmrc-drives
  fi
 fi
done

echo '</RootMenu>
</JWM>' >> /root/.jwmrc-drives
fsync /root/.jwmrc-drives
###end###
