#!/bin/sh
#
# $Id: prepare,v 1.1.1.1 2005/05/11 09:03:12 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).
#

if [ -f /etc/scpm.conf ]; then
    . /etc/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 /etc/scpm.conf ]; then
    if [ -f $CONF_TEMPL ]; then
	echo "`basename $0`: installing $CONF_TEMPL as /etc/scpm.conf" >&2
	cp $CONF_TEMPL /etc/scpm.conf || exit 1
    else
	echo "`basename $0`: ERROR: could find neither /etc/scpm.conf nor $CONF_TEMPL" >&2
    fi
fi

. /etc/scpm.conf

test -z "$DBFILE" && {
    echo "`basename $0`: ERROR: DBFILE is unset. Seems your /etc/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
