#!/bin/sh
# Script/function to add all partitions of a certain device to fstab
# device name (without "/dev/") passed as argument 1
# possible second argument: "yes" for automounting partitions
# part of the "hotpup" daemon scripts
## yet another dubious Dougal script for Puppy Linux, April 2007
## now have automount option
## Latest update: August 19th (move mountpoint creation to after we add to fstab)


## files used:
FSTAB="/etc/fstab"
#OLDFSTAB="`cat $FSTAB`"
MTAB=/etc/mtab
OLDMTAB="`cat $MTAB`"

fsfunc() { # supplied with a line of text, sets FSTYPE
case "$@" in 
#	*iso9660*) FSTYPE="iso9660" ; return 0;;
	*Ext3*) FSTYPE="ext3" ; return 0;;
	*Ext2*) FSTYPE="ext2" ; return 0;;
	*NTFS*) FSTYPE="ntfs" ; return 0;; ## needs my modified "mount"
	*[Ff][Aa][Tt]*|*msdos*) FSTYPE="vfat" ; return 0;;
	*[Rr]eiser*) FSTYPE="reiserfs" ; return 0;;
	*Minix*) FSTYPE="minix" ; return 0;;
	*'HFS Plus'*) FSTYPE="hfsplus" ; return 0;;
	*HFS*) FSTYPE="hfs" ; return 0;;
	*XFS*) FSTYPE="xfs" ; return 0;;
	*JFS*) FSTYPE="jfs" ; return 0;;
	*UFS*) FSTYPE="ufs" ; return 0;;
	*VxFS*) FSTYPE="vxfs" ; return 0;;
	*SysV*) FSTYPE="sysv" ; return 0;;
	*'BeOS BFS'*) FSTYPE="befs" ; return 0;;
	*) return 1 ;;
esac
}

removefromfstab(){
[ "$1" ] || return
grep -v "$1\W" $FSTAB >/tmp/fstab.tmp
sync
mv -f /tmp/fstab.tmp $FSTAB
}


#add_device_to_fstab(){
#[ -b /dev/$1 ] || return 1
[ -b /dev/$1 ] || exit 1
ADEV="$1"

# check if asked to automount partitions
[ "$2" = "yes" ] && AUTOMOUNT="yes"

