#!/bin/sh

CHRP=0

doDone() {
        echo "*****************************************************************"
        echo "* The Quik installer script was not able to find a partition    *"
        echo "* to install into.  This may be due to an error or if you are   *"
        echo "* installing onto a drive other than /dev/sda quik errs on the  *"
        echo "* side of caution when installing and doesn't not write itself   *"
        echo "* out to disk.  If you do want to finish the install you can do *"
        echo "* it by hand with: dd if=/boot/second of=/dev/XXX where XXX is  *"
        echo "* the partition you want to install the bootloader into.        *"
        echo "*****************************************************************"
        exit 0
}

if [ ! -z "`grep CHRP /proc/cpuinfo`" ] ; then
        echo CHRP system detected.
	CHRP=1
#       PART=`fdisk -l | fgrep "PPC PReP"`
        PART=`cfdisk -P s | fgrep "PPC PReP"`
        if [ -z "$PART" ] ; then
            echo "*********************************************************"
            echo "* You must create a PPC PReP Boot partition (type 0x41) *"
            echo "* for the CHRP bootloader to be installed.              *"
            echo "*********************************************************"
	    doDone
        fi
        if [ `echo "$PART" | wc -l` != 1 ] ; then
            echo "**************************************************************"
            echo "* There are more than 1 PPC PReP Boot partitions (type 0x41) *"
            echo "* on this system.  Aborting install rather than picking one. *"
            echo "**************************************************************"
            doDone
        fi
        if [ -z "`echo "$PART" | grep "Boot (80)"`" ] ; then
            echo "***************************************************************"
            echo "* The PPC PReP Boot partition (type 0x41) is not marked as    *"
            echo "* bootable.  You will need to mark this partition as bootable *"
            echo "* before Quik can be installed onto it.                       *"
            echo "**************************************************************"
            doDone
        fi
        P=`echo "$PART" | awk "{print $1}"`
        # assume /dev/sda!!!
        if [ "${P}" != "1" ] ; then
            echo "***************************************************************"
            echo "* You do not have sda1 setup as the boot partition.  This     *"
            echo "* will work but Quik will not know where the configuration    *"
            echo "* file is.                                                    *"
            echo "***************************************************************"
        fi
        P=/dev/sda${P}
        echo Installing onto $P
        dd if=/boot/second of=$P
fi

if [ $CHRP = "0" ] ; then
        echo "***************************************************************"
	echo "* Quik should only be used on CHRP-based PowerPC systems. If  *"
	echo "* you are on a Macintosh system, please consider using yaboot *"
	echo "* or miBoot instead.					    *"
	echo "***************************************************************"
fi  
	

