#!/bin/sh
#
# lli          This shell script takes care of starting and stopping
#              LLVM byte-code interpreter
#
# Author:      Al Stone <ahs3@fc.hp.com>
#	       from a script by S.Eranian <eranian@hpl.hp.com>
#
# chkconfig: 2345 80 30
# description: install the binary_fmt module and also make sure the LLVM
#	       interpreter is configured properly
# processname: 
# config: 
# pidfile:

fmt_base=/proc/sys/fs/binfmt_misc/
interp=/usr/bin/lli

function verify_binfmt_mnt 
{
   if test -d $fmt_base; then
      /sbin/modprobe binfmt_misc
   fi
   mntd=$( mount | grep binfmt_misc )
   if test "$mntd"X = X; then
      mount -t binfmt_misc binfmt_misc $fmt_base
   fi
}

# See how we were called.
case "$1" in
  start)
	echo -n "Installing LLVM byte-code interpreter: "
	verify_binfmt_mnt
	echo ":llvc:M::llvc::$interp:" >$fmt_base/register
	echo done
	;;

  stop) 
	echo -n "Removing LLVM byte-code interpreter: "
  	if test -f $fmt_base/llvc; then
	   echo -1 >$fmt_base/llvc
	fi
	echo done
	;;

  restart)
	echo -n "Restarting LLVM byte-code interpreter: "
	verify_binfmt_mnt
	if [ -f $fmt_base/llvc ]; then
	   echo -1 >$fmt_base/llvc
	fi
	echo ":llvc:M::llvc::$interp:" >$fmt_base/register
	echo done
	;;

  status)
	if [ ! -d $fmt_base ]; then
		echo binfmt_misc support not present
		exit 1
	fi

	echo binfmt_misc status is `cat $fmt_base/status`

	if [ ! -f $fmt_base/llvc ]; then
		 echo LLVM byte-code interpreter not configured
		 exit 1
	fi

	cat $fmt_base/llvc
	;;

  force-reload)
	/etc/init.d/lli stop
	/etc/init.d/lli start
	;;
  *)
	echo "Usage: lli {start|stop|restart|status}"
	exit 1
esac

exit 0

