From 4a03496417f521caa06f7f4e02592aa5f0b6a390 Mon Sep 17 00:00:00 2001
From: joey <joey>
Date: Tue, 17 Aug 1999 04:21:03 +0000
Subject: [PATCH] r5: Initial Import

---
 README               | 15 +++++++++++++++
 debian/changelog     |  8 ++++++++
 debian/control       |  1 +
 debian/rules         | 10 ++++++++--
 dh_clean             |  2 +-
 dh_installdebfiles   | 24 +++++++++++++++++++++--
 dh_installdebfiles.1 | 10 ++++++++++
 dh_installdocs       |  2 +-
 dh_installexamples   |  2 +-
 dh_installmenu       | 14 ++++++++++++++
 dh_installmenu.1     | 10 +++++++++-
 dh_lib               | 46 ++++++++++++++++++++++++++++++++++++++++----
 12 files changed, 132 insertions(+), 12 deletions(-)

diff --git a/README b/README
index 8fbaebe..a13c772 100644
--- a/README
+++ b/README
@@ -6,6 +6,21 @@ To help you get started, I've included an example of a debian/rules file
 that uses debhelper commands extensivly. See
 /usr/doc/debhelper/examples/rules .
 
+Automatic generation of debian install scripts:
+----------------------------------------------
+
+Some debhelper commands will automatically generate parts of debian install
+scripts. If you want these automatically generated things included in your
+debian install scripts, then you need to add "#DEBHELPER#" to your scripts,
+in the place the code should be added. "#DEBHELPER#" will be replaced by any 
+autogenerated code when you run dh_installdebfiles.
+
+All scripts that automatically generate code in this way let it be disabled
+by the -n parameter.
+
+Note that it will be shell code, so you cannot directly use it in a perl 
+script.
+
 Notes on multiple binary packages:
 ---------------------------------
 
diff --git a/debian/changelog b/debian/changelog
index 7f1faa3..f904102 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -1,3 +1,11 @@
+debhelper (0.3) unstable; urgency=low
+
+  * Added support for automatic generation of debian install scripts to
+    dh_installmenu and dh_installdebfiles and dh_clean.
+  * Removed some pointless uses of cat.
+
+ -- Joey Hess <joeyh@master.debian.org>  Fri, 26 Sep 1997 21:52:53 -0400
+
 debhelper (0.2) unstable; urgency=low
 
   * Moved out of unstable, it still has rough edges and incomplete bits, but
diff --git a/debian/control b/debian/control
index b934849..35ad8f0 100644
--- a/debian/control
+++ b/debian/control
@@ -6,6 +6,7 @@ Standards-Version: 2.1.1.0
 
 Package: debhelper
 Architecture: all
+Depends: perl
 Description: helper programs for debian/rules
  A collection of programs that can be used in a debian/rules file to
  automate common tasks. Programs are included to install various files into
diff --git a/debian/rules b/debian/rules
index 247b48b..c6a4fa9 100755
--- a/debian/rules
+++ b/debian/rules
@@ -1,6 +1,11 @@
 #!/usr/bin/make -f
 # Note that I have to refer to debhelper programs witrh ./, to make sure
-# I run the most current ones.
+# I run the most current ones. That's also why there is a symlink to the 
+# current dh_lib in this debian/ directory.
+
+# If any automatic script generation is done in building this package, 
+# be sure to use the new templates from this package.
+export DH_AUTOSCRIPTDIR=autoscripts
 
 export DH_VERBOSE=1
 
@@ -29,10 +34,11 @@ binary-indep: build
 		-exec install -p {} debian/tmp/usr/bin \;
 	install -d debian/tmp/usr/lib/debhelper
 	cp dh_lib debian/tmp/usr/lib/debhelper
