#!/bin/sh
#(c) Copyright Nov 2007 Barry Kauler www.puppylinux.com
#2007 Lesser GPL licence v2 (http://www.fsf.org/licensing/licenses/lgpl.html)
#a complete rewrite for Puppy 'Dingo', Nov 27 2007, Dec 1,5 2007.
#v3.95 4jan2007: -x option for rox.
#v3.96 17jan2008: full install, mounted '/' partition was not shown.
#v3.98 13mar2008: switch probepart and mut2 engines.
#v3.99 7apr2008: fix zombie process prevents partition unmount.

#using ${$} which is pid of script...
MYPID=${$}

. /etc/rc.d/PUPSTATE #v3.96
PMOUNT="pmount" #name of this executable.
MINIFOLDERXPM='/usr/local/lib/X11/mini-icons/mini-filemgr.xpm'

yaf-splash -font "8x16" -outline 0 -margin 4 -bg orange -text "Puppy Drive Mounter

Please wait, probing hardware..." &
X1PID=$!

#v3.98... 
HAVEMUT2='no'
[ -f /usr/sbin/mut ] && [ "`file /usr/sbin/mut | grep 'ELF 32-bit LSB executable'`" != "" ] && HAVEMUT2='yes'
PMOUNTENGINE='default'
PROBEPART="probepart"
PROBEDISK="probedisk"
ENGINEFLAG='false'
if [ "$HAVEMUT2" = "yes" ];then
 [ -f /root/.pmountengine ] && PMOUNTENGINE="`cat /root/.pmountengine`"
 if [ "$PMOUNTENGINE" = "mut2" ];then
  PROBEPART='mut probepart'
  PROBEDISK='mut probedisk'
  ENGINEFLAG='true'
 fi
fi

#v3.96 'mount' misreports which partition mounted on '/'...
ROOTDEV2=""
[ "$PUPMODE" = "2" ] && ROOTDEV2="`df | grep ' /$' | grep '^/dev/' | cut -f 1 -d ' '`"

DISKINFO="`$PROBEDISK`"
PARTSINFO="`$PROBEPART -k | grep -v 'none' | tr ' ' '_' | tr '\t' '_' | cut -f 1-3 -d '|' | tr '\n' ' '`" #v3.98 fix for mut2.

#add floppy drive, if any (note, zip detected by probedisk/probepart)...
if [ -e /sys/block/fd0 ];then
 DISKINFO="/dev/fd0|floppy|フロッピーディスクドライブ
$DISKINFO"
 PARTSINFO="/dev/fd0|vfat|1440
$PARTSINFO"
fi

