#!/bin/sh
dir=/usr/tmp
auto=$dir/addexp.auto	# Auto file: example entry: /usrX ijc0d1 94191
			# /usrX is sequential.
tmp=$dir/addexp.tmp
mounted=$dir/addexp.mounted
mountedNP=$dir/addexp.mountedNP
list=$dir/addexp.list
sedCmdFile=$dir/addexp.sed
nSwap=14	# 1st additional swap partition; OS limits to 16: root can have
		#  up to 2 swap partitions (7 & 5), so addexp limits s to 14.
# HISTORY:
# 1.11 910125 Long - Fix bug of re-creation of ip c0d2 thru c3d3 device files.
# 1.10 901219 Jay - Added "erase" option to force a newfs.  Added dkis support.
# 1.9 901102 Long - Major changes and improvements.

echo "addexp version 1.11                        If problems, call Long x8535"
pwd=`pwd`
if [ "$pwd" != "$dir" ]
then
  echo "Error: addexp not loaded in $dir"
  echo "Move addexp to $dir by typing 'mv addexp $dir'"
  echo "                        Then type 'cd $dir'"
  echo 'Bye - have a nice day!'
  exit 0
fi
USER=`whoami`
if [ "${USER}" != "root" ]
then
  echo "Error: You must be logged on as root to use addexp. "
  exit 0
fi
if [ ! -d /usr/bit ]
then 
  echo "Error: You must load bit before you run addexp. "
  exit 0
fi

SYSTYPE=`uname -t`
echo "Add expansion disks to the ${SYSTYPE} system.\
 Proceed? (y/n/?)" | tr '\12' '\40'
read ans
if test "$ans" != "y" -a "$ans" != "?"
then
  echo 'Bye - have a nice day!'
  exit 0
else 
if test "$ans" = "?"
then
  echo
  echo This script attempts to set up filesystems on formatted expansion disks
  echo and establish directories to mount the disks to.  Each expansion disk
  echo MUST have the volume header written to it using the format program,
  echo and proper jumpers set.  Disks which are already mounted or already
  echo contain a valid file system are not modified unless the ERASE option
  ehco is used.
  echo
  exit 0
fi
fi
# Find out mounted expansion disks.
df | grep '/usr[0-9]' | awk '{print $7" "$1}' | sort >$mounted
cat $mounted | awk 'BEGIN {FS = "/"} 
{print "/"$2 substr($5,1,6)" already mounted"}' >$mountedNP

enterOK=0		# flag unset: for confirming user's entered disk data

while test $enterOK -eq 0
do
  echo "   To specify SMD Ctlr0 Disk3 & model 2372-64,   type:  ipc0d3 2372-64"
  echo "   or to specify SCSI Ctlr0 Disk1 & model 94191, type:  ijc0d1 94191"
  echo "   or to specify SCSI C0, D1, 94601, & erased,   type:  ijc0d1 94601 erase"
  echo "   or other commands: (c)hange a line; (l)ist; (q)uit"

  i=1				# start with first expansion disk
  all=0				# flag unset: user not finished entering all
  rm $list > /dev/null 2>&1	# ensure empty $list file by removing it first
  while test $all -eq 0
  do
    grep "/usr${i} " $mountedNP > $tmp
    if [ -s $tmp ]		# mounted disk precedent than auto list
    then
      cat $tmp; cat $tmp >> $list
      i=`expr $i + 1`
    else
      if [ ! -s $auto ]	# if absent, create $auto file to silent grep complaint
      then
        touch $auto
      fi
      grep "/usr${i} " $auto > $tmp
      if [ -s $tmp ]
      then
        cat $tmp; cat $tmp >> $list
        i=`expr $i + 1`
      else
        echo "/usr${i}" | tr '\12' '\40'
        read ans
        case $ans in
        q)       all=1;;		# set flag
        l)       cat $list;;
        c)       echo "change which /usr?" | tr '\12' '\40'
                 read line	
                 echo "new mapping? /usr$line" | tr '\12' '\40'
                 read ans
                 echo $line'c\' >$sedCmdFile
                 echo /usr$line $ans >>$sedCmdFile
                 sed -f $sedCmdFile $list >$tmp
		 mv $tmp $list;;
        *c0d0*)  echo "Error: root disk not allowed!";;
        "")      echo "Error: blank entry!";;
        *)	 ERASE=`echo $ans | awk '{print $3}'`
		 if test "$ERASE" = "erase" -o "$ERASE" = "ERASE"
		 then
		   echo "Erasing the disk will delete everything on it."
		   /bin/echo "Are you sure you want to erase the disk? (y) \c"
		   read ans2
		   if test "$ans2" != "" -a "$ans2" != "y"
		   then
		     ans=`echo $ans | awk '{print $1" "$2}'`
		   fi
		 fi
	         echo /usr${i} $ans >> $list 
                 i=`expr $i + 1`;;
        esac
      fi
    fi
  done    # while test $all
  if [ -s $list ]		# if $list file exists and not empty
  then
    echo "You specified this list of expansion disks:"
    cat $list
    cp $list $auto
  else
    echo "You didn't specify any expansion disks!"
    echo 'Bye - have a nice day!'
    exit 0
  fi
  echo "OK ? (y/n)" | tr '\12' '\40'
  read ans
  if test "$ans" = "y" 
  then
    enterOK=1
  fi
done    # while test $enterOK

# Create new fstab: only root disk, including its swap partition(s).
cp /etc/fstab /etc/fstab.addexp
grep "/dev/root" /etc/fstab.addexp > $tmp
grep "/dev/usr" /etc/fstab.addexp >> $tmp
grep "c0d0" /etc/fstab.addexp >> $tmp
mv $tmp /etc/fstab
chmod 444 /etc/fstab

cd /dev
i=1	# Create SCSI device files
while test $i -le 7
do
  grep "ijc${i}" $list > $tmp
  if [ -s $tmp ]		# file $tmp is not empty
  then			
    if [ ! -r /dev/rscsi/ijc${i}d6s15 ]     # device files of ctlr not readable
    then
      echo "Creating SCSI c${i} device files..."
      /dev/MKDEV -m ${SYSTYPE} b_scsi$i
      /dev/MKDEV -m ${SYSTYPE} c_scsi$i	
    fi
  fi
  i=`expr $i + 1`
done  # while test $i

# Create SMD device files
d=ipc1
grep $d $list > $tmp
d=ipc0d2
grep $d $list >> $tmp
if [ -s $tmp ]		# file $tmp is not empty
then			
  if [ ! -r /dev/rdsk/${d}s15 ]     # device files of ctlr not readable
  then
    echo "Creating SMD c0d2 thru c3d3 device files..."
    /dev/MKDEV -m ${SYSTYPE} firstdisks
  fi
fi
d=ipc4d0
grep $d $list > $tmp
if [ -s $tmp ]		# file $tmp is not empty
then			
  if [ ! -r /dev/rdsk/${d}s15 ]     # device files of ctlr not readable
  then
    echo "Creating SMD c4d0 thru c7d3 device files..."
    /dev/MKDEV -m ${SYSTYPE} seconddisks
  fi
fi

p="3"   # partition 3 of non ROOT drive as /usrX; partitions 0 and 1 as swap
i=1	# start with first line of file $list
last=`wc -l $list`
s=0	# keep track of number of swap partitions
while test $i -le $last
do
  d=`sed -n ${i}p $list | awk '{print $2}'`
  case $d in
    ij*)     dir=scsi;;
    sd*)     dir=scsi;;
    is*)     dir=scsi;;
    ip*)     dir=dsk;;
    *)       echo Error: assigning scsi or smd;;
  esac

# Check if filesystem is already on drive
# 0  fs is not mounted & appears OK (except for root where 0 means mounted & OK)
# 1  fs is not mounted & needs to be checked.
# 2  fs is mounted.
# 3  command failed.
  grep "/usr${i} " $mounted > $tmp
  if [ -s $tmp ]		
  then
    mountedDev=`awk '{print $2}' $tmp`
    fsstat.ffs $mountedDev > /dev/null 2>&1
  else
    fsstat.ffs /dev/${dir}/${d}s${p} > /dev/null 2>&1
  fi

case $? in
0)
echo "Not mounted and OK       /usr${i}   ${d}"
# We get here if the filesystem is good, but the system did not mount at
# boot time.  Could be that a mount directory was not present, or fstab not
# set up for this drive yet...
ERASE=`sed -n ${i}p $list | awk '{print $4}'`
if test "$ERASE" = "erase" -o "$ERASE" = "ERASE"
then
  DRVSZ=`prtvtoc -h /dev/r${dir}/${d}s${p} | grep "	 3" | awk '{print $5}'`
  DVT=`sed -n ${i}p $list | awk '{print $3}'`	# device type
  echo "  newfs.ffs -s $DRVSZ /dev/${dir}/${d}s${p} $DVT" 
  newfs.ffs -s $DRVSZ /dev/${dir}/${d}s${p} $DVT > /dev/null 2>&1
