#!/bin/bash2
#
# Shell script to start Mac-on-Linux
#
# Usage: startmol [mol-options] [-- [vmlinux] [System.map] ]
#

echo "Mac-on-Linux (C) 1997-2000 Samuel Rydh <samuel@ibrium.se>"

# We look for the mol lib directory in three places,
# ./lib, /usr/lib/mol and /usr/local/lib
# (in that particular order)

MOL_LIB_DIR=""
if [ -d ./lib/pci_roms ] ; then
    MOL_LIB_DIR=`pwd`/lib
elif [ -d /usr/lib/mol ] ; then
    MOL_LIB_DIR=/usr/lib/mol
elif [ -d /usr/local/lib/mol ] ; then
    MOL_LIB_DIR=/usr/local/lib/mol
fi

if [ ! -d "$MOL_LIB_DIR/pci_roms" ] ; then
    echo "The Mac-on-Linux library directory /usr/lib/mol is missing!"
    exit 1
fi
echo "Uses binaries and modules in $MOL_LIB_DIR"
export MOL_LIB_DIR

# Run the mol.sh script if available
if [ -f /etc/mol.sh ] ; then
/etc/mol.sh start
fi

SHEEP_MOD_PATHS="./NetDriver /lib/modules/`uname -r`/net $MOL_LIB_DIR/modules"
MOL_MOD_PATHS="./kernel_module /lib/modules/`uname -r`/misc $MOL_LIB_DIR/modules"

VERS=`cat /proc/version | awk -- '{ print $3 }' | sed s/[a-zA-Z].*// | sed s/\-.*//`
VERS_MAJOR=`echo $VERS | awk -F . -- '{ print $2 }'`
VERS_MINOR=`echo $VERS | awk -F . -- '{ print $3 }'`

# The 2.2.6 kernel is now supported (probably any 2.2 kernel works)
#if [ $VERS_MINOR -le 6 -o $VERS_MAJOR -lt 2 ] ; then
#    echo "*** Warning: Mac-on-Linux preferes a kernel version > 2.2.6 ***"
#    sleep 1;
#fi

# Make sure the mol kernel module is loaded
#
/sbin/lsmod | grep mol > /dev/null

if [ $? != 0 ] ; then
    # Has the kernel Mac-on-Linux support?

    grep mol_interface /proc/ksyms > /dev/null
    if [ $? != 0 ] ; then

	$MOL_LIB_DIR/bin/ismolpatched
	RET="$?"
	if [ $RET == 2 ] ; then
	    echo "Please run 'startmol' as root"
	    exit 1
	fi
	if [ $RET != 0 ] ; then
	    echo "Trying to apply MOL runtime patches."

	    #perhaps we should not use all arguments here
	    $MOL_LIB_DIR/bin/molpatching $*

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

    echo "Trying to load the Mac-on-Linux kernel module."
    echo

    MMOD=""
    for x in $MOL_MOD_PATHS ; do 
	x=$x/mol.o
	if [ -f $x ] ; then
	    MMOD=$x
	    break
	fi
    done
    if [ "$MMOD" = "" ] ; then 
	MMOD=mol
    fi

    if [ $UID != 0 ] ; then
	echo "The Mac-on-Linux kernel module is not loaded. Please run 'startmol' as root"
	exit 1
    fi

    /sbin/insmod -f $MMOD

    if [ $? != 0 ] ; then
	echo
	echo 'An error occured while trying to load the Mac-on-Linux kernel module'
	exit 1;
    fi
fi

# Install the ethernet driver 
/sbin/lsmod | grep sheep_net > /dev/null

if [ $? != 0 -a $UID == 0 ] ; then 
    SHEEP_NET=""
    for x in $SHEEP_MOD_PATHS ; do
	x=$x/sheep_net.o
	if [ -f "$x" ] ; then
	    SHEEP_NET=$x
	    break
	fi
    done
    if [ "$SHEEP_NET" = "" ] ; then 
	SHEEP_NET=sheep_net
    fi
    /sbin/insmod -f $SHEEP_NET
fi

# Detect common misconfigurations

if [ -f /var/lock/mol ] ; then
    ps `cat /var/lock/mol` | grep mol > /dev/null
    if [ $? != 0 ] ; then
	echo "Removing stale lockfile /var/lock/mol"
	rm -f /var/lock/mol
    else 
	echo "Mac-on-Linux is already running with pid `cat /var/lock/mol`"
	echo "according to the lockfile /var/lock/mol."
	exit 1;
    fi
fi

if [ ! -c /dev/sheep_net ] ; then
    echo "The ethernet interface /dev/sheep_net is missing."
    echo "Run 'mknod /dev/sheep_net c 10 198' to create it"
    echo
fi

if [ ! -c /dev/fb0 ] ; then
    echo "The framebuffer device /dev/fb0 is missing."
    echo "Run 'mknod /dev/fb0 c 29 0' to create it"
    exit 1
fi

#if [ "$MOLRC" == "" -a -f molrc ] ; then
#    MOLRC=`pwd`/molrc
#    echo $MOLRC
#fi

export MOLRC
cd $MOL_LIB_DIR
$MOL_LIB_DIR/bin/mol $*

# give MOL time to create the lock file with the appropriate PID
sleep 1

if [ -f /var/lock/mol ] ; then
    MOLPID=`cat /var/lock/mol`
    trap "kill -INT $MOLPID" SIGINT

    while [ -d /proc/$MOLPID ] ; do
	sleep 3;
    done

    # finally run the user script
    if [ -f /etc/mol.sh ] ; then
	/etc/mol.sh stop
    fi
fi