PARTSGUI=""
echo -n "" > /tmp/ONEDISKNAME_${MYPID}
FRAMEFLAG="off"
for ONEPART in $PARTSINFO
do
 ONEDEV="`echo -n "$ONEPART" | cut -f 1 -d '|'`"
 DEVNAME="`echo -n "$ONEDEV" | cut -f 3 -d '/'`"
 ONEFS="`echo -n "$ONEPART" | cut -f 2 -d '|'`"
 [ "$ONEFS" = "swap" ] && continue
 #DRIVEXPM='/usr/local/lib/X11/mini-icons/mini-hdisk.xpm'
 
 OLDDISKNAME="`cat /tmp/ONEDISKNAME_${MYPID}`"
 #note, not the most efficient, doing this every loop...
 echo "$DISKINFO" |
 while read ONEDISKINFO
 do
  ONEDISKNAME="`echo "$ONEDISKINFO" | cut -f 1 -d '|' | cut -f 3 -d '/'`"
  [ "`echo -n "$DEVNAME" | grep "$ONEDISKNAME"`" != "" ] && echo "$ONEDISKNAME" > /tmp/ONEDISKNAME_${MYPID}
 done
 ONEDISKNAME="`cat /tmp/ONEDISKNAME_${MYPID}`"
 DSKPATTERN="^/dev/${ONEDISKNAME}|"
 ONEDISKDESCR="`echo "$DISKINFO" | grep "$DSKPATTERN" | cut -f 3 -d '|' | tr '<' '_' | tr '>' '_'`"
 if [ "$OLDDISKNAME" != "$ONEDISKNAME" ];then
  if [ "$FRAMEFLAG" = "off" ];then
   PARTSGUI="${PARTSGUI}<frame ${ONEDISKDESCR}>"
   FRAMEFLAG="on"
  else
   PARTSGUI="${PARTSGUI}</frame><frame ${ONEDISKDESCR}>"
   #FRAMEFLAG="off"
  fi
 fi
 
 ONESIZEK=`echo -n "$ONEPART" | cut -f 3 -d '|'`
 if [ $ONESIZEK -gt 1048576 ];then #1024*1024
  ONESIZE="`dc $ONESIZEK 1048576 \/ p`"
  ONESIZE="`printf "%.1f" $ONESIZE`G"
 else
  if [ $ONESIZEK -gt 99 ];then
   ONESIZE="`expr $ONESIZEK \/ 1024`M"
  else
   ONESIZE="`dc $ONESIZEK 1024 \/ p`"
   ONESIZE="`printf "%.1f" $ONESIZE`M"
  fi
 fi
 [ "$ONESIZE" = "0.0M" ] && ONESIZE=""
 DEVPATTERN="^${ONEDEV} "
 ONEMNTPT="`mount | grep "$DEVPATTERN" | cut -f 3 -d ' '`"
 [ "$ROOTDEV2" != "" ] && [ "$ROOTDEV2" = "$ONEDEV" ] && ONEMNTPT='/' #v3.96
 if [ "$ONEMNTPT" = "" ];then #not mounted.
  #[ "$ONEFS" = "iso9660" ] && DRIVEXPM='/usr/local/lib/X11/mini-icons/mini-cd.xpm'
  #<input file>${DRIVEXPM}</input>
  PARTSGUI="${PARTSGUI}<hbox><text><label>${DEVNAME} ${ONEFS} ${ONESIZE}</label></text><button><label>マウント</label><action type=\"exit\">DOMOUNT_${ONEDEV}_${ONEFS}</action></button></hbox>"
 else
  #DRIVEXPM='/usr/local/lib/X11/mini-icons/mini-hdisk-green.xpm'
  #[ "$ONEFS" = "iso9660" ] && DRIVEXPM='/usr/local/lib/X11/mini-icons/mini-cd-green.xpm'
  FREEK=`df -k | tr -s ' ' | grep "$DEVPATTERN" | cut -f 4 -d ' '`
  if [ $FREEK -gt 1048576 ];then #1024*1024
   ONEFREE="`dc $FREEK 1048576 \/ p`"
   ONEFREE="`printf "%.1f" $ONEFREE`G"
  else
   if [ $FREEK -gt 99 ];then
    ONEFREE="`expr $FREEK \/ 1024`M"
   else
    ONEFREE="`dc $FREEK 1024 \/ p`"
    ONEFREE="`printf "%.1f" $ONEFREE`M"
   fi
  fi
  FREEINSERT="<text><label>${ONEFREE} 空き</label></text>"
  [ "$ONEFREE" = "0.0M" ] && FREEINSERT=""
  #in some cases prevent unmounting...
  INVISBUT=""
  [ "`echo -n "$ONEMNTPT" | grep '^/initrd'`" -o "$ONEMNTPT" = "/" ] && INVISBUT='<visible>disabled</visible>'
  
  #if 'home' partition, substitute /mnt/home...
  if [ -L /mnt/home -a "`echo "$ONEMNTPT" | grep '/initrd/'`" != "" ];then
   HOMELINK="`readlink /mnt/home`"
   [ "$HOMELINK" = "$ONEMNTPT" ] && ONEMNTPT="/mnt/home"
  fi
  
  #<input file>${DRIVEXPM}</input>
  PARTSGUI="${PARTSGUI}<hbox><button><input file>${MINIFOLDERXPM}</input><action>rox -d ${ONEMNTPT} &</action></button><text><label>${DEVNAME} ${ONEFS} ${ONESIZE}</label></text>${FREEINSERT}<button><label>アンマウント</label>${INVISBUT}<action type=\"exit\">UNMOUNT_${ONEDEV}_${ONEFS}_${ONEMNTPT}</action></button></hbox>"
 fi
done

if [ "$FRAMEFLAG" = "on" ];then
 PARTSGUI="${PARTSGUI}</frame>"
fi

if [ "$HAVEMUT2" = "yes" ];then
 ENGBUTTON="
   <button><label>エンジン: ${PMOUNTENGINE}</label>
    <action type=\"exit\">SETENGINE</action>
   </button>"
fi
export PMOUNTGUI="
<window title=\"Pmount-パピードライブマウンタ\" icon-name=\"gtk-harddisk\">
 <vbox>
  <text use-markup=\"true\">
  <label>\"<b>マウントされているメディアは外さないで下さい</b>\"</label></text>

  ${PARTSGUI}

  <hbox>
   ${ENGBUTTON}
   <button><input file stock=\"gtk-refresh\"></input><label>更新</label>
    <action type=\"exit\">REFRESH</action>
   </button>
   <button><input file stock=\"gtk-quit\"></input><label>終了</label></button>
  </hbox>
 </vbox>
</window>"

echo "$PMOUNTGUI" > /tmp/pmountdlg.txt_${MYPID}

kill $X1PID

RETPARAMS="`gtkdialog3 --file=/tmp/pmountdlg.txt_${MYPID} --center`"

rm -f /tmp/pmountdlg.txt_${MYPID}

#echo "$RETPARAMS"

eval "$RETPARAMS"

[ "$EXIT" = "REFRESH" ] && exec /usr/sbin/$PMOUNT

if [ "$EXIT" = "SETENGINE" ];then
 DLGSETENG="
