#!/bin/sh
#
# 08/01
# - tmpfs ramdisk is now 48m
#
# 07/14
# - added mkdirs at start
#
# 06/04
# - remount root as rw just in case
#
# 05/04
# - replaced /mnt/groovy-stuff with $GROOVY
#
# 04/30
# - if your CD was created by Mondoarchive when it backed up
#   to ISO(s) then re-jig tmp/mondo-restore.cfg accordingly;
#   this is nec. if you backup to a hard disk directory but
#   then burn the ISOs to physical CDs
#
# 04/26
# - offer user the chance to specify another tape device, if
#   the one they specified at backup-time refuses to act as 
#   aux data disk at boot-time
#
# 04/21
# - start scsi if cciss
# - use ataraid.tgz too
#
# 05/03
# - mount / -o rw,remount at end, just in case
#   RH9 futzes with the mountpoint
#
# 04/04
# - increased tmpfs ramdisk size from 34m to 38m
#
# 01/01 - 01/31/2003
# - fixed issue with LVM and SW Raid devices (Brian Borgeson)
# - friendlier 'upgrade your ram' msg
# - better fallback msg
# - if /tmp/2880.siz exists then assume boot CD
#
# 11/01 - 12/31/2002
# - updated intro msg
# - if /dev/boot_device and /BOOTLOADER.DEVICE exist then
#   copy device /dev/boot_device to pathname contained
#   in /BOOTLOADER.DEVICE text file
#
# 04/01 - 08/31/2002
# - handle new 'backup_media_type' entry in mondo-restore.cfg
# - added 'sleep 2' at start of WelcomeMessage
# - disabled a silly 'abort' line at end
# - better handling of devfs (DuckX) --- new RunDevfsd()
# - assume start-nfs is in /sbin
# - extract rd.tgz, raw.tgz, ida.tgz, cciss.tgz to /; they include
#   /dev entries which would otherwise fill up the ramdisk at bkp-time
#
# 01/01 - 03/31/2002
# - Carl Wilhelm Soderstrom disabled exec sh; replaced with exit 1
# - if you are an idiot & your computer has very little RAM,
#   this script will no longer abort; it will merely warn you
# - run chmod 755 `which start-nfs` before running start-nfs
# - use a ramdisk for /tmp (after copying old /tmp stuff into it)
# - fixed segfaults ('open') by replacing 'sh' call with '/bin/sh' call
# - changed LaunchTerminals() to try to work with newly-compiled 'open'
# - don't write 'start-nfs error' if start-nfs doesn't exist; just go on
# - added tmpfs and some space-checking
# - cleaned up script
# - added stuff to start NFS services if necessary
# - reordered some of the main() stuff, for elegance's sake
# - added Petris and 'tail -f /tmp/mondo-restore.log' to LaunchTerminals()
# - changed /usr/share/ to /usr/local/
#
# 2001
# - fix some typos
# - added lots of cool stuff to make Mindi _nearly_ boot from tape
# - restructured the welcome msg echoed to screen
# - broke up spaghetti code; made lots of subroutines
#
#------------------------------------------------------------


ConfigureLoggingDaemons() {
    echo -en "Running klogd..."
    klogd -c 1 > /dev/null 2> /dev/null
    echo -en "Done.\nRunning syslogd..."
    syslogd > /dev/null 2> /dev/null
    echo "Done."
    LogIt "klogd and syslogd have been started."
}



CopyBootDevEntry() {
    local outfile devfile
    devfile=/dev/boot_device
    outfile=`cat /BOOTLOADER.DEVICE 2> /dev/null`
    [ ! "$outfile" ] && return 0
    echo -en "Copying boot device to $outfile..."
    if [ -e "$outfile" ] ; then
	echo "OK."
	return 0
    fi
    mkdir -p $outfile
    rmdir $outfile
    cp -pRdf $devfile $outfile
    if [ "$?" -ne "0" ] ; then
	echo "Failed."
	return 1
    else
	echo "OK."
	return 0
    fi
}



Die() {
    LogIt "Fatal error! $1" 1
    exit 1
}



ExtractDevTarballs() {
    cd /
    for fname in ataraid.tgz ida.tgz rd.tgz raw.tgz cciss.tgz nst.tgz ; do
        if [ ! -e "/$fname" ] ; then
            LogIt "/$fname not found; cannot extract to /." 1
        else
            echo -en "\rExtracting /$fname...     "
            tar -zxf /$fname || LogIt "Error occurred while extracting /$fname"
        fi
    done
    echo -en "\r"
    LogIt "Extracted additional /dev entries OK.     " 1
}



