#!/bin/sh
# DocumentId:	$Id: dpsch-genpkg 2318 2006-07-19 17:58:05Z ola $
# Author:	$Author: ola $
#		Ola Lundqvist <opal@debian.org>
# Summary:
#	This tool creates a file-structure for a
#	system configuration package.
#
# Copyright (C) 2001-2004 Ola Lundqvist <opal@debian.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#

. /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
