#!/bin/bash
# Begin /lib/lsb/install_initd

. /lib/lsb/initd_functions

# This script is grossly over commented, but is somewhat complex.
# It is exactly to spec and meets the LSB requirements, and LFS's 
# requirements as I have defined them.  It takes advantage of the 
# GNU make utility to handle dependency tracking, so that note
# might help explain the numerous comments pointing to these
# magical makefiles.

# First, the program should take exactly one argument, the full path of the
# script only! Check to make sure only one argument is passed, if not exit 2
# This is not defined by LSB, but is consistent with the exit code for
# invalid or excess arguments in init scripts.
if test ! -n "${1}" -o ! -z "${2}"
then
    echo ""
    echo "Invalid or excess arguments!"
    echo "Usage: ${0} /etc/init.d/<scriptname>"
    echo ""
    exit 2
fi

case "${1}" in
    -*)
        echo ""
        echo "Usage: ${0} /etc/init.d/<scriptname>"
        echo ""
        exit 0
    ;;
esac

# Next check to make sure that the script really exists
# If it does not, exit 5. Again this is not defined by the LSB, however, is
# consistent with the exit values for init scripts when the program is not 
# installed.

if test ! -f "${1}"
then
    echo ""
    echo "Script does not exist!"
    echo "Usage: ${0} /etc/init.d/<scriptname>"
    echo ""
    exit 5
fi

# Go ahead and get all the information that is needed from disk in one shot
get_headers

# Now get the array index number for the specified script.
# Also note that the 'filename' variable is set (basename of script), and
# the script is verified that it is LSB compliant.
get_index "${1}"
install_index=${index}
unset index

# Check to see if script is already enabled
grep "${filename}" /lib/lsb/scriptlist 2>&1 >/dev/null
if test ${?} -eq 0; then
    echo "Script is already enabled!"
    echo "Check /lib/lsb/scriptlist if you believe this is in error."
    exit 0
else
    checkfind=`find /etc/rc*.d -name ???${filename}`
    if test ! -z ${checkfind}; then
        echo "Script is manually enabled!"
        echo "You will need to remove symlinks prior to activating this script."
        echo "Unable to continue."
        exit 2
    fi
    unset checkfind
fi

# Make sure dependencies are in place
get_required_start 

# Give the user something other than dead time
echo -n "Resolving dependencies (this may take a bit)..."

# Create all of our makefiles
touch /tmp/all.mk
touch /tmp/sysinit.start.mk
for runlevel in 0 1 2 3 4 5 6
do
    touch /tmp/${runlevel}.{start,stop}.mk
done

# Start the toplevel all target
echo -n "all:	" > /tmp/all.mk

# Now determine what scripts are to be run and generate makefiles
for runlevel in sysinit 0 1 2 3 4 5 6
do
    # get the scripts that stop in $runlevel
    # skip for sysinit
    if test "${runlevel}" != "sysinit"; then
        for script in `cat /lib/lsb/scriptlist`
        do
            get_index "${script}"
            get_default_stop 
            echo "${defaultstop}" | grep "${runlevel}" > /dev/null
            if test ${?} -eq 0; then
                stoplist="${stoplist}${script} "
            fi
        done
        unset script
    fi # !sysinit

    # get the scripts that start in $runlevel
    for script in `cat /lib/lsb/scriptlist`
    do
        get_index "${script}"
        get_default_start
        echo "${defaultstart}" | grep "${runlevel}" > /dev/null
        if test ${?} -eq 0; then
            startlist="${startlist}${script} "
        fi
    done
    unset script

    # Now create some temporary makefiles so we can let make, and it's built-in
    # dependency tracking, handle the script order for us.
    # First, find all scripts for each runlevel that should be stoped
    # Skip sysinit for stop links
    if test "${runlevel}" != "sysinit"; then
        for script in ${stoplist}
        do
            # update all.mk
            grep "${script}" /tmp/all.mk >/dev/null
            if test ${?} -ne 0; then
                echo "\\" >> /tmp/all.mk
                echo -n "	${script}	" >> /tmp/all.mk
            fi

            # create the $runlevel.stop.mk
            echo -n "${script}:	" >> /tmp/${runlevel}.stop.mk
            get_index ${script} 
            # get previous stop facilities, and convert lsb facility names
            # into script names and send to makefile for dependency tracking
            get_required_stop
            convert_lsb_required "${requiredstop}"
            echo -n "${reqprovideslist}" >> /tmp/${runlevel}.stop.mk
            get_should_stop
            convert_lsb_should "${shouldstop}"
            echo "${optprovideslist}" >> /tmp/${runlevel}.stop.mk
        done
    fi

    # Now, all scripts that should be started by runlevel
    for script in ${startlist}
    do
        # update all.mk
        grep "${script}" /tmp/all.mk >/dev/null
        if test ${?} -ne 0; then
            echo "\\" >> /tmp/all.mk
            echo -n "       ${script}       " >> /tmp/all.mk
        fi

        # create the $runlevel.start.mk
        echo -n "${script}: " >> /tmp/${runlevel}.start.mk
        get_index ${script}
        # get previous start facilities, and convert lsb facility names
        # into script names and send to makefile for dependency tracking
        # in runlevels 0 and 6 we should verify stops, not starts
        if test "${runlevel}" = "0" -o "${runlevel}" = "6"; then
              get_required_stop
              convert_lsb_required "${requiredstop}"
        else
              get_required_start 
              convert_lsb_required "${requiredstart}"
        fi
        echo -n "${reqprovideslist}" >> /tmp/${runlevel}.start.mk
        # Again in runlevels 0 and 6 get stop prereqs
        if test "${runlevel}" = "0" -o "${runlevel}" = "6"; then
            get_should_stop
            convert_lsb_should "${shouldstop}"
        else
            get_should_start
            convert_lsb_should "${shouldstart}"
        fi
        echo "${optprovideslist}" >> /tmp/${runlevel}.start.mk
    done
    unset startlist
    unset stoplist
done

# Now that dependencies are handled completely for each script, fix the startup
# makefiles for items who are already started during sysinit and items stopped
# in runlevels 0 and 6.
for runlevel in 1 2 3 4 5
do
    # Get a list of scripts started in sysinit
    for script in `sed -e 's/:.*$//' /tmp/sysinit.start.mk`
    do
        sed -i "s@${script}@@" /tmp/${runlevel}.start.mk
    done
done

# Modify runlevels 0 and 6 start scripts to remove items already stopped
for runlevel in 0 6
do
    # get a list of scripts stopped in $runlevel
    for script in `sed -e 's/:.*$//' /tmp/${runlevel}.stop.mk`
    do
        # and remove them from the deps in the start script
        sed -i "s@${script}@@" /tmp/${runlevel}.start.mk
    done
done


# Finish our statement above...
echo "Completed!"

## EITHER USE MAKE, OR DO IT IN BASH HERE!!!!!!


# Okay script is valid so it's okay to add it to the list
## Commented out for now
#echo `echo "${1}" | sed 's@/etc/init.d/@@'` >> /lib/lsb/scriptlist

# Clean up our mess
## Commented out for now
#echo -n "Cleaning up..."
#for runlevel in 0 1 2 3 4 5 6
#do
#    rm /tmp/${runlevel}.start.mk 2>/dev/null
#    rm /tmp/${runlevel}.stop.mk 2>/dev/null
#done
#rm /tmp/sysinit.start.mk 2>/dev/null
#rm /tmp/all.mk 2>/dev/null
#echo "Completed!"