LaunchTerminals() {
    open -c 2 /bin/sh
    open -c 3 /bin/sh
    open -c 4 /bin/sh
    open -c 5 /bin/sh
    open -c 6 /bin/sh
    open -c 7 -- /bin/sh /sbin/wait-for-petris
    open -c 8 -- /usr/bin/tail -f /tmp/mondo-restore.log
}


LoadKeymap() {
    local fname
    fname=`cat /tmp/KEYMAP-LIVES-HERE 2> /dev/null`
    [ "$fname" = "" ] && return
    if which loadkeys > /dev/null 2> /dev/null ; then
        loadkeys $fname
# loadkmap != loadkeys
#    elif which loadkmap > /dev/null 2> /dev/null ; then
#        loadkmap < $fname
    else
        LogIt "Using default keyboard map." 1
    fi
}


HandleTape() {
    local res
    tapedev=`cat /tmp/mondo-restore.cfg | grep media-dev | tr -s ' ' ' ' | cut -d' ' -f2`
    cd $GROOVY
    tar -zxf $tapedev
    res=$?
    while [ "$res" -ne "0" ] ; do
        LogIt "$tapedev failed to act as extended data disk for booting." 1
        LogIt "Please specify an alternate tape device," 1
        LogIt "or hit <Enter> to boot from CD/floppies." 1
        echo -en "---> "
        read tapedev
        if [ "$tapedev" ] ; then
            LogIt "User specified $tapedev instead"
            tar -zxf $tapedev
            res=$?
        else
            LogIt "User opted not to specify an alternate tapedev"
            res=1
	    break
        fi
    done

    if [ "$res" -ne "0" ] ; then
	cd /
	LogIt "Failed to use tape as extended datadisk. Reverting to floppies." 1
	HandleCDROMorFloppies
	res=$?
    else
	clear
	LogIt "Using tape as extended datadisk. Good." 3
	echo "Using tape as extd ddisk." > /tmp/TAPEDEV-HAS-DATA-DISKS
	res=0
        CD_MOUNTED_OK=yes
    fi
    return $res
}



HandleCDROMorFloppies() {
    find-and-mount-cdrom
    res=$?
    if [ "$res" -eq "0" ] ; then
        clear
	LogIt "OK, I am running on a CD-ROM. Good." 3
        CD_MOUNTED_OK=yes
    elif [ "$res" -eq "1" ] ; then
        clear
	LogIt "OK, I am running on floppies. Good." 3
	CD_MOUNTED_OK=""
    else
	LogIt "OK, I am falling back to floppy mode." 3
        LogIt "That means you'll have to copy the data disk images from" 3
        LogIt "the CD/hard disk/whatever to physical 1.44MB disks and" 3
        LogIt "insert them, one after the other. Please see the manual." 3
        LogIt "The images are in /images on the CD, or /root/images/mindi" 3
        LogIt "on your hard disk. Your archives are probably fine but" 3
        LogIt "your tape streamer and/or CD-ROM drive are eccentric. :-)" 3
	CD_MOUNTED_OK=""
    fi
    return 0
}


HowMuchFreeSpaceOnRamdisk() {
   df -m | grep /dev/ram | head -n1 | tr -s '\t' ' ' | cut -d' ' -f4
}



InsertEssentialModules() {
    for j in 1 2 3 4 ; do
        for i in `ls /*.o* 2> /dev/null` ; do
            [ -f "$i" ] && insmod $i > /dev/null 2> /dev/null
        done
    done
}



PauseForRaids() {
    if [ "`dmesg | grep -i "RAID Controller"`" != "" ] || [ "`dmesg | grep -i "Vendor: 3ware"`" != "" ] ; then
        LogIt "RAID controller(s) detected. Pausing 10 seconds to let them warm up." 1
        echo -en "Pausing..."
        for i in 1 2 3 4 5 6 7 8 9 10 ; do
            sleep 1
            echo -en "$(($i*10))%..."
        done
        echo "Done."
    fi
}


