#!/bin/bash
#
# /*
# Copyright (c) 2007, Intel Corp.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
#
#    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
#    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
#    * Neither the name of Intel Corporation nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# */
#
#
# chkconfig: - 35 91 35
# description: Starts and stops the iommapd daemons \
#			   used to provide iommapd services.
#
# config:  /etc/iommapd.conf
#

### BEGIN INIT INFO
# Provides:          iommap
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Example initscript
# Description:       This file should be used to construct scripts to be
#                    placed in /etc/init.d.  This example start a
#                    single forking daemon capable of writing a pid
#                    file.  To get other behavoirs, implemend
#                    do_start(), do_stop() or other functions to
#                    override the defaults in /lib/init/init-d-script.
### END INIT INFO

############################################################################
# Check that iommapd.conf exists.										   #
############################################################################
[ -f /etc/iommapd.conf ] || exit 0

# Source iommap driver configuration.
. /etc/iommapd.conf

# Check that major number is set.
[ -z ${MAJOR} ] && exit 0

# Check that minor number is set.
[ -z ${MINOR} ] && exit 0

# Check that os version is set.
[ -z ${OSVER} ] && exit 0

# Check that driver name is set.
[ -z ${TARGET} ] && exit 0

# Check that os version is set.
[ -z ${AUTO_DETECT} ] && exit 0

############################################################################
# Source function library.												   #
############################################################################
if [ ${OSVER} = "REDHAT" ] ;then
  if [ -f /etc/init.d/functions ] ; then
    . /etc/init.d/functions
  elif [ -f /etc/rc.d/init.d/functions ] ; then
    . /etc/rc.d/init.d/functions
  else
    exit 0
  fi
fi

if [ ${OSVER} = "SUSE" ] ; then
  if [ -f /etc/rc.status ] ; then
	. /etc/rc.status
	rc_reset
  else
    exit 0
  fi
fi

############################################################################
# function																   #
############################################################################
RETVAL=0

if [ ${OSVER} = "REDHAT" ] ; then
  CMD="daemon"
#elif [ ${OSVER} = "UBUNTU" ] ; then
#  CMD="daemon"
else
  CMD=""
fi

TARGET_OUTPUT=/lib/modules/`uname -r`/iMTA/iommap

############################################################################
# Start() function														   #
############################################################################
start() {
    KIND="iommap"

	echo -n $"Starting $KIND services..."
	if [ -e /dev/iommap ] ; then
		$CMD rm -f /dev/iommap
	fi

	if [ -f /proc/iommap ] ; then
		$CMD /sbin/rmmod iommap
	fi
	
	if [ -f /proc/iMTA/iommap ] ; then
		$CMD /sbin/rmmod iommap
	fi

	if [ ! ${OSVER} = "UBUNTU" ] ; then
		if [ ${AUTO_DETECT} = "yes" ] || [ ${AUTO_DETECT} = "YES" ]; then
			$CMD /sbin/insmod -f $TARGET_OUTPUT/$TARGET AutoMajor_detect=1 >/dev/null
		else
			$CMD /sbin/insmod -f $TARGET_OUTPUT/$TARGET >/dev/null
		fi
	else
		if [ ${AUTO_DETECT} = "yes" ] || [ ${AUTO_DETECT} = "YES" ]; then
			$CMD /sbin/insmod $TARGET_OUTPUT/$TARGET AutoMajor_detect=1 >/dev/null
		else
			$CMD /sbin/insmod $TARGET_OUTPUT/$TARGET >/dev/null
		fi
	fi

	MAJOR=`cat /proc/devices | awk '/iommap/{print $1}'`
	MINOR=${MINOR}
	
	echo "MAJOR=$MAJOR" > /etc/iommapd.conf
	echo "MINOR=$MINOR" >> /etc/iommapd.conf
	echo "OSVER=${OSVER}" >> /etc/iommapd.conf
	echo "TARGET=${TARGET}" >> /etc/iommapd.conf
	echo "AUTO_DETECT=${AUTO_DETECT}" >> /etc/iommapd.conf

	$CMD mknod /dev/iommap c ${MAJOR} ${MINOR}
	$CMD chmod 640 /dev/iommap

	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/iommapd || \
	   RETVAL=1
	return $RETVAL
}	

