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

# Normally, the mol library path is hardcoded or obtained from 
# /etc/molrc, but when we are started by root we should also be 
# aware of the case when MOL is started directly from the source
# tree.

MOL_LIB_DIR=""
MOL_BINLIB_DIR=""
if [ $UID -eq 0 ] ; then
    if [ -d "lib/pci_roms" -a -f "Makefile" ] ; then
	MOLRCGET=`pwd`/emulation/molrcget
	MOL_LIB_DIR=`$MOLRCGET -p`
	MOL_BINLIB_DIR="`pwd`/lib"
	MOL_DBG="`pwd`/debugger/deb"
	echo Using binaries in $MOL_BINLIB_DIR and support files in $MOL_LIB_DIR.
    fi
fi
if [ "$MOL_LIB_DIR" == "" ] ; then
    MOLRCGET="molrcget"
    MOL_LIB_DIR=`$MOLRCGET -p`
    MOL_BINLIB_DIR=`$MOLRCGET -P`
    MOL_DBG="moldeb"
    [ $? -ne 0 ] && exit 1
fi
export MOL_LIB_DIR MOL_BINLIB_DIR
cd $MOL_BINLIB_DIR

echo "Mac-on-Linux `$MOLRCGET -V | sed s/\n//`, (C) 1997-2000 Samuel Rydh <samuel@ibrium.se>"

# 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_BINLIB_DIR/modules"
MOL_MOD_PATHS="./kernel_module /lib/modules/`uname -r`/misc $MOL_BINLIB_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

	if [ $VERS_MAJOR -ge 3 ] ; then
	    echo "Please recompile your kernel with Mac-on-Linux support (CONFIG_MOL)"
	    echo "(recent 2.4 kernels should contain MOL support)"
	    exit 1
	fi

	$MOL_BINLIB_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_BINLIB_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

$MOLRCGET -b enable_console_video && if [ ! -f $MOL_LIB_DIR/config/vmodes ] ; 
then
    echo
    echo "*************************************************************"
    echo " No video modes has been configured. Please run 'molvconfig'"
    echo " as root to setup full screen video or disable console"
    echo " video in the /etc/molrc file"
    echo "*************************************************************"
    exit 1
fi

cd $MOL_BINLIB_DIR

if $MOLRCGET -b debug_stop -- $* ; then
    $MOL_BINLIB_DIR/bin/mol $* > /dev/null >& /dev/null
    $MOL_DBG $*
    kill -HUP `cat /var/lock/mol`
    exit 0
else
    $MOL_BINLIB_DIR/bin/mol $*
fi

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

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
