#!/bin/sh
#
# $Id: service_get_all,v 1.10 2003/07/18 13:26:59 jg Exp $
#
#
# Project     :  SCPM (System Configuration Profile Management)
# Module      :  SCPM Utilities
# File        :  service_get_all
# Description :  Determines all installed services
# Author      :  Joachim Gleissner <jg@suse.de>
#
# Copyright 2001 SuSE GmbH
#           2002-2003 SuSE Linux AG
#
# Released under the terms of the GNU General Public License
# (see file COPYRIGHT in project root directory).
#

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

LANG=C

SERVICE_EXCLUDE_PATTERN="$SERVICE_EXCLUDE_PATTERN $( cat $LIBDIR/resource_types/service/exclude 2>/dev/null  )"
SERVICE_EXTRA_PATTERN="$SERVICE_EXTRA_PATTERN $( cat $LIBDIR/resource_types/service/extra 2>/dev/null )"

is_init_script()
{
    while read LINE ; do
	test "$LINE" = "### BEGIN INIT INFO" && return 0
    done < $i
    return 1
}

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

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

resolve_group()
{
    test -z "$1" && return 
    if [ -f /var/lib/scpm/resource_groups/$1 ]; then
	GROUP=/var/lib/scpm/resource_groups/$1
    elif [ -f $LIBDIR/resource_groups/$1 ]; then
	GROUP=$LIBDIR/resource_groups/$1
    else
	echo "resource group $1 unknown" >&2
	exit 1
    fi
    while read TYPE NAME REST ; do
	test "${REST}" = "inactive" && continue
	test "${TYPE}" = "service" && echo "$NAME"
    done < $GROUP
}

if [ -e /var/lib/scpm/active_groups ]; then
    while read GROUPNAME REST ; do
	resolve_group $GROUPNAME
    done < /var/lib/scpm/active_groups | sort | uniq
    exit 0
fi

if [ -n "$RESOURCE_SET" ]; then
    if [ -f /var/lib/scpm/resource_sets/$RESOURCE_SET ]; then
	SET=/var/lib/scpm/resource_sets/$RESOURCE_SET
    elif [ -f $LIBDIR/resource_sets/$RESOURCE_SET ]; then
	SET=$LIBDIR/resource_sets/$RESOURCE_SET
    else
	echo "resource set $RESOURCE_SET unknown" >&2
	exit 1
    fi
    while read TYPE NAME ; do
	test "${TYPE}" = "service" && echo "$NAME"
	test "${TYPE}" = "group" && resolve_group $NAME
    done < $SET
    exit 0
fi

for i in $INITD/* ; do

    test -d $i && continue
    is_init_script $i || continue
    echo $( basename $i )

done | sort | uniq | filter_excludes

test -z "$SERVICE_EXTRA_PATTERN" && exit 0

for i in $SERVICE_EXTRA_PATTERN ; do
    test -f $INITD/$i && echo $i
done

exit 0
