#!/bin/sh
#
# $Id: write_boot_message,v 1.2 2004/03/19 10:34:44 jg Exp $
#
#
# Project     :  SCPM (System Configuration Profile Management)
# Module      :  SCPM utilities
# File        :  write_boot_messages
# Description :  creates profiles entry in /boot/messages
# Author      :  Joachim Gleissner <jg@suse.de>
#
# Copyright 2004 SuSE Linux AG
#
# Released under the terms of the GNU General Public License
# (see file COPYRIGHT in project root directory).
#

BM=/boot/message

cleanup_and_exit()
{
    rm -rf $TMPDIR
    exit $1
}

test -z "$1" && { echo "No profile names given." >&2 ; exit 1 ; }
test -x `type -p cpio` || { echo "Can not execute 'cpio'." >&2 ; exit 1 ; }
test -f $BM || { echo "$BM inaccessible" >&2 ; exit 1 ; }

trap "cleanup_and_exit 1" SIGINT SIGQUIT SIGTERM SIGSEGV

FLIST=$( cpio -t -F $BM ) || exit 1
TMPDIR=$( mktemp -d /tmp/scpm_boot.XXXXXX) || {
    echo "Could not create temporary directory" >&2 ; exit 1; }

cd $TMPDIR

if [ "$1" = "clear" -a -z "$2" ]; then
    # maybe we do not need to do anything
    test "$FLIST" = "${FLIST/profiles/}" && cleanup_and_exit 0
    cpio -i -F $BM -f profiles || cleanup_and_exit 1
    echo "${FLIST/profiles/}" | cpio -o -F $BM || cleanup_and_exit 1
    cleanup_and_exit 0
fi

ACTIVE="$1"
shift

cpio -i -F $BM -f profiles || cleanup_and_exit 1

while [ -n "$1" ]; do
    if [ "$1" = "$ACTIVE" ]; then
	echo -n '*'
    fi
    echo "$1 PROFILE=$1"
    shift
done > profiles

( echo "$FLIST" ;
  test "$FLIST" = "${FLIST/profiles/}" && echo profiles ) |
cpio -o -F $BM  || cleanup_and_exit 1

cleanup_and_exit 0
