#! /bin/bash

##
## mkofboot creates filesystem and uses ybin to install yaboot.
## Copyright (C) 2000 Ethan Benson
##
## This program is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License
## as published by the Free Software Foundation; either version 2
## of the License, or (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
##

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
PRG=`basename $0`
CONF=/etc/ybin.conf
ERR="Error in $CONF"

## check for existence of a configuration file, and make sure we have
## read permission.

confexist()
{
    if [ ! -f "$CONF" ] ; then
	echo 1>&2 "$PRG: $CONF: File not found"
	return 1
    elif [ ! -r "$CONF" ] ; then
	echo 1>&2 "$PRG: $CONF: Permission denied"
	return 1
    else 
	return 0
    fi
}

## check to make sure the configuration file is sane and correct.
## maybe this is an insane ammount of error checking, but I want to
## make sure nothing unexpected ever happens.  and i just like useful
## errors from programs.  every error just marks an error variable so
## we give the user as much info as possible before we abandon ship.

checkconf()
{
    if [ ! -e "$boot" ] ; then
	echo 1>&2 "$PRG: $boot: File not found"
	CONFERR=1
    elif [ ! -b "$boot" -a ! -f "$boot" ] ; then
	    echo 1>&2 $"$PRG: $boot: Not a regular file or block device"
	    CONFERR=1
    elif [ ! -w "$boot" -o ! -r "$boot" ] ; then
	echo 1>&2 "$PRG: $boot: Permission denied"
	CONFERR=1
    fi

    if [ ! -f "$bootfile" ] ; then
	echo 1>&2 "$PRG: $bootfile: File not found"
	CONFERR=1
    elif [ ! -r "$bootfile" ] ; then
	echo 1>&2 "$PRG: $bootfile: Permission denied"
	CONFERR=1
    fi

    if [ ! -f "$bootconf" ] ; then
	echo 1>&2 "$PRG: $bootconf: File not found"
	CONFERR=1
    elif [ ! -r "$bootconf" ] ; then
	echo 1>&2 "$PRG: $bootconf: Permission denied"
	CONFERR=1
    fi

    if [ "$wrapper" ] ; then
	if [ ! -f "$wrapper" ] ; then
	    echo 1>&2 "$PRG: $wrapper: File not found"
	    CONFERR=1
	elif [ ! -r "$wrapper" ] ; then
	    echo 1>&2 "$PRG: $wrapper: Permission denied"
	    CONFERR=1
	fi
    fi

    case "$usemount" in 
	yes|no)
	  ;;
	*)
	  echo 1>&2 "$PRG: $ERR: \`usemount' must be either \`yes' or \`no'"
	  CONFERR=1
	  ;;
    esac

    case "$fstype" in
	hfs|msdos)
	 ;;
	*)
	 echo 1>&2 "$PRG: $ERR: \`fstype' must be either \`hfs' or \`msdos'"
	 CONFERR=1
	 ;;
    esac

    ## if we are not using HFS filesystems we don't care about HFS
    ## specific config options.

    if [ "$fstype" = hfs ] ; then
	if [ `printf $hfstype | wc -c` != 4 ] ; then
	    echo 1>&2 "$PRG: $ERR: \`hfstype' must be 4 characters"
	    CONFERR=1
	fi
	
	if [ `printf $hfscreator | wc -c` != 4 ] ; then
	    echo 1>&2 "$PRG: $ERR: \`hfscreator' must be 4 characters"
	    CONFERR=1
	fi

	if [ "$hide" ] ; then
	    case "$hide" in 
	       yes|no)
		;;
	       *)
		echo 1>&2 "$PRG: $ERR: \`hide' must be either \`yes' or \`no'"
		CONFERR=1
	        ;;
	    esac
	fi

	case "$bless" in 
	    yes|no)
	     ;;
	    *)
	     echo 1>&2 "$PRG: $ERR: \`bless' must be either \`yes' or \`no'"
	     CONFERR=1
	     ;;
	esac
	
	case "$kludge" in 
	    yes|no)
	     ;;
	    *)
	     echo 1>&2 "$PRG: $ERR: \`kludge' must be either \`yes' or \`no'"
	     CONFERR=1
	     ;;
	esac

	if [ "$kludge" = yes ] ; then
	    BTFILE=`basename $bootfile`
	    CFFILE=`basename $bootconf`
	    if [ ! -f $kludgedir/$BTFILE -o ! -f $kludgedir/$CFFILE ] ; then 
		echo 1>&2 "$PRG: Error kludge files missing, see README file"
		CONFERR=1
	    fi
	fi
    fi

    ## protect works for both dosfs and hfs

    if [ "$protect" ] ; then
	case "$protect" in 
	    yes|no)
	     ;;
	    *)
	     echo 1>&2 "$PRG: $ERR: \`protect' must be either \`yes' or \`no'"
	     CONFERR=1
	     ;;
	esac
    fi

    if [ "$CONFERR" = 1 ] ; then 
	return 1
    else 
	return 0
    fi
}


