#!/bin/sh
#Barry Kauler www.puppylinux.com
#LGPL 2007 Puppy Linux www.puppylinux.com
#17 june 2007
#v3.93 10 dec 2007 BK: updated for 2.6.24 kernel, no /dev/hd*
#v3.97 handle /dev/hd also for retro kernels.
#v3.97 25feb2008 BK: guess_fstype does not work when testing 'makebootfat', use fdisk.
#v4.01 19may2008 BK: bugfix for 2.6.25.4, ram entries in /proc/partitions.
#100912 no longer supporting /proc/ide.
#101127 debug output.
#110125,110126 brought back /proc/ide /dev/hd* support.
#111109 replace guess_fstype with blkid (note, blkid very slow, not preferred).
#120602 kernel 3.2.18 major deviance from earlier kernels, /proc/partitions (and /sys/block) now shows /dev/sr0 when no disk.
#       new situation, getting duplicate /dev/sr0 so need 'sort -u'.
#130127 related to 120602, also screen sr0-9 out of /proc/partitions -- this may fix some problems, see main 'probepart'.
#130128 support 'udf' f.s. also see main 'probepart'.

#***this one runs in initrd, very slightly different from the main one.***

SUNITS="$1" #allowed params are '-k' or '-m'.
OUTPUT=""

##devices that have partitions... 
#([^k] is to eliminate mmcblk0 device -- allow mmcblk0p1 etc) v4.01 bugfix eliminate ram... 110126
#130127 [^kr][0-9] screens out sr0-9... (note, unfortunately will also screen out sdr0-9)
PARTITIONS="`grep -E '^ .*[^kr][0-9]$' /proc/partitions | tr -s ' ' | cut -f 4-5 -d ' ' | grep -vE ' loop| ram'`"
echo "PARTITIONS=${PARTITIONS}" > /tmp/probepart.log #101127
PARTNAMES="`echo "$PARTITIONS" | cut -f 2 -d ' '`" #120602

#101127 for comparison, log this... 110125 restore hd...
SYSBLOCKSUB="`ls -1 /sys/block/*/ | grep -E '^hd|^scd|^sd|^mmc|^sr' | tr '\n' ' '`"
echo "SYSBLOCKSUB=${SYSBLOCKSUB}" >> /tmp/probepart.log #101127

#all disk devices...
#ALLDEVS="`grep -E ' hd| scd| sd| mmc| sr' /proc/diskstats | tr -s ' ' | cut -f 4 -d ' ' | tr '\n' ' '`"
#ALLDRVS="`ls -1 /sys/block | grep -E '^hd|^scd|^sd|^mmc|^sr' | tr '\n' ' '`"
ALLDRVS="`ls -1 /sys/block | grep -E '^scd|^sd|^mmc|^sr'`"
[ -e /proc/ide ] && ALLDRVS="${ALLDRVS}
`ls -1 /proc/ide | grep '^hd'`" #110125 120602
echo "ALLDRVS=${ALLDRVS}" >> /tmp/probepart.log #101127 for debugging.

#all drives and partitions... 120602 sort... crap busybox sort does not support -V
ALLDEVS="`echo "${PARTNAMES}
${ALLDRVS}" | sort -u | tr '\n' ' '`"

for ONEDEV in $ALLDEVS
do
 MEDIATYPE="" #130128
 FSTYPE="unknown"
 DPATTERN=' '${ONEDEV}'$'
 SIZE=`echo "$PARTITIONS" | grep "$DPATTERN" | cut -f 1 -d ' '`
 DEVICE="`echo "$PARTITIONS" | grep "$DPATTERN" | cut -f 2 -d ' '`"
 echo "ONEDEV=${ONEDEV} SIZE=${SIZE} DEVICE=${DEVICE}" >> /tmp/probepart.log #101127 for debugging.
 if [ ! $SIZE ];then
  [ "`echo "$PARTITIONS" | grep "$ONEDEV"`" != "" ] && continue
  #must be a device without partitions...
  SIZE=0
  DEVICE="$ONEDEV"
  case $DEVICE in
   hd*)
    [ "`cat /proc/ide/$DEVICE/media`" = "cdrom" ] && MEDIATYPE="optical" #130128
   ;;
   scd*|sr*) #usb,sata,scsi cd/dvd drive.
    MEDIATYPE="optical" #130128
   ;;
  esac
  if [ "$MEDIATYPE" != "optical" ];then #130128
   #for hd* or sd* superfloppy, determine size...
   BSIZE=`disktype /dev/$DEVICE 2>/dev/null | grep '^Block device' | cut -f 2 -d '(' | cut -f 1 -d ' '`
   [ $BSIZE ] && SIZE=$(($BSIZE/1024)) #KB
  fi
 fi
 if [ -f /bin/guess_fstype ];then
  FSTYPE="`guess_fstype /dev/$DEVICE 2>/dev/null`"
 else
  #111109 replace guess_fstype with blkid (note, blkid very slow, not preferred)...
  #130526 Karl Godt reported this o/p from blkid, breaks parsing:
  # /dev/sdb1: UUID="5a126d47-065d-4eb6-baf9-dd7701bfe71a" TYPE="ext4" LABEL="boot"
  FSTYPE="`blkid /dev/$DEVICE 2>/dev/null | grep -o ' TYPE=".*"' | cut -f 2 -d '"'`" #'geany 130526
  [ `echo "$FSTYPE" | wc -l` -gt 1 ] && FSTYPE="unknown" #blkid may fallback to returning all partitions.
  [ ! "$FSTYPE" ] && FSTYPE="unknown" #paranoid precaution.
 fi
 echo "  SIZE=${SIZE} FSTYPE=${FSTYPE}" >> /tmp/probepart.log #101127 for debugging.
  
 #v3.97 guess_fstype fails sometimes...
 #(using makebootfat to setup a USB-FLOPPY/-HDD/-ZIP combined bootable FAT drive).
 xFSTYPE=''
 if [ "$FSTYPE" = "unknown" ];then
  fsPATTERN='^/dev/'"$DEVICE"' '
  xDEVICE="`echo -n "$DEVICE" | sed -e 's/[0-9]*$//'`" #"${DEVICE/[0-9]/}" #remove partition number.
  xFSTYPE="`fdisk -l /dev/$xDEVICE 2>/dev/null | grep "$fsPATTERN" | head -n 1 | grep -o -E 'FAT12$|FAT16$|FAT32$'`"
  [ "$xFSTYPE" != "" ] && FSTYPE='vfat'
 fi
 
 [ "$FSTYPE" = "unknown" ] && FSTYPE="none"
 
 [ "$SUNITS" = "" ] && SIZE=$(($SIZE*2)) #512 byte blocks.
 [ "$SUNITS" = '-m' ] && SIZE=$(($SIZE/1024)) #MB

 echo "/dev/$DEVICE|$FSTYPE|$SIZE"
 echo "  /dev/$DEVICE|$FSTYPE|$SIZE" >> /tmp/probepart.log #101127
done

###END###