############################################################################
# Stop() function														   #
############################################################################
stop() {
    KIND="iommap"

	echo -n $"Shutting down $KIND services... "

	if [ -e /dev/iommap ] ; then
		$CMD rm -f /dev/iommap
	fi

	if [ -f /proc/iommap ] ; then
		$CMD /sbin/rmmod iommap
	fi
	
	if [ -f /proc/iMTA/iommap ] ; then
		$CMD /sbin/rmmod iommap
	fi

	RETVAL=$?
	[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/iommapd
	echo ""
	return $RETVAL
}	

############################################################################
# Restart() function													   #
############################################################################
restart() {
    echo $"Restarting iommap services... "

	if [ -f /var/lock/subsys/iommapd ] ; then
		stop
		
		if [ -f /proc/iommap ] ; then
		  stop
		fi
		
		if [ -f /proc/iMTA/iommap ] ; then
		  stop
		fi
	fi

	if [ ! -f /var/lock/subsys/iommapd ] ; then
		start
		if [ ! -f /proc/iommap ] ; then
			if [ ! -f /proc/iMTA/iommap ] ; then
			  start
			fi
		fi
	fi
}	

############################################################################
# Reload() function														   #
############################################################################
reload() {
    echo $"Reloading iommap.conf file... "
	restart
	
	MAJOR=`cat /proc/devices | awk '/iommap/{print $1}'`
	MINOR=0
	
	echo "MAJOR=$MAJOR" > /etc/iommapd.conf
	echo "MINOR=$MINOR" >> /etc/iommapd.conf
	echo "OSVER=${OSVER}" >> /etc/iommapd.conf
	echo "TARGET=${TARGET}" >> /etc/iommapd.conf
	echo "AUTO_DETECT=${AUTO_DETECT}" >> /etc/iommapd.conf
	
	restart
	
	RETVAL=$?
	return $RETVAL
}	

############################################################################
# rhstatus() function													   #
############################################################################
rhstatus() {
	if [ -f /var/lock/subsys/iommapd ] ; then
		echo $"iommapd services is started..."
		
		if [ -f /proc/iommap ] ; then
		  cat /proc/iommap
		fi

		if [ -f /proc/iMTA/iommap ] ; then
		  cat /proc/iMTA/iommap
		fi
				
	else
		if [ ${OSVER} = "REDHAT" ] ; then
		  status iommapd
		else
		  echo $"iommapd services is stopped..."
		fi
	fi
}

############################################################################
# link() function for RPM.												   #
############################################################################
link() {
    echo -n $"Linking iommap services to "

	
	if [ ${OSVER} = "UBUNTU" ] ; then
		echo "run level 3 & 5... "
		if [ ! -f /etc/rc3.d/S91iommapd ] ; then
			ln -fs /etc/init.d/iommapd /etc/rc3.d/S91iommapd
		fi

		if [ ! -f /etc/rc5.d/S91iommapd ] ; then
			ln -fs /etc/init.d/iommapd /etc/rc5.d/S91iommapd
		fi
	elif [ ${OSVER} = "REDHAT" ] || [ ${OSVER} = "SUSE" ] ; then
		echo "run level 3 & 5... "
		if [ ! -f /etc/rc.d/rc3.d/S91iommapd ] ; then
			ln -fs /etc/init.d/iommapd /etc/rc.d/rc3.d/S91iommapd
		fi

		#if [ ! -f /etc/rc.d/rc3.d/K35iommapd ] ; then
		#	ln -fs /etc/init.d/iommapd /etc/rc.d/rc3.d/K35iommapd
		#fi

		if [ ! -f /etc/rc.d/rc5.d/S91iommapd ] ; then
			ln -fs /etc/init.d/iommapd /etc/rc.d/rc5.d/S91iommapd
		fi

		#if [ ! -f /etc/rc.d/rc5.d/K35iommapd ] ; then
		#	ln -fs /etc/init.d/iommapd /etc/rc.d/rc5.d/K35iommapd
		#fi
	else
		echo "default run level... "
		if [ ! -f /etc/runlevels/default/iommapd ] ; then
			ln -fs /etc/init.d/iommapd /etc/runlevels/default/iommapd
		fi
	fi

}

############################################################################
# unlink() function for RPM.											   #
############################################################################
unlink() {

	if [ -f /var/lock/subsys/iommapd ] ; then
	  stop
	fi

	echo -n $"Unlinking iommap services from "

	if [ ${OSVER} = "UBUNTU" ] ; then
		echo "run level 3 & 5... "
		if [ -f /etc/rc3.d/S91iommapd ] ; then
			rm -rf /etc/rc3.d/S91iommapd
		fi

		if [ -f /etc/rc5.d/S91iommapd ] ; then
			rm -rf /etc/rc5.d/S91iommapd
		fi
	elif [ ${OSVER} = "REDHAT" ] || [ ${OSVER} = "SUSE" ] ; then
		echo "run level 3 & 5... "
		if [ -f /etc/rc.d/rc3.d/S91iommapd ] ; then
			rm -rf /etc/rc.d/rc3.d/S91iommapd
		fi

		#if [ -f /etc/rc.d/rc3.d/K35iommapd ] ; then
		#	rm -rf /etc/rc.d/rc3.d/K35iommapd
		#fi

		if [ -f /etc/rc.d/rc5.d/S91iommapd ] ; then
			rm -rf /etc/rc.d/rc5.d/S91iommapd
		fi

		#if [ -f /etc/rc.d/rc5.d/K35iommapd ] ; then
		#	rm -rf /etc/rc.d/rc5.d/K35iommapd
		#fi
	else
		echo "default run level... "
		if [ -f /etc/runlevels/default/iommapd ] ; then
			rm -rf /etc/runlevels/default/iommapd
		fi
	fi
}

############################################################################
# version() function.													   #
############################################################################
version() {
	if [ -f /proc/iommap ] ; then
		cat /proc/iommap
	fi
	
	if [ -f /proc/iMTA/iommap ] ; then
		cat /proc/iMTA/iommap
	fi	
}

############################################################################
# switch case.															   #
############################################################################

case "$1" in
  start)
  	start
	;;
  stop)
  	stop
	;;
  restart)
  	restart
	;;
  reload)
  	reload
	;;
  status)
  	rhstatus
	;;
  link)
  	link
	;;
  unlink)
  	unlink
	;;
  version)
  	version
	;;	
  *)
	echo $"Usage: $0 {start|stop|restart|reload|status|link|unlink|version}"
	exit 1
esac

if [ ${OSVER} = "REDHAT" ] || [ ${OSVER} = "UBUNTU" ];then
	exit $?
fi

if [ ${OSVER} = "SUSE" ] ;then
	rc_exit
fi