## make sure the hfsutils we need are installed and accessable.

checkhfsutils()
{
    which hformat > /dev/null && [ -x `which hformat` ]
    if [ $? != 0 ] ; then
	return 2
    fi

    which hmount > /dev/null && [ -x `which hmount` ]
    if [ $? != 0 ] ; then 
	return 1
    fi

    which humount > /dev/null && [ -x `which humount` ]
    if [ $? != 0 ] ; then
	return 1
    fi

    which hcopy > /dev/null && [ -x `which hcopy` ]
    if [ $? != 0 ] ; then
	return 1
    fi

    which hattrib > /dev/null && [ -x `which hattrib` ]
    if [ $? != 0 ] ; then 
	return 1
    fi

    return 0
}

## make sure we can find ybin.

which ybin > /dev/null && [ -x `which ybin` ] 
if [ $? != 0 ] ; then
    echo 1>&2 "$PRG: ybin cannot be found."
    exit 1
fi

confexist
if [ $? != 0 ] ; then
    exit 1
else 
    source $CONF
fi

## make people RTFM

if [ "$boot" = unconfigured ] ; then
    echo 1>&2 "$PRG: Oops you forgot to properly configure $PRG"
    echo 1>&2 "$PRG: Read the README for details on how to do that"
    exit 1
fi

## check config for sanity

checkconf
if [ $? != 0 ] ; then
    exit 1
fi

## make sure what we need is available.

if [ "$usemount" = no ] ; then
    if [ "$fstype" = hfs ] ; then 
        checkhfsutils
        if [ $? != 0 ] ; then
	    echo 1>&2 "$PRG: hfsutils is not installed or cannot be found"
            echo 1>&2 "$PRG: Try usemount=yes if `uname -sr` supports HFS"
            exit 1
        fi
    else 
	echo 1>&2 "$PRG: DOS filesystems can only be handled with mount"
	exit 1
    fi
else
    if [ "$fstype" = hfs ] ; then
	which hformat > /dev/null && [ -x `which hformat` ]
	if [ $? != 0 ] ; then
	    echo 1>&2 "$PRG: hformat is missing, we cannot continue."
	    exit 1
	fi
    else
	which mkdosfs > /dev/null && [ -x `which mkdosfs` ]
	if [ $? != 0 ] ; then
	    echo 1>&2 "$PRG: mkdosfs is missing, we cannot continue."
	    exit 1
	fi
    fi
fi

## make sure we won't need root permission before blowing anything
## away.

if [ "$usemount" = yes ] ; then
    if [ `id -u` != 0 ] ; then
	echo 1>&2 "$PRG: \`usemount' is \`yes' so we can only be run by root."
	exit 1
    fi
fi

## ask permission then create the desired filesystem, unless -f given.

if [ $# = 0 ] ; then
    printf "$PRG: create $fstype filesystem on $boot? (y/N) "
    read ans
else
    case $1 in 
	-f|--force)
	    ans=y
	    ;;
	 *)
	    echo "$PRG: Usage: mkofboot [ -f | --force ]"
	    exit 1
	    ;;
    esac
fi

## check whether to proceed.

case $ans in 
    y|Y)
    if [ "$fstype" = hfs ] ; then
	    mount | grep $boot > /dev/null 
	    if [ $? = 0 ] ; then
	    echo 1>&2 "$PRG: $boot appears to be mounted! aborting."
	    exit 1
	fi
	hformat -l bootstrap $boot > /dev/null
	if [ $? != 0 ] ; then
	    echo 1>&2 "$PRG: HFS filesystem creation failed!"
	    exit 1
	fi
	humount $boot ## otherwise ybin might get confused.
	ybin
	if [ $? != 0 ] ; then
	    echo 1>&2 "$PRG: A ybin failure has occured!"
	    exit 1
	fi
    else
	mount | grep -w $boot > /dev/null
	if [ $? = 0 ] ; then 
	    echo 1>&2 "$PRG: $boot appears to be mounted! aborting."
	    exit 1
	fi
	mkdosfs -n bootstrap $boot > /dev/null
	if [ $? != 0 ] ; then
	    echo 1>&2 "$PRG: DOS filesystem creation failed!"
	    exit 1
	fi
	ybin
	if [ $? != 0 ] ; then
	    echo 1>&2 "$PRG: A ybin failure has occured!"
	    exit 1
	fi
    fi
    ;;
    *)
     echo "$PRG: OK, aborting."
     exit 2
     ;;
esac

exit 0