# 06/13/2002 --- DuckX's & Hugo's new routine
RunDevfsd() {
    loc=`which devfsd 2> /dev/null`
    if [ "$loc" != "" ] ; then
	LogIt "Devfs found. Testing kernel support..."
	if [ ! -e "/dev/.devfsd" ] ; then
	    mount -t devfs devfs /dev 2>> /tmp/mondo-restore.log
            if [ "$?" -ne "0" ] ; then
		LogIt "Error while trying to mount devfs"
	    else
		LogIt "Devfs mounted OK"
	    fi
	fi
	#check if the kernel supports devfs
	if [ -e "/dev/.devfsd" ] ; then
	    [ -d "/proc/1" ] || mount -n /proc
	    LogIt "Kernel support found. Starting devfsd"
	    devfsd /dev &
	    sleep 5
	else
	    LogIt "No devfs kernel support."
	fi
    fi
}






#RunDevfsd() {
#    loc=`which devfsd 2> /dev/null`
#    if [ "$loc" != "" ] ; then
#        LogIt "Starting devfsd"
#        devfsd /dev &
#        sleep 5
#    fi
#}



SpaceTests() {
    [ -e "/tmp/filelist.full" ] && cp /tmp/filelist.full /tmp/FLF
    if [ "`HowMuchFreeSpaceOnRamdisk`" -le "3" ] ; then
        LogIt "Ramdisk is a bit smaller than I would like." 1
        LogIt "Please re-run Mondo/Mindi but edit /usr/local/mindi first" 1
        LogIt "and set EXTRA_SPACE to something high, e.g. 8000" 1
        Die "Aborting. Please reboot."
    fi
    rm -f /tmp/FLF
}



StartLvms() {
    if [ -e "/tmp/i-want-my-lvm" ] ; then
        LogIt "Scanning LVM's..." 1
        vgscan
        cat /tmp/i-want-my-lvm | grep "# vgchange" | sed s/#// > /tmp/start-lvm
        chmod +x /tmp/start-lvm
        echo -en "Starting LVM's..."
        /tmp/start-lvm &
        for i in 1 2 3 4 5 ; do
	    echo -en "."
	    sleep 1
        done
        echo "Done."
    fi
# If necessary, cannibalize 'analyze-my-lvm'; copy some of its code here,
# pipe vgscan's output, strip it, run 'vgchange' on its output, etc.etc.
    LogIt "LVM's have been started."
}



StartNfs() {
    if which portmap > /dev/null 2> /dev/null ; then
        LogIt "Running portmap"
	portmap
        [ "$?" -eq "0" ] && LogIt "Portmap started OK" || LogIt "Portmap error"
        if [ -e "/sbin/start-nfs" ] ; then
            chmod 755 /sbin/start-nfs
            /sbin/start-nfs
        fi
    fi
}


StartRaids() {
    local raid_devices i
    raid_devices=`cat /tmp/mountlist.txt | grep /dev/md | cut -d' ' -f1`
    for i in $raid_devices ; do
        if cat /proc/mdstat | grep `basename $i` > /dev/null 2> /dev/null ; then
            LogIt "$i is started already; no need to run 'raidstart $i'" 1
        else
	    LogIt "Running 'raidstart $i'" 1
	    raidstart $i
        fi
#        sleep 1
    done
#    [ "$raid_devices" != "" ] && PauseForRaid
}


SwapTheMountExecs() {
    if [ -e "/bin/mount.libc5" ] ; then
        LogIt "Swapping busybox's mount with libc5 mount"
        mv /bin/mount /bin/mount.bb
        mv /bin/mount.libc5 /bin/mount
        sync
    fi
}


TryAgainToFindCD() {
    local res
    [ "`cat /tmp/mondo-restore.cfg | grep "backup_media_type" | grep "cdstream"`" ] && return
    LogIt "Trying to mount CD-ROM a 2nd time..."
    find-and-mount-cdrom --second-try
    res=$?
    if [ "$res" -eq "0" ] ; then
        CD_MOUNTED_OK=yes
        LogIt "CD-ROM drive mounted successfully." 1
    else
        LogIt "I still cannot find or mount the CD-ROM drive, by the way."
    fi
}