# go over the output of "disktype", get all valid partitions and add to fstab	
FLAG="off" ; NUM="" ; FSTYPE="" # last two for superfloppy
while read ALINE 
do
    case "$ALINE" in
	*Partition?[0-9]*:*)
	  set -- $ALINE
	  NUM=${2%:}
	  case "$ADEV" in mmc*) NUM="p$NUM" ;; esac
	  #NUM=${ALINE%%:*} ; NUM=${NUM#* }
	  FLAG="on" # turn on here, since combined Type with fs detection
	  # (this doesn't cause problems with superfloppy, as the flag isn't used)
	  ;;
	#*Type*) 
	  #FLAG="on" #so we know if next is fs -- use it
	  #fsfunc "$ALINE"
	  #;;
	*Type*|*file?system*) 
	  # try getting fs from "Type", too (Linux in Type skipped)
	  fsfunc "$ALINE" && [ "$FLAG" = "on" ] || continue #filters out bad fs
	  # turn flag off, in case we're in a Type line (so won't repeat)
	  FLAG="off"
	  APNT="/mnt/$ADEV$NUM"
	  #CURRENTM="`grep -m1 "/dev/$ADEV$NUM\W" $MTAB`"
	  case "$OLDMTAB" in 
	   *"/dev/$ADEV$NUM / "*) continue ;; # full install mounted on /
	   */dev/$ADEV$NUM?/initrd/mnt/dev_save*) APNT="/mnt/home" ;;
	   */dev/$ADEV$NUM?/initrd/mnt/dev_ro1*) # seperate cases
	     LOOPINFO=`losetup /dev/loop1 2>/dev/null | cut -d'(' -f2`
	     case "$LOOPINFO" in
	      /mnt/dev_ro1/pup_save*) # pup_save on same partition as sfs
	        # link /initrd/mnt/dev_ro1 to /mnt/home
	        ln -snf /initrd/mnt/dev_ro1 /mnt/home
	        APNT="/mnt/home"
	        ;;
	      /mnt/dev_save/pup_save*) # pup_save on different partition
	        # link /initrd/mnt/dev_ro1 to $APNT
	        ln -snf /initrd/mnt/dev_ro1 $APNT
	        ;;
	      *) # two cases left: running in RAM ; files saved to partition
	        if [ "`. /etc/rc.d/PUPSTATE ; echo "$PUPMODE"`" = "5" ]; then
	          # link /initrd/mnt/dev_ro1 to $APNT
	          ln -snf /initrd/mnt/dev_ro1 $APNT
	        else # skip: mounted on /
	          continue
	        fi
	        ;; 
	     esac
	     ;;
	  esac
	  #[ -d "$APNT" ] || mkdir $APNT
	  CURRENTF="`grep -m1 "/dev/$ADEV$NUM\W" $FSTAB`"
	  case "$CURRENTF" in # checking if exists and fstype correct
	   */dev/$ADEV$NUM*$APNT*$FSTYPE*) # exists and is ok  
	     #PARTSLIST="/dev/$ADEV$NUM $PARTSLIST" 
	     # automount option
	     [ "$AUTOMOUNT" ] && mount $APNT
	     continue 
	     ;;
	   */dev/$ADEV$NUM*/mnt/*) #entry exists for partition, but wrong fs/point!
	     removefromfstab "/dev/$ADEV$NUM" 
	     ;; #...and add again below  
	   */dev/$ADEV$NUM*/*$FSTYPE*) # full install to that partition?
	     continue
	     ;;
	  esac
	  case "$FSTYPE" in 
	   vfat) OPTIONS="shortname=mixed" ;;
	   ext2|ext3|reiserfs) OPTIONS="defaults,noatime" ;;
	   *) OPTIONS="defaults" ;;
	  esac
	  echo -e "/dev/$ADEV$NUM\t$APNT\t\t$FSTYPE\t\t$OPTIONS\t\t0 0" >>$FSTAB
	  [ -d "$APNT" ] || mkdir $APNT
	  # automount option
	  [ "$AUTOMOUNT" ] && mount $APNT
	  #PARTSLIST="/dev/$ADEV$NUM $PARTSLIST"
	  ;;
	*'Volume size'*) FLAG="off"
	  ;;
	*)
	  ;;
	esac
done <<_END
$(disktype /dev/$ADEV 2>/dev/null)
_END

# no line starting with "Partition", but fs found: a superfloppy?
if [ ! "$NUM" ] && [ "$FSTYPE" ];then
  # to get past disktype bug with FAT parts having "ext3" in the info...
  while read ALINE 
  do # get FSTYPE from first (valid) instance of "file system"
    FSTYPE=""
    case "$ALINE" in *file?system*) 
      fsfunc "$ALINE" && break ;; #sets FSTYPE
    esac
  done <<_END
$(disktype /dev/$ADEV 2>/dev/null)
_END
  if [ "$FSTYPE" ] ; then
    APNT="/mnt/$ADEV"
    #CURRENTM="`grep -m1 "/dev/$ADEV\W" $MTAB`"
    case "$OLDMTAB" in 
	   *"/dev/$ADEV / "*) continue ;; # full install mounted on /
  	   */dev/$ADEV?/initrd/mnt/dev_save*) APNT="/mnt/home" ;;
  	   */dev/$ADEV?/initrd/mnt/dev_ro1*) 
  	     LOOPINFO=`losetup /dev/loop1 2>/dev/null | cut -d'(' -f2`
	     case "$LOOPINFO" in
	      /mnt/dev_ro1/pup_save*) # pup_save on same partition as sfs
	        # link /initrd/mnt/dev_ro1 to /mnt/home
	        ln -snf /initrd/mnt/dev_ro1 /mnt/home
	        APNT="/mnt/home"
	        ;;
	      /mnt/dev_save/pup_save*) # pup_save on different partition
	        # link /initrd/mnt/dev_ro1 to $APNT
	        ln -snf /initrd/mnt/dev_ro1 $APNT
	        ;;
	      *) # two cases left: running in RAM ; files saved to partition
	        if [ "`. /etc/rc.d/PUPSTATE ; echo "$PUPMODE"`" = "5" ]; then
	          # link /initrd/mnt/dev_ro1 to $APNT
	          ln -snf /initrd/mnt/dev_ro1 $APNT
	        else # skip: mounted on /
	          exit 0
	        fi
	        ;; 
	     esac
	     ;;
	esac
	#[ -d "$APNT" ] || mkdir $APNT
    CURRENTF="`grep -m1 "/dev/$ADEV\W" $FSTAB`"
    case "$CURRENTF" in # checking if exists and fstype correct
     */dev/$ADEV*$APNT*$FSTYPE*) # exists and is ok 
       #PARTSLIST="/dev/$ADEV $PARTSLIST" 
       # automount option
       [ "$AUTOMOUNT" ] && mount $APNT
       exit 0
       ;; 
     */dev/$ADEV*/mnt/*) #entry exists for partition, but wrong fs/point!
       removefromfstab "/dev/$ADEV " 
       ;; #remove...and add again below 
     */dev/$ADEV*/*$FSTYPE*) # full install to that partition?
	   continue
	   ;;
    esac
    #[ "$FSTYPE" = "ntfs" ] && continue
    case "$FSTYPE" in 
	 vfat) OPTIONS="shortname=mixed" ;;
	 ext2|ext3|reiserfs) OPTIONS="defaults,noatime" ;;
	 *) OPTIONS="defaults" ;;
	esac
    echo -e "/dev/$ADEV\t$APNT\t\t$FSTYPE\t\t$OPTIONS\t\t0 0" >>$FSTAB
    [ -d "$APNT" ] || mkdir $APNT
    #PARTSLIST="/dev/$ADEV $PARTSLIST"
    # automount option
    [ "$AUTOMOUNT" ] && mount $APNT
  fi
fi

#} # add_device_to_fstab
exit 0
# run the function
#add_device_to_fstab $1
#exit $?