#!/usr/bin/env bash
#
# OpenXPKI (Re)configuration script
#
# Written by Martin Bartosch for the OpenXPKI project 2006
# Copyright (c) 2006 by The OpenXPKI Project
#

prefix="/usr"
exec_prefix="/usr"
TEMPLATEDIR="/usr/share/openxpki/templates"
TEMPLATE="default"
SYSCONFDIR="/etc"
OPENXPKICONFDIR="/etc/openxpki/instances/Base"

INTERACTIVE=1
CREATE_DIRS=""
FORCE=""


reconfigure() {
    # interactively create initial configuration
    $CFG --sectionsmenu --interactive --force --writecfg $OPENXPKICONFDIR/openxpki.conf
    return $?
}

write_config() {
    echo
    echo "* Creating configuration files in $OPENXPKICONFDIR"
    for xmlfile in $* ; do
	basename=`basename $xmlfile`
	echo -n "$basename... "
	$CFG --file $xmlfile $FORCE --dstdir $OPENXPKICONFDIR
	if [ $? = 0 ] ; then
	    echo "done"
	else
	    echo "FAILED"
	    echo "*** Configuration incomplete (and probably broken)."
	    echo "*** Please correct the problem and repeat."
	    exit 1
	fi
    done
}

# if no arguments are given this function only checks if the required
# directories are present. otherwise these directories are also created
check_dirs() {
    echo
    echo "* Checking directories"
    error=0
    for entry in openxpkistatedir openxpkisessiondir dataexchange tmpdir; do
	dir="`$CFG --getcfg dir.$entry`"
	echo -n "$entry: "
	if [ -d "$dir" ] ; then
	    echo "$dir: OK"
	else
	    if [ "$1" != "" ] ; then
		if [ "`uname`" == "SunOS" ]; then 
			INSTALL="/usr/sbin/install -u"; 
		else 
			INSTALL="install -o"; 
		fi ; 
		if $INSTALL $RUNUSER -g $RUNGROUP -m 0750 -d $dir ; then
		    echo "$dir: CREATED"
		else
		    echo "$dir: FAILED TO CREATE"
		    error=1
		fi
	    else
		echo "$dir: DOES NOT EXIST"
		error=1
	    fi
	fi
    done
    return $error
}


###########################################################################
echo
echo "OpenXPKI (Re)configuration Script"
echo "Copyright (c) 2006 by The OpenXPKI Project"
echo

while [ -n "$1" ] ; do
    case "$1" in
	--help)
	    pod2man $0 | nroff -man -Tascii
	    exit 0
	    ;;
	--batch)
	    echo "Entering batch mode..."
	    INTERACTIVE=0
	    shift
	    ;;
	--force)
	    FORCE="$1"
	    shift
	    ;;
	--template)
	    TEMPLATE="$2"
	    shift
	    shift
	    ;;
	--createdirs)
	    CREATE_DIRS=1
	    shift
	    ;;
        --)
            shift
            METACONF_OPTS="$*"
            echo "Additional options to openxpki-metaconf: '$METACONF_OPTS'"
            shift $#
            ;;
	*)
	    echo "Usage: $0 OPTIONS"
	    echo "Options:"
	    echo "  --batch    Noninteractively reconfigure XML files from openxpki.conf"
	    exit 0;
	    ;;
    esac
done

for DIR in /etc/openxpki /usr/local/etc/openxpki "$OPENXPKICONFDIR" "`pwd`/etc/openxpki" "`pwd`"; do
    if [ -e "$DIR/openxpki.conf" ]; then
	OPENXPKICONFDIR="$DIR"
    fi
done

CFG="openxpki-metaconf --config $OPENXPKICONFDIR/openxpki.conf $METACONF_OPTS"

echo "Configuring in $OPENXPKICONFDIR..."

if [ "$INTERACTIVE" = "1" ] ; then
    if ! reconfigure ; then
	echo "Exiting..."
	exit 0
    fi
fi

XMLSTYLE="`$CFG --getcfg deployment.xmlstyle`"
RUNUSER="`$CFG --getcfg server.runuser`"
RUNGROUP="`$CFG --getcfg server.rungroup`"

FILES="$TEMPLATEDIR/$TEMPLATE/log.conf"

case $XMLSTYLE in
    multi-file)
	echo "Creating configuration (multiple XML files)..."
	FILES="$FILES `ls $TEMPLATEDIR/$TEMPLATE/*.xml`"
	;;
    all-in-one)
	echo "Creating configuration (single XML file)..."
	FILES="$FILES $TEMPLATEDIR/$TEMPLATE/config.xml"
	;;
    *)
	echo "Illegal deployment mode $XMLSTYLE"
	exit 1
	;;
esac

write_config $FILES

if ! check_dirs $CREATE_DIRS ; then
    echo
    echo "One or more required directories do not exist. Please inspect the above"
    echo "error output and either create the missing directories or run"
    echo
    echo "$0 [OPTIONS] --createdirs"
    echo
    echo "to automatically create them."
    echo
    exit 1
fi

echo
echo "OpenXPKI instance configured successfully."

exit 0

:<<__EOF

###########################################################################

=head1 NAME

openxpki-configure - Create OpenXPKI config files from meta-configuration.

=head1 USAGE 

openxpki-configure [OPTIONS]

  Options:
    --help         display this help text and exit
    --batch        non-interactive configuration
    --force        overwrite existing files
    --template     specify template (default: default)
    --createdirs   create missing directories
    --             args following -- are literally passed to openxpki-metaconf

This script generates the OpenXPKI configuration files from a set of
templates (taken from $TEMPLATEDIR)
by filling in values specified in the OpenXPKI meta configuration
$OPENXPKICONFDIR/openxpki.conf.

Please note that this meta configuration is intended for getting
OpenXPKI up and running quickly. Beyond basic configuration there 
are many configuration parameters that cannot be set by this approach.

(For a more advanced setup may be advisable to modify the settings in
OpenXPKI's XML configuration files directly and NOT use openxpki-configure.)
__EOF
