#!/bin/bash

# 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

process_file()
{
	sed -e "s/#PACKAGE#/$PACKAGE/g" \
		-e "s/#VERSION#/$VERSION/g" \
		-e "s/#EMAIL#/$EMAIL/g" \
		-e "s/#DATE#/$DATE/g" \
		-e "s&#DOCS#&$DOCS&g" \
		-e "s&#CONFIGURE#&$CONFIGURE&g" \
		-e "s&#INSTALL#&$INSTALL&g" \
		-e "s&#CLEAN#&$CLEAN&g" \
		-e "s/#USERNAME#/$USERNAME/g" \
		-e "s/#POLICY#/$POLICY/g"
}

DEBMAKE_DIR=/usr/share/debmake

# Generate all the values we need

POLICY=3.6.1

if [ "$EMAIL" = "" ]; then
	EMAIL="$USER@`cat /etc/mailname`"
fi
echo "Email-Address		: $EMAIL"

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

USERNAME=`getent passwd $USER | awk -F: '{ print $5 }'`

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-z0-9][-+:a-z0-9\.]*$' >/dev/null = 0; then
	echo "Illegal package name $PACKAGE. Must be lowercase letters and digits, +, -, . or : ."
	exit 1
fi

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

echo -ne "\nType of Package (S=Single Binary, M=Multi-Binary, L=Library, X=Abort? s/m/l/x "
read A
case $A in
 s|S|m|M|l|L)
		;;
 *)	echo "Abort: Not debianized"
	exit 1
esac

PTYPE=`echo $A|tr A-Z a-z`

# 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

# Figure out where documentation is
DOCS=`ls N[Ee][Ww][Ss] *[cC][hH][aA][Nn][Gg][Ee][lL][oO][gG]* *[cC][hH][aA][Nn][Gg][Ee][sS]* \
	README* *.README [rR]eadme* *.[rR]eadme [Bb][Uu][Gg][Ss] \
	[tT][oO][dD][oO] 2>/dev/null | tr "\\n" " "`

if [ -f configure ]; then
	CONFIGURE='./configure --prefix=/usr'
	INSTALL='$(MAKE) install prefix=`pwd`/debian/tmp/usr'
	CLEAN='$(MAKE) distclean'
else
	CONFIGURE=""
	INSTALL='$(MAKE) install DESTDIR=`pwd`/debian/tmp'
	CLEAN='$(MAKE) clean'
fi

# Customize files
mkdir debian
cd debian
# General Files
X=`(cd $DEBMAKE_DIR/debian;ls)`
for i in $X; do
	process_file < $DEBMAKE_DIR/debian/$i >$i
done

# Special Files
X=`(cd $DEBMAKE_DIR/debian$PTYPE;ls)`
for i in $X; do
	process_file < $DEBMAKE_DIR/debian$PTYPE/$i >$i
done

# Variations for the native files.
if [ "$1" ]; then
	# Native Files
	X=`(cd $DEBMAKE_DIR/native;ls)`
	for i in $X; do
		process_file < $DEBMAKE_DIR/native/$i >$i
	done
fi

X="`ls package* 2>/dev/null`"
if [ "$X" ]; then
	for i in $X; do
		mv $i `echo $i|sed -e "s/^package/$PACKAGE/"`
	done
fi

chmod a+x rules

if [ "$CONFIGURE" ]; then
	echo "$PACKAGE debianized. It uses a configure script which probably"
	echo "means that you do not have to edit the Makefile."
else
	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 /"
fi

exit 0
