#!/bin/sh
#
# $Id: prepare,v 1.2 2005/12/13 16:30:15 gleissner Exp $
#
#
# Project     :  SCPM (System Configuration Profile Management)
# Module      :  SCPM utilities
# File        :  prepare
# Description :  Prepares the system for SCPM
# 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).
#

SCPM_CONF=/etc/sysconfig/scpm

if [ -f $SCPM_CONF ]; then
    . $SCPM_CONF
    CONF_TEMPL=$LIBDIR/scpm.conf.templ
    DB_TEMPL=$LIBDIR/scdb.templ
else
    # using defaults
    CONF_TEMPL=/lib/scpm/scpm.conf.templ
    DB_TEMPL=/lib/scpm/scdb.templ
fi

show_usage()
{
    echo "$0 [options]"
    echo
    echo "options may be"
    echo
    echo "-h   shows help (this one)"
    echo "-f   force initialization"
    echo "     warning: this overrides an existing profile database!"
}


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

	-f)
	FORCE=yes
	;;

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


if [ ! -f $SCPM_CONF ]; then
    if [ -f $CONF_TEMPL ]; then
	echo "`basename $0`: installing $CONF_TEMPL as $SCPM_CONF" >&2
	cp $CONF_TEMPL $SCPM_CONF || exit 1
    else
	echo "`basename $0`: ERROR: could find neither $SCPM_CONF nor $CONF_TEMPL" >&2
    fi
fi

. $SCPM_CONF

test -z "$DBFILE" && {
    echo "`basename $0`: ERROR: DBFILE is unset. Seems your $SCPM_CONF is broken." >&2 ;
    exit 2 ; }

if [ -f "$DBFILE" ]; then
    if [ "$FORCE" = "yes" ]; then
	test -f $DB_TEMPL || {
	    echo "`basename $0`: ERROR: $DB_TEMPL does not exist!" >&2 ;
	    exit 2; }
	cp $DB_TEMPL $DBFILE || exit 1
    fi
else
    test -f $DB_TEMPL || {
	echo "`basename $0`: ERROR: $DB_TEMPL does not exist!" >&2 ;
	exit 2; }
    cp $DB_TEMPL $DBFILE || exit 1
fi

exit 0
