#!/usr/bin/ksh
# Copyright (c) 1991, 1992 by Frank J. Edwards
# See the file COPYRIGHT in this distribution for copyright details.

# $Id: addados,v 1.6 1992/11/23 03:27:58 root Exp root $

# This script reads the output of the rdb command on the given
#  device(s) and pulls out a list of filesystems which are AmigaDOS
#  formatted.
# It creates the vfstab entry using a directory name which corresponds
#  to the volume label output by the rdb command.

# Usage:  addados [-f filename] [device]
#
#  The "-f filename" modifies the given file to contain the entry,
#  with any previous entry removed.  Otherwise, the new entries are
#  merely written to stdout.
#
#  "Device" can be either a full pathname ("/dev/dsk/c6d0s3"),
#  a partition description with the "/dev/dsk/" prefix ("c2d0s2"),
#  or just the controller-device pair ("c0d0").  Whatever format is
#  given, any "/dev/dsk/" prefix is removed first, then any trailing
#  "slice" value.  The resulting controller-device pair has "s0"
#  appended to it, "/dev/dsk/" prepended, and what's left is the
#  device name that's used.

# There's a main loop that walks through the device names.  For each
#  device, we run rdb -H on each name and scan the output looking
#  for partitions with the Flag setting of 0x444f53?? (DOSx) and
#  add or update those partitions.

# $Log: addados,v $
# Revision 1.6  1992/11/23  03:27:58  root
# Modified usage to add "-f filename" since Keith thought the script
# should not automatically edit /etc/vfstab but only write the new
# entries to stdout.  Then the user could use I/O redirection to append
# the new records to /etc/vfstab or just display them on the screen.
# The "-f" option allows the script to remove any conflicting entry
# where the partition name is the same as an existing one.
#
# Revision 1.5  1992/11/20  04:03:11  root
# Preliminary release (with correct $Id: addados,v 1.6 1992/11/23 03:27:58 root Exp root $ string)

USAGE="Usage:  ${0##*/} [-f filename] [device]"
[[ $# = 0 ]] && { echo "$USAGE"; exit 5; }

VFSTAB=${VFSTAB-/dev/stdout}

# This is the function that edits the vfstab file.
#  If an entry exists for the given input, sed is used to remove it
#  from the $VFSTAB file and then echo tacks on the new entry.

function edit_vfstab {
dev=${1%s?}s$2
if [[ $VFSTAB != /dev/stdout ]]
then
    file=$(sed -e "/^\/dev\/dsk\/$dev/d" $VFSTAB)
    echo "$VFSTAB:  $dev /$3 ados 2 no fb=2,l=$4,ro"
fi
echo "$file\n/dev/dsk/$dev /dev/rdsk/$dev /$3 ados 2 no l=$4,ro" > $VFSTAB
}

IFS=":$IFS"			# Let the colon be eaten up as a delimiter
while (( $# > 0 ))
do
    if [[ "$1" = "-f" ]]
    then
	VFSTAB="$2"
	shift
    else
	base=${1##/dev*/}		# Trim any leading "/dev" pattern
	base=${base%s?}s0		# Make sure the "slice" is 0

#   	echo "Doing device /dev/dsk/$base..."
	rdb -H /dev/dsk/$base | tail +3 |
	    while read party name start size inMB MB mount pri custom flag
	    do
		# Don't touch it unless the flag value is correct, and
		#  the partition doesn't start with "Unix".
		if [[ ${name#Unix} = $name ]]
		then
		    case "$flag" in
		    F0x444f53??)  edit_vfstab $base $party $name $size ;;
		    esac
		fi
	    done
    fi
    shift
done
