#!/bin/sh
#
# $Id: update_storage,v 1.2 2002/08/20 10:41:39 jg Exp $
#
#
# Project     :  SCPM (System Configuration Profile Management)
# Module      :  SCPM utilities
# File        :  update_storage
# Description :  Tries to fix storage to work with new SCPM version
# Author      :  Joachim Gleissner <jg@suse.de>
#
# Copyright 2002 SuSE Linux AG
#
# Released under the terms of the GNU General Public License
# (see file COPYRIGHT in project root directory).
#

get_sysconfig_resource()
{
    NAME="nothing"
    while read n r ; do
	if [ "${r%%-*}" != "$NAME" ]; then
	    echo $r
	    NAME=$r
	fi
    done < $1
}

get_sub()
{
    while read n r ; do
	test "$r" = "$2" && continue
	if [ "${r%%-*}" = "$2" ]; then
	    echo "${r#*-}"
	fi
    done < $1
}

get_new_index()
{
    idx=0;
    while read n r ; do
	test $n -gt $idx && idx=$n
    done < $1
    echo $((idx+1))
}

for PROFILE_DIR in /var/lib/scpm/profiles/* ; do
    cd $PROFILE_DIR
    
    test -d sysconfig || continue

    for RESOURCE in $( get_sysconfig_resource sysconfig/.sindex ) ; do
	SUBRES=$( get_sub sysconfig/.sindex $RESOURCE )
	test -z "$SUBRES" && continue
	INDEX_META=$(get_new_index file/.sindex)
	INDEX=$INDEX_META
	# write index file
	echo "$INDEX_META META-/etc/sysconfig/$RESOURCE" >> file/.sindex
	echo "##SCPM_FILE_METADATA" > file/$INDEX_META
	for s in $SUBRES ; do
	    if [ -L /etc/sysconfig/$RESOURCE/$s ]; then
		echo "##SCPM_FILE_LINK" >> file/$INDEX_META
	    elif [ -f /etc/sysconfig/$RESOURCE/$s ]; then
		echo "##SCPM_FILE_REGULAR" >> file/$INDEX_META
	    elif [ ! -e /etc/sysconfig/$RESOURCE/$s ]; then
		echo "##SCPM_FILE_GHOST" >> file/$INDEX_META
	    else
		echo "##SCPM_FILE_UNKNOWN" >> file/$INDEX_META
	    fi
	    echo /etc/sysconfig/$RESOURCE/$s >> file/$INDEX_META
	    INDEX=$((INDEX+1))
	    cp -a /etc/sysconfig/$RESOURCE/$s file/$INDEX
	    echo "$INDEX /etc/sysconfig/$RESOURCE/$s" >>file/.sindex
	    md5sum /etc/sysconfig/$RESOURCE/$s | cut -d' ' -f1  >file/$INDEX.md5sum
	done
	md5sum file/$INDEX_META | cut -d' ' -f1 >file/$INDEX_META.md5sum
    done

done
