#!/bin/sh
#
# $Id: file_get_all,v 1.11 2003/07/18 13:26:58 jg Exp $
#
#
# Project     :  SCPM (System Configuration Profile Management)
# Module      :  SCPM utilities
# File        :  file_get_all
# Description :  Returns all resources of type File
# 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).
#

EXTRA_FILES="/etc/fstab /etc/rc.config /etc/XF86Config /etc/X11/XF86Config /etc/nscd.conf"
LANG=C

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

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
	;;

	*)
	echo "unkown option $1" >&2
	exit 1
	;;
    esac
    shift
done


. /etc/scpm.conf || exit 1

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}" = "file" && 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 REST ; do
	test "${TYPE}" = "file" && echo "$NAME"
	test "${TYPE}" = "group" && resolve_group $NAME
    done < $SET
    exit 0
fi

FILE_EXTRA_PATTERN="$FILE_EXTRA_PATTERN $( cat $LIBDIR/resource_types/file/extra 2>/dev/null )"
FILE_EXCLUDE_PATTERN="$FILE_EXCLUDE_PATTERN $( cat $LIBDIR/resource_types/file/exclude 2>/dev/null )"

(

cd /etc/init.d

for i in * ; do 

    test -d $i && continue

    PACKAGE_NAME=`rpm -qf $i 2>/dev/null`
    test -z "$PACKAGE_NAME" && { echo "Warning: /etc/init.d/$i is not owned by any package, ignored" >&2 ;
				 continue ; }
    rpm -q --configfiles $PACKAGE_NAME

done | filter_excludes

# print extra files
if [ -n "$FILE_EXTRA_PATTERN" ]; then
    for i in $FILE_EXTRA_PATTERN ; do
	test -f $i && echo $i
    done
fi

) | sort | uniq

exit 0