<wtitle>Pmount: エンジンの選択</wtitle>
 <vbox>
  <text><label>パピーがドライブの精査に利用するデフォルトユーティリティは「probedisk」と「probepart」です。もう一つのものは、Jesse Liley Lileyが開発した「mut」と呼ばれるユーティリティです。これはデフォルトユーティリティと同様な動作をする複数の機能があります。デフォルトユーティリティがドライブを正しく検出しないなら「mut」あるいは「vice-versa」に切替えて下さい。</label></text>
  <checkbox>
   <label>「mut」を使うにはボックスにチェックを入れて下さい</label>
   <default>${ENGINEFLAG}</default>
   <variable>NEWENGINEFLAG</variable>
  </checkbox>
  <hbox>
   <button ok></button>
   <button cancel></button>
  </hbox>
 </vbox>"
 NEWENGINEFLAG=""
 RETPARAMS="`echo "$DLGSETENG" | gtkdialog2 --stdin`"
 eval "$RETPARAMS"
 if [ "$EXIT" = "OK" ];then
  [ "$NEWENGINEFLAG" = "true" ] && echo -n "mut2" > /root/.pmountengine
  [ "$NEWENGINEFLAG" = "false" ] && echo -n "default" > /root/.pmountengine
 fi
 exec /usr/sbin/$PMOUNT
fi

if [ "`echo -n "$EXIT" | grep 'DOMOUNT'`" != "" ];then
 DODEV="`echo -n "$EXIT" | cut -f 2 -d '_'`"
 DEVNAME="`echo -n "$DODEV" | cut -f 3 -d '/'`"
 DOFS="`echo -n "$EXIT" | cut -f 3 -d '_'`"
 mkdir -p /mnt/$DEVNAME
 case $DOFS in
  ntfs)
   #'mount' is a script that takes care of mounting ntfs...
   mount -t ntfs-fuse $DODEV /mnt/$DEVNAME
   RETVAL1=$?
   ;;
  vfat)
   mount -t vfat -o shortname=mixed,iocharset=utf8 $DODEV /mnt/$DEVNAME
   RETVAL1=$?
   ;;
  *)
   mount -t $DOFS $DODEV /mnt/$DEVNAME
   RETVAL1=$?
   ;;
 esac
 #now restart pmount, but want rox window on top... 
 if [ $RETVAL1 -eq 0 ];then
  /usr/sbin/$PMOUNT &
  NEWPID="$!"
  while [ ! -f /tmp/pmountdlg.txt_${NEWPID} ];do
   usleep 200000
  done
  usleep 100000
  #v3.95 -x option to rescan...
  exec rox -x /mnt/$DEVNAME -d /mnt/$DEVNAME #want this window on top.
  #exit
 else
  gxmessage -bg red -center -title "Pmount" "エラー: $DEVNAMEをマウントできません"
  exec /usr/sbin/$PMOUNT
 fi 
fi

if [ "`echo -n "$EXIT" | grep 'UNMOUNT'`" != "" ];then
 DODEV="`echo -n "$EXIT" | cut -f 2 -d '_'`"
 DEVNAME="`echo -n "$DODEV" | cut -f 3 -d '/'`"
 DOFS="`echo -n "$EXIT" | cut -f 3 -d '_'`"
 MNTPT="`echo -n "$EXIT" | cut -f 4 -d '_'`"
 rox -D $MNTPT
 sync
 umount $MNTPT ; umVAL=$?
 #v3.99 fix zombie process prevents partition unmount, invisible to fuser...
 if [ $umVAL -ne 0 ];then
  ZOMBIES="`ps -H -A | grep '<defunct>' | sed -e 's/  /|/g' | grep -v '|||' | cut -f 1 -d ' ' | tr '\n' ' '`"
  for ONEZOMBIE in $ZOMBIES
  do
   echo "Killing parentless zombie process $ONEZOMBIE"
   kill $ONEZOMBIE
  done
  umount $MNTPT ; umVAL=$?
 fi
 #v3.99 rare ocassion, partition got unmounted before umount runs above...
 umPATTERN="${MNTPT} "
 [ "`mount | grep "$umPATTERN"`" = "" ] && umVAL=0
 if [ $umVAL -ne 0 ];then
  EXTRAMSG0="`fuser -v -m $MNTPT`"  
  if [ "$EXTRAMSG0" = "" ];then
   gxmessage -bg "#ff8080" -title "Puppy drive mounter: エラー" -center "==失敗！== リムーバブルメディアの場合
大部分の理由はメディアが差し込まれていない事です。
もし差し込んでなかいなら差し込んで下さい。"
  else
   gxmessage -bg "#ff00ff" -title "KILL, KILL..." -center -buttons KILL:10,EXIT:20 "==失敗！== 一つ以上のプロセス(プログラム)が現在
パーティションを使用中です。ほら:
$EXTRAMSG0

「KILL」ボタンを押すとパピーは問題のあるプログラムを
つぶそうとします。これは最後の手段として下さい。
最初は手動でプログラムを閉じてみて下さい。
そのため「KILL」を押さないで下さい。
==「EXIT」ボタンを押して下さい！=="
   [ $? -eq 10 ] && fuser -k -m $MNTPT
  fi
 else
  [ "$DOFS" = "iso9660" ] && eject $DODEV
 fi
 exec /usr/sbin/$PMOUNT
fi

###END###