fi

if [ ! -d /usr${i} ]	#  Checking for mount directory...
then
  echo "    /usr${i} absent. Making it..."
  mkdir /usr${i}
fi
# Add to /etc/fstab
echo "/dev/${dir}/${d}s${p}	/usr${i} ffs rw 0 0" >> /etc/fstab
if test $s -lt $nSwap
then
  echo "/dev/${dir}/${d}s0	none swap rw,noauto 0 0" >> /etc/fstab
  echo "/dev/${dir}/${d}s1	none swap rw,noauto 0 0" >> /etc/fstab
  s=`expr $s + 2`
fi
;;

1)
echo "Not mounted and dirty    /usr${i}   ${d}"
# reading drive info from the header...
DRVSZ=`prtvtoc -h /dev/r${dir}/${d}s${p} | grep "	 3" | awk '{print $5}'`
DVT=`sed -n ${i}p $list | awk '{print $3}'`	# device type
echo "  newfs.ffs -s $DRVSZ /dev/${dir}/${d}s${p} $DVT" 
newfs.ffs -s $DRVSZ /dev/${dir}/${d}s${p} $DVT > /dev/null 2>&1
if [ ! -d /usr${i} ]	#  Checking for mount directory...
then
  echo "    /usr${i} absent. Making it..."
  mkdir /usr${i}
fi
# Add to /etc/fstab
echo "/dev/${dir}/${d}s${p}	/usr${i} ffs rw 0 0" >> /etc/fstab
if test $s -lt $nSwap
then
  echo "/dev/${dir}/${d}s0	none swap rw,noauto 0 0" >> /etc/fstab
  echo "/dev/${dir}/${d}s1	none swap rw,noauto 0 0" >> /etc/fstab
  s=`expr $s + 2`
fi
;;

2) 
echo "Mounted                  /usr${i}   ${d}"

ERASE=`sed -n ${i}p $list | awk '{print $4}'`
if test "$ERASE" = "erase" -o "$ERASE" = "ERASE"
then
  echo "  unmounting /usr${i} before erasing."
  umount /usr${i}
  DRVSZ=`prtvtoc -h /dev/r${dir}/${d}s${p} | grep "	 3" | awk '{print $5}'`
  DVT=`sed -n ${i}p $list | awk '{print $3}'`	# device type
  echo "  newfs.ffs -s $DRVSZ /dev/${dir}/${d}s${p} $DVT" 
  newfs.ffs -s $DRVSZ /dev/${dir}/${d}s${p} $DVT > /dev/null 2>&1
fi

# Add to /etc/fstab
grep "/usr${i} " /etc/fstab.addexp >> /etc/fstab
if test $s -lt $nSwap
then
  echo "/dev/${dir}/${d}s0	none swap rw,noauto 0 0" >> /etc/fstab
  echo "/dev/${dir}/${d}s1	none swap rw,noauto 0 0" >> /etc/fstab
  s=`expr $s + 2`
fi
;;

3) 
echo "New                      /usr${i}   ${d}"
# reading drive info from the header...
DRVSZ=`prtvtoc -h /dev/r${dir}/${d}s${p} | grep "	 3" | awk '{print $5}'`
DVT=`sed -n ${i}p $list | awk '{print $3}'`	# device type
echo "  newfs.ffs -s $DRVSZ /dev/${dir}/${d}s${p} $DVT" 
newfs.ffs -s $DRVSZ /dev/${dir}/${d}s${p} $DVT > /dev/null 2>&1
if [ ! -d /usr${i} ]
then
  echo "    /usr${i} absent. Making it..."
  mkdir /usr${i}
fi
# Add to /etc/fstab
echo "/dev/${dir}/${d}s${p}	/usr${i} ffs rw 0 0" >> /etc/fstab
if test $s -lt $nSwap
then
  echo "/dev/${dir}/${d}s0	none swap rw,noauto 0 0" >> /etc/fstab
  echo "/dev/${dir}/${d}s1	none swap rw,noauto 0 0" >> /etc/fstab
  s=`expr $s + 2`
fi
;;
esac
#
i=`expr $i + 1`
done

cd /
mount -a
chown bit usr?* 
chgrp bin usr?*
echo "mounted drives are:"
mount
echo
echo "Remember to reboot (type 'init 6') to add swap space!"