UseTmpfs()
{
    local mount_cmd
    echo -en "Mounting /tmp/tmpfs..."
    mkdir -p /tmp/tmpfs
# For technical reasons, some sets are as large as 16MB.
# I am allowing 32MB because selective restore occupies a lot of space.
    mount_cmd="mount /dev/shm -t tmpfs -o size=48m" ; # was 34m until 04/2003
    $mount_cmd /tmp/tmpfs 2>> /tmp/mondo-restore.log
    if [ "$?" -ne "0" ] ; then
        LogIt "Failed. I could not run '$mount_cmd /tmp/tmpfs'. Your kernel is BROKEN or you do not have enough RAM." 1
        umount /tmp/tmpfs > /dev/null 2> /dev/null
	rmdir /tmp/tmpfs
        ln -sf /mnt/RESTORING/tmp /tmp/tmpfs; # used by mondo-restore
	LogIt "Failed to mount /tmp/tmpfs; using ugly softlink instead"
    else
        echo -en "Pivoting /tmp..."
        umount /tmp/tmpfs
        mkdir -p /tmp.old
        mv -f /tmp/* /tmp.old/
        $mount_cmd /tmp
        mv /tmp.old/* /tmp/
        rmdir /tmp.old
        mkdir -p /tmp/tmpfs
        mkdir -p $GROOVY
	echo "Done."
	LogIt "Successfully mounted dynamic /tmp ramdisk"
    fi
}


WelcomeMessage()
{
    echo "********************************************************************"
    echo "MINDI-LINUX by Hugo Rabson --- web site: http://www.mondorescue.org"
    echo "Boot disk based on AlfaLinux & Trinux. Sbminst by Suzhe & Lonius."
    echo "BusyBox by Erik Andersen. LZO and LZOP by Marcus Oberhumer."
which petris > /dev/null 2> /dev/null && echo "Petris was written by Peter Seidler <p.seidler@mail1.stofanet.dk>."
    echo "Executables and source code are covered by the GNU GPL. No warranty."
    echo "********************************************************************"
}


EnableCcissIfAppropriate() {
    local i fname
    for i in 0 1 2 3 ; do
	fname="/proc/driver/cciss/cciss$i"
	if [ -e "$fname" ] ; then
	    LogIt "Engaging $fname"
	    echo "engage scsi" > $fname
	    LogIt "...result=$?"
	fi
    done
}



# ------------------------ main -----------------------


PATH=/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/mondo:/usr/local/mondo:/usr/local/mindi:.
GROOVY=/tmp/groovy-stuff
export PATH GROOVY

mount -o remount rw /
[ ! "$GROOVY" ] && Die "I'm not groovy!"
for i in $GROOVY /mnt/floppy /mnt/cdrom /mnt/isodir ; do
    mkdir -p $i
done
/bin/update
mount /proc/ /proc -v -t proc 
rm -f /foozero
ExtractDevTarballs
LaunchTerminals
InsertEssentialModules
EnableCcissIfAppropriate
#-------------------------------
#WHOLIVESINAPINEAPPLEUNDERTHESEA#;# --- don't touch this :)
#-------------------------------
UseTmpfs
if [ ! -e "/tmp/mondo-restore.cfg" ] ; then
    LogIt "Warning - /tmp/mondo-restore.cfg not found"
fi
if [ -e "/tmp/2880.siz" ] ; then
    HandleCDROMorFloppies
elif [ "`cat /tmp/mondo-restore.cfg | grep tape`" ] || [ "`cat /tmp/mondo-restore.cfg | grep udev`" ] ; then
    HandleTape
else
    HandleCDROMorFloppies
fi
res=$?
insert-all-my-modules > /dev/null &
sleep 2
install-additional-tools
SwapTheMountExecs
ConfigureLoggingDaemons
insert-all-my-modules &
sleep 4
#RunDevfsd
PauseForRaids
StartRaids
StartLvms
StartNfs
CopyBootDevEntry
LoadKeymap
mkdir -p /tmp/tmpfs
sleep 2
clear
WelcomeMessage
# SpaceTests; # Mandrake Linux 8.1 doesn't like this
[ -e "/tmp/mountlist.txt" ] && cp -f /tmp/mountlist.txt /tmp/mountlist.original

res="`cat /mnt/cdrom/archives/THIS-CD-NUMBER 2> /dev/null`"
[ "$res" != "1" ] && [ "$res" != "" ] && Die "This is CD #$res in the series. Please insert CD #1."
[ "$CD_MOUNTED_OK" != "yes" ] && TryAgainToFindCD
#mount | grep cdrom > /dev/null 2> /dev/null || mount /dev/cdrom /mnt/cdrom >> $LOGFILE 2>> $LOGFILE
hack-cfg-if-necessary || LogIt "Cannot find hack-cfg-if-necessary"

#-------------------------------
#ABSORBENTANDYELLOWANDPOROUSISHE#;# --- don't touch this either :)
#-------------------------------

LogIt "Calling post-init $mountlist"
post-init
echo -en "To reboot, press CTRL-ALT-DEL together.\r"
umount /mnt/cdrom 2> /dev/null
mount / -o rw,remount > /dev/null 2> /dev/null
exec sh
