#!/bin/sh
# DocumentId:	$Id: dpsch-genpkg,v 1.16 2003/05/12 19:31:34 ola Exp $
# Author:	$Author: ola $
#		Ola Lundqvist <opal@debian.org>
# Summary:
#	This tool creates a file-structure for a
#	system configuration package.

. /usr/share/dpsyco/change.func

SRCDIR=""

if [ -z "$1" ] ; then
    echo "USAGE: dpsch-genpkg \$example"
    echo ""
    cd /usr/share/dpsyco-devel/genpkg
    ls
    cd /etc/dpsyco/examples
    ls
    echo ""
    exit 0
elif echo "$1" | grep "^/" > /dev/null 2>&1 ; then
    SRCDIR="$1"
elif [ -d /usr/share/dpsyco-devel/genpkg/$1 ] ; then
    SRCDIR=/usr/share/dpsyco-devel/genpkg/$1
elif [ -d /etc/dpsyco/genpkg/$1 ] ; then
    SRCDIR=/usr/share/dpsyco-devel/genpkg/$1
else
    echo "USAGE: dpsch-genpkg example"
    echo ""
    cd /usr/share/dpsyco-devel/genpkg
    ls
    cd /etc/dpsyco/examples
    ls
    echo ""
    exit 0
fi

FLIST="$(find $SRCDIR -type f -printf '%P ')"

if [ -z "$EMAIL" ] ; then
    EMAIL="$USER@$(hostname -f)"
fi

if [ ! -z "$DEBEMAIL" ] ; then
    EMAIL="$DEBEMAIL"
fi

if [ ! -z "$NAME" ] ; then
    ADMIN="$NAME"
elif [ ! -z "$USER" ] ; then
    ADMIN=$(getent passwd $USER | {
	OIFS="$IFS"
	IFS=:
	read U P I G ADMIN HOME MORE SHELL
	IFS="$OIFS"
	echo "$ADMIN" | sed -e "s|\,.*$||g;"
    })
fi

if [ -z "$ADMIN" ] ; then
    echo "Can not find the administrators full name, so you have to type it here."
    read ADMIN
fi

if [ -z "$EMAIL" ] ; then
    echo "Can not find the administrators email, so you have to type it here."
    read EMAIL
fi

if [ -z "$PACKAGE" ] ; then
    PACKAGE=$(pwd | sed -e "s|^.*/||;")
fi

for FILE in $FLIST ; do
    if echo "$FILE" | grep "/" > /dev/null 2>&1 ; then
	DIR=$(echo "$FILE" | sed -e "s|/[^/]*$||;")
	if [ ! -d "$DIR" ] ; then
	    mkdir -p $DIR
	fi
    fi
    if [ ! -e "$FILE" ] ; then
	cp -af $SRCDIR/$FILE $FILE
	changefile "s|%ADMIN%|$ADMIN|g;
		    s|%EMAIL%|$EMAIL|g;
		    s|%PACKAGE%|$PACKAGE|g;
		    " $FILE
    else
	echo "$FILE does already exist, skipping."
    fi
done

dpsch-gencvsignore
