#!/bin/sh

# Script to debianize a regular source archive
# This script is to be called from within the source archive
# If it has one parameter then a native debian archive will be generated
# and no .orig directory made.
#
# Christoph Lameter, <clameter@debian.org> October 10, 1996

LIB=/usr/lib/deb-make

# Generate all the values we need
if [ "$EMAIL" = "" ]; then
	EMAIL="$USER@`cat /etc/mailname`"
fi
echo "Email-Address		: $EMAIL"

DATE="`822-date`"
echo "Date used		: $DATE"

USERNAME=`awk -F: -vUSER=$USER '$1 == USER { print $5; }' /etc/passwd`

if [ "$USERNAME" = "" -a -x /usr/bin/ypmatch ]; then
	# Give NIS a try
	USERNAME=`ypmatch $USER passwd.byname|awk -F: '{ print $5; }'`
fi

if echo $USERNAME | grep -q "\,"; then
	X=`expr index "$USERNAME" ","`
	X=`expr $X - 1`
	USERNAME=`expr substr "$USERNAME" 1 $X`
fi

echo "Maintainer		: $USERNAME"

# Analyze the directory name
NAME=`expr $PWD : '.*/\(.*\)'`

if ! echo $NAME | grep -q "\-"; then
	echo "Current directory name must be <package>-<version> for debmake to work!"
	echo "No underscores are allowed!"
	exit 1
fi

VERSION=`expr $NAME : '.*-\([^-]*\)'`
PACKAGE=`expr $NAME : '\(.*\)-[^-]*'`

if expr $PACKAGE : '[a-z][a-z0-9-]*$' >/dev/null = 0; then
	echo "Illegal package name $PACKAGE. Must be lowercase letters and digits."
	exit 1
fi

echo "Package Name		: $PACKAGE"
echo "Version			: $VERSION"

echo -ne "\nDebianize with these parameters? y/N "
read A
if [ "$A" != "y" -a "$A" != "Y" ]; then
	echo "Not debianized"
	exit 1
fi

# Setup the original archive
if [ "$1" = "" ]; then
	if [ -d ../$NAME.orig ]; then
		echo "Skipping copying to $NAME.orig since $NAME.orig exists"
	else
		cp -a ../$NAME ../$NAME.orig
	fi
fi

# Customize files
mkdir debian
cd debian
X=`(cd $LIB/debian;ls)`
for i in $X; do
	sed <$LIB/debian/$i >$i -e "s/#PACKAGE#/$PACKAGE/g" \
		-e "s/#VERSION#/$VERSION/g" \
		-e "s/#EMAIL#/$EMAIL/g" \
		-e "s/#DATE#/$DATE/g" \
		-e "s/#USERNAME#/$USERNAME/g"
done

if [ "$1" = "" ]; then
	rm changelog.native
else
	mv changelog.native changelog
fi

chmod a+x rules

echo "$PACKAGE debianized. Please edit the files in the debian directory now"
echo "and check that the Makefile puts the binaries into $DESTDIR and not in /"

exit 0
