#!/bin/sh
#
# $Id: service_get_deps,v 1.2 2005/12/13 16:30:15 gleissner Exp $
#
#
# Project     :  SCPM (System Configuration Profile Management)
# Module      :  SCPM utilities
# File        :  service_get_deps
# Description :  Spits out service dependencies on given Resource type
# Author      :  Joachim Gleissner <jg@suse.de>
#
# Copyright 2001 SuSE GmbH
#
# Released under the terms of the GNU General Public License
# (see file COPYRIGHT in project root directory).
#

. /etc/sysconfig/scpm || exit 1
. $LIBDIR/resource_types/service/common || exit 1

# files that should not be handled as dependencies for other resources
# otherwise this could lead to unnecessary restarts
EXCLUDE_FILE_DEPS="/etc/DIR_COLORS /etc/csh.cshrc /etc/csh.login /etc/init.d/boot \
/etc/init.d/halt /etc/init.d/powerfail /etc/init.d/random /etc/init.d/single \
/etc/inittab /etc/inputrc /etc/insserv.conf /etc/ld.so.conf /etc/mailcap \
/etc/mime.types /etc/modules.conf /etc/motd /etc/netgroup /etc/nsswitch.conf \
/etc/permissions /etc/permissions.easy /etc/permissions.paranoid /etc/permissions.secure \
/etc/ppp/ip-down /etc/ppp/ip-up /etc/profile /etc/rc.status /etc/shells /etc/ttytype \
/etc/zshrc /root/.exrc /root/.xinitrc /sbin/quick_halt /etc/issue* /etc/logfiles"

# dito, for services
EXCLUDE_SERVICE_DEPS="boot random serial"

# when spitting out file dependencies, we suppress exclude files
FILE_EXCLUDE_PATTERN="$FILE_EXCLUDE_PATTERN $( cat $LIBDIR/resource_types/file/exclude 2>/dev/null )"

show_usage()
{
    echo "$0 [options] init_script resource_type"
    echo
    echo "options may be"
    echo
    echo "-h   shows help (this one)"
}

resolv_service_var()
{
    test -z "$1" && return 0
    # strip possible + sign
    r=${1#+*}
    vars=`grep ^$r /etc/insserv.conf | cut -f2- `
    test -z "$vars" && { echo $r ; return 0 ; }
    for i in $vars ; do
	resolv_service_var $i
    done
}

filter_excludes()
{
    GLOBIGNORE="*"
    test -z "$FILE_EXCLUDE_PATTERN" && { cat ; return; }

    while read FILE ; do
	hit=no
	for i in $FILE_EXCLUDE_PATTERN ; do
	    test -z ${FILE/"$i"/} && { hit=yes; continue; }
	done
	test $hit = no && echo $FILE
    done
}

while test -n "$1" ; do
    case "$1" in
        -h)
	show_usage
	exit 0
	;;

	*)
	if [ -n "$SERVICE" ]; then
	    test -n "$RESTYPE" && { echo "unknown option $1" >&2 ; exit 1 ; }
	    RESTYPE=$1
	else
	    SERVICE=$1
	fi
	;;
    esac
    shift
done

test -z "$SERVICE" && { echo "No service given" >&2 ; exit 1 ; }
test -z "$RESTYPE" && { echo "No resource type given" >&2 ; exit 1 ; }
test -f $INITD/$SERVICE || { echo "Can not access $INITD/$SERVICE" >&2 ; exit 1 ; }

case $RESTYPE in

    service|rcservice)
    DEPS_RAW=$( grep -E "^# Required-Start:" $INITD/$SERVICE | cut -d':' -f 2 )
    test -z "$DEPS_RAW" && exit 0

    DEPS=$( for j in $DEPS_RAW ; do resolv_service_var $j ; done )

    if [ -n "$DEPS" ]; then
	for k in $DEPS
	do 
	    excl="no"
	    for i in $EXCLUDE_SERVICE_DEPS ; do
		test "$i" = "$k" && excl="yes"
	    done
	    test "$excl" = "no" && echo $k
	done | filter_excludes
    fi
    ;;

    file)
    PACKAGE=$( rpm -qf $INITD/$SERVICE )
    test -z "$PACKAGE" && { echo "Could not determine package name" >&2 ; exit 1 ; }
    DEPS=$( rpm -ql --configfiles $PACKAGE )
    # sysconfig hack here
    SYSCONFIG_DEPS=$( grep -E "/etc/sysconfig/[[:alnum:]]*" $INITD/$SERVICE 2>/dev/null |\
	sed -e 's/\(.*\)\(\/etc\/sysconfig\/[[:alnum:]]*\)\(.*\)/\2/g' |\
	sort | uniq )
    if [ -n "$DEPS" ]; then
	for k in $DEPS $SYSCONFIG_DEPS
	do 
	    excl="no"
	    for i in $EXLUDE_FILES $EXCLUDE_FILES_DEPS ; do
		test "$i" = "$k" && excl="yes"
	    done
	    test "$excl" = "no" && echo $k
	done | filter_excludes | sort | uniq
    fi
    ;;

    sysconfig)
    ;;

    *)
    echo "Unknown resource type $RESTYPE" >&2
    exit 1
    ;;

esac

exit 0