+	cp -a autoscripts debian/tmp/usr/lib/debhelper
 
 	./dh_installdocs TODO README
 	./dh_installexamples examples/*
-#	./dh_installmenu
+	./dh_installmenu
 	./dh_installmanpages
 	./dh_installchangelogs
 	./dh_compress
diff --git a/dh_clean b/dh_clean
index e7bec96..cb20e97 100755
--- a/dh_clean
+++ b/dh_clean
@@ -12,7 +12,7 @@ for PACKAGE in $DH_DOPACKAGES; do
 	doit "rm -rf debian/$TMP"
 	doit "rm -f debian/${EXT}substvars"
 done
-doit "rm -f debian/files* $*"
+doit "rm -f debian/files* debian/*.debhelper $*"
 
 # Remove other temp files. I don't run this through doit becuase
 # I haven't figured out what I have to esacape to put it in quotes.
diff --git a/dh_installdebfiles b/dh_installdebfiles
index 872eacb..a09cd12 100755
--- a/dh_installdebfiles
+++ b/dh_installdebfiles
@@ -14,10 +14,30 @@ for PACKAGE in $DH_DOPACKAGES; do
 		doit "install -o root -g root -d debian/$TMP/DEBIAN"
 	fi
 
-	# Install executable files.
+	# Install debian install scripts.
+	# If any .debhelper files exist, add them into the scripts.
 	for file in postinst preinst prerm postrm; do
 		if [ -f debian/$EXT$file ]; then
-			doit "install -o root -g root -p debian/$EXT$file debian/$TMP/DEBIAN"
+			# Add this into the script, where it has #DEBHELPER#
+			if [ -f debian/$EXT$file.debhelper ]; then
+				verbose_echo "perl -pe \"s~#DEBHELPER#~qx{cat debian/$EXT$file.debhelper}~eg\" < debian/$EXT$file > debian/$TMP/DEBIAN/$EXT$file"
+				perl -pe "s~#DEBHELPER#~qx{cat debian/$EXT$file.debhelper}~eg" < debian/$EXT$file > debian/$TMP/DEBIAN/$EXT$file
+				doit "chown root.root debian/$TMP/DEBIAN/$EXT$file"
+				doit "chmod 755 debian/$TMP/DEBIAN/$EXT$file"
+			else
+				doit "install -o root -g root -p debian/$EXT$file debian/$TMP/DEBIAN/"
+			fi
+		else
+			# Auto-generate script header and add .debhelper
+			# content to it.
+			if [ -f debian/$EXT$file.debhelper ]; then
+				verbose_echo "echo '#!/bin/sh -e' > debian/$TMP/DEBIAN/$EXT$file"
+				echo '#!/bin/sh -e' > debian/$TMP/DEBIAN/$EXT$file
+				verbose_echo "cat debian/$EXT$file.debhelper >> debian/$TMP/DEBIAN/$EXT$file"
+				cat debian/$EXT$file.debhelper >> debian/$TMP/DEBIAN/$EXT$file
+				doit "chown root.root debian/$TMP/DEBIAN/$EXT$file"
+				doit "chmod 755 debian/$TMP/DEBIAN/$EXT$file"
+			fi
 		fi
 	done
 
diff --git a/dh_installdebfiles.1 b/dh_installdebfiles.1
index 1f62084..3f63986 100644
--- a/dh_installdebfiles.1
+++ b/dh_installdebfiles.1
@@ -22,6 +22,16 @@ the DEBIAN directory:
 prefix these filenames with then name of the "package.", for example, 
 "foo.postinst".)
 .P
+The files postinst, preinst, postrm, and prerm are handled specially: If a
+corresponding file named debian/script.debhelper exists, the contents of that 
+file are merged into the script as follows: If the script exists, then 
+anywhere in it that "#DEBHELPER#" appears, the text of the .debhelper file is
+inserted. If the script does not exist, then a script is generated from the 
+.debhelper file. The .debhelper files are created by other debhelper programs,
+such as 
+.BR dh_installmenu (1)
+, and are shell scripts.
+.P
 In addition, it will generate a DEBIAN/control file, by running
 .BR dpkg-shlibdeps (1)
 and
diff --git a/dh_installdocs b/dh_installdocs
index 336ed6e..dc21628 100755
--- a/dh_installdocs
+++ b/dh_installdocs
@@ -17,7 +17,7 @@ for PACKAGE in $DH_DOPACKAGES; do
 	docs=""
 
 	if [ -e debian/${EXT}docs ]; then
-		docs=`cat debian/${EXT}docs | tr "\n" " "`
+		docs=`tr "\n" " " < debian/${EXT}docs`
 	fi
 
 	if [ "$PACKAGE" = "$MAINPACKAGE" -a "$*" ]; then
diff --git a/dh_installexamples b/dh_installexamples
index 06e28d6..f82b000 100755
--- a/dh_installexamples
+++ b/dh_installexamples
@@ -13,7 +13,7 @@ for PACKAGE in $DH_DOPACKAGES; do
 	examples=""
 
 	if [ -e debian/${EXT}examples ]; then
-		examples=`cat debian/${EXT}examples | tr "\n" " "`
+		examples=`tr "\n" " " < debian/${EXT}examples`
 	fi
 
 	if [ "$PACKAGE" = "$MAINPACKAGE" -a "$*" ]; then
diff --git a/dh_installmenu b/dh_installmenu
index 775945a..1180c60 100755
--- a/dh_installmenu
+++ b/dh_installmenu
@@ -5,6 +5,8 @@
 # If debian/menu file exists, save it to debian/$TMP/usr/lib/menu/$PACKAGE
 # If debian/menu-method file exists, save it to 
 # debian/$TMP/etc/menu-methods/$PACKAGE
+#
+# Also, add to postinst and postrm.
 
 PATH=debian:$PATH:/usr/lib/debhelper
 source dh_lib
@@ -18,6 +20,13 @@ for PACKAGE in $DH_DOPACKAGES; do
 			doit "install -d debian/$TMP/usr/lib/menu"
 		fi
 		doit "install -p -m644 debian/${EXT}menu debian/$TMP/usr/lib/menu/$PACKAGE"
+
+		# Add the scripts if a menu-method file doesn't exist.
+		# The scripts for menu-method handle everything these do, too.
+		if [ ! -e debian/${EXT}menu-method -a ! "$DH_NOSCRIPTS" ]; then
+			autoscript "postinst" "postinst-menu"
+			autoscript "postrm" "postrm-menu"
+		fi
 	fi
 
 	if [ -e debian/${EXT}menu-method ]; then
@@ -25,5 +34,10 @@ for PACKAGE in $DH_DOPACKAGES; do
 			doit "install -d debian/$TMP/etc/menu-methods"
 		fi
 		doit "install -p debian/${EXT}menu-method debian/$TMP/etc/menu-methods/$PACKAGE"
+
+		if [ ! "$DH_NOSCRIPTS" ]; then
+			autoscript "postinst" "postinst-menu-method" "s/#PACKAGE#/$PACKAGE/"
+			autoscript "postrm" "postrm-menu-method" "s/#PACKAGE#/$PACKAGE/"
+		fi
 	fi
 done
diff --git a/dh_installmenu.1 b/dh_installmenu.1
index c7fa1bf..f3eba62 100644
--- a/dh_installmenu.1
+++ b/dh_installmenu.1
@@ -6,7 +6,12 @@ dh_installmenu \- install debian menu files into package build directories
 .I "[-v] [-a] [-i] [-ppackage]"
 .SH "DESCRIPTION"
 dh_installmenu is a debhelper program that is responsible for installing
-fils used by the debian menu package into package build directories.
+fils used by the debian menu package into package build directories. 
+.P
+It also automatically generates the postinst and postrm commands needed to 
+interface with the debian menu package. See 
+.BR dh_installdebfiles (1)
+for an explanation of how this works.
 .P
 If a file named debian/menu exists, then it is installed into
 usr/lib/menu/package in the package build directory. This is a debian menu
@@ -32,6 +37,9 @@ Install menu files into all architecture independent packages.
 .TP
 .B \-ppackage
 Install menu files into the package named "package".
+.TP
+.B \-n
+Do not automatically generate code.
 .SH NOTES
 The
 .B \-a
diff --git a/dh_lib b/dh_lib
index b8562cd..b18c7e5 100644
--- a/dh_lib
+++ b/dh_lib
@@ -3,15 +3,17 @@
 # Run a command, and display the command to stdout if verbose mode is on.
 # All commands that modifiy files in debian/$TMP should be ran via this 
 # function.
+# Unfortunatly, this function doesn't work if your command uses redirection,
+# you will have to call verbose_echo by hand then.
 function doit() {
-	verbose_echo "$1"
-	$1
+	verbose_echo "$*"
+	$*
 }
 
 # Echo something if the verbose flag is on.
 function verbose_echo() {
 	if [ "$DH_VERBOSE" ]; then
-		echo "	$1"
+		echo "	$*"
 	fi
 }
 
@@ -40,10 +42,42 @@ function pkgext() {
 	fi
 }
 
+# Automatically add a shell script snippet to a debian script.
+# Only works if the script has #DEBHELPER# in it.
+#
+# Parameters:
+# 1: script to add to
+# 2: filename of snippet
+# 3: sed commands to run on the snippet. Ie, s/#PACKAGE#/$PACKAGE/
+function autoscript() {
+	autoscript_script=$1
+	autoscript_filename=$2
+	autoscript_sed=$3
+	autoscript_debscript=debian/`pkgext $PACKAGE`$autoscript_script.debhelper
+
+	if [ -e "$DH_AUTOSCRIPTDIR/$autoscript_filename" ]; then
+		autoscript_filename="$DH_AUTOSCRIPTDIR/$autoscript_filename"
+	else
+		if [ -e "/usr/lib/debhelper/autoscripts/$autoscript_filename" ]; then
+			autoscript_filename="/usr/lib/debhelper/autoscripts/$autoscript_filename"
+		else
+			error "/usr/lib/debhelper/autoscripts/$autoscript_filename does not exist"
+		fi
+	fi
+
+	# Running doit doesn't cut it here.
+	verbose_echo echo "# Automatically added by `basename $0` on `822-date`" ">>" $autoscript_debscript
+	echo "# Automatically added by `basename $0` on `822-date`" >> $autoscript_debscript
+	verbose_echo sed "$autoscript_sed" $autoscript_filename ">>" $autoscript_debscript
+	sed "$autoscript_sed" $autoscript_filename >> $autoscript_debscript
+	verbose_echo  echo "# End automatically added section" ">>" $autoscript_debscript
+	echo "# End automatically added section" >> $autoscript_debscript
+}
+
 # Argument processing and global variable initialization is below.
 
 # Parse command line.
-set -- `getopt viap: $*`
+set -- `getopt vianp: $*`
 
 for i; do
 	case "$i"
@@ -65,6 +99,10 @@ for i; do
 			shift
 			shift
 			;;
+		-n)
+			DH_NOSCRIPTS=1
+			shift
+			;;
 		--)
 			shift
 			break
-- 
2.39.5