#!/bin/sh
#############################################################################
# Copyright (C)2004-2005, Sun Microsystems Inc. All rights reserved.
# Use is subject to license terms.
#
# Name: create_initd_entry
# Description: Common function to create initd entry for the client components

# pragma ident	
##############################################################################

################################################################################
#  Function of the create_initd_entry
#  1.  Make an entry into the /etc/init.d and the appropriate rcX.d for the executable passed in.
#################################################################################

#
# control logging method
#
log=echo
#log="logger -pdaemon.err"

#
#  make an entry of the executable script into the /etc/init.d and the appropriate 
#

#
# This script requires at least one, but no more than two args.
#
if [ $# -lt 1 ] || [ $# -gt 2 ] ; then
    $log "Usage : `basename $0` <executable_script_path> [run_level_file]"
    exit 1
fi

/usr/lib/cc-cfw/framework/lib/get_interface CHECK_CCR > /dev/null
if [ $? -ne 0 ] ; then
    $log "The CCR package installation could not be verified.  Exiting."
    exit 1
fi
cli="/usr/lib/cc-ccr/bin/ccr"

init_script_path=$1

#
# Check the executable path. Is it part of a registered component?
#
echo $init_script_path | grep "/usr/lib/cc-cfw/"
if [ $? -ne 0 ] ; then
    $log "enable : ERROR the executable path ${init_script_path} is for a non-registered component!"
    exit 1
fi

#
# Arg 2 is optional for the runlevel file. Otherwise it defaults to runlevel 3.
#
if [ -n "${2}" ] ; then
    run_level_file=$2
    
    #
    # Check the runlevel file path. Is it part of a registered component?
    #
    echo $run_level_file | grep "/usr/lib/cc-cfw/"
    if [ $? -ne 0 ] ; then
        $log "enable : ERROR the runlevel file ${run_level_file} is for a non-registered component!"
        exit 1
    fi

    run_level_value=`cat $run_level_file`
    if [ $? -ne 0 ] ;  then
        $log "Cannot read the runlevel file ${run_level_file}.  Defaulting to 3"
    fi
fi

if [ -f "$init_script_path" ] ; then
    script_name=`basename $init_script_path`

    ###  Create a symlink in the /etc/init.d to the executable ###
    initdentry=/etc/init.d/$script_name
    ln -s $init_script_path $initdentry
    
    ### Default values for the level and order ###
    slevels="2"
    sorder="99"
    korder="01"
    klevels="01S"

    if [ "${run_level_value}" = "1" ]  ; then
        #Anything other than proxy#
        sorder="97"
        korder="03"
    elif [ "${run_level_value}" = "2" ]  ; then
        sorder="98"
        korder="02"
    fi

    for sl in `echo $slevels | sed 's/\(.\)/\1 /g'`
    do
        # link on the rc scripts
        file="/etc/rc${sl}.d/S${sorder}${script_name}"
        if [ ! -f $file ] ; then
            ln -s $initdentry $file 
        fi
      
    done
    
    for kl in `echo $klevels | sed 's/\(.\)/\1 /g'`
    do
        # link on the rc scripts
        file="/etc/rc${kl}.d/K${korder}${script_name}"
        if [ ! -f $file ] ; then
            ln -s $initdentry $file 
        fi
    done

else
    $log "enable : ERROR cannot find the executable script ${init_script_path}"
    exit 1
fi
