From b5e7ef106f575b718c84ef2c0b91c08e57557a83 Mon Sep 17 00:00:00 2001
From: Dragan Dosen <bane@nekkar.carnet.hr>
Date: Thu, 19 Feb 2009 21:48:57 +0100
Subject: [PATCH]   Izmjene vezane za migraciju servisa sa inetd konfiguracije
   na odvojenu xinetd konfiguraciju.

  Datoteka debian/postinst:
  + migracija na zasebnu xinetd konfiguraciju provodi se za
    sve servise, neovisno o -inetd_compat opciji,
  + brisanje zaostalih privremenih datoteka u slucaju da
    migracija na odvojenu xinetd konfiguraciju ne prodje
    uspjesno,
  + dodatne manje izmjene.

  Izmjene unutar README.CARNet datoteke.
---
 debian/README.CARNet |    8 ++---
 debian/changelog     |   16 +++++++---
 debian/postinst      |   85 +++++++++++++++++++++++++++++---------------------
 3 files changed, 63 insertions(+), 46 deletions(-)

diff --git a/debian/README.CARNet b/debian/README.CARNet
index 5d4ed33..533dfb0 100644
--- a/debian/README.CARNet
+++ b/debian/README.CARNet
@@ -7,11 +7,9 @@ daemon".
 
 INSTALACIJA:
 
-U slucaju da je unutar /etc/default/xinetd datoteke aktivna 
--inetd_compat opcija ili postoji zapis 'INETD_COMPAT=Yes', servisi
-zapisani u /etc/inetd.conf datoteci bit ce automatski konvertirani
-u zasebne konfiguracijske datoteke unutar /etc/xinetd.d/
-direktorija.
+Servisi koji su zapisani u /etc/inetd.conf datoteci bit ce
+automatski konvertirani u zasebne konfiguracijske datoteke unutar
+/etc/xinetd.d/ direktorija.
 
 Servisi koji su konfigurirani unutar konfiguracijske datoteke
 /etc/xinetd.conf bit ce takodjer automatski konvertirani u 
diff --git a/debian/changelog b/debian/changelog
index bfed64f..47d1adc 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -1,11 +1,17 @@
 xinetd-cn (1:2.3.14-2) stable; urgency=low
 
   * Tranzicijski paket za buduci prelazak na Debian Lenny.
-  * Deaktivacija novog 'INETD_COMPAT=Yes' zapisa unutar datoteke
-    /etc/default/xinetd - isti se postavlja u 'INETD_COMPAT=No'.
-  * Dodatne manje izmjene unutar debian/postinst datoteke.
-  * debian/control - ovisnost o xinetd (>= 1:2.3.14-7~cn1).
-  * Manje izmjene unutar README.CARNet datoteke.
+  * Datoteka debian/postinst:
+    + migracija na zasebnu xinetd konfiguraciju provodi se za sve
+      servise, neovisno o -inetd_compat opciji,
+    + brisanje zaostalih privremenih datoteka u slucaju da
+      migracija na odvojenu xinetd konfiguraciju nije uspjesna,
+    + deaktivacija novog 'INETD_COMPAT=Yes' zapisa unutar datoteke
+      /etc/default/xinetd - isti se postavlja u 'INETD_COMPAT=No',
+    + dodatne manje izmjene.
+  * Datoteka debian/control:
+    + ovisnost o xinetd (>= 1:2.3.14-7~cn1).
+  * Izmjene unutar README.CARNet datoteke.
 
  -- Dragan Dosen <ddosen@ffzg.hr>  Thu, 18 Dec 2008 18:26:46 +0100
 
diff --git a/debian/postinst b/debian/postinst
index 84b2ca9..20e4b9d 100644
--- a/debian/postinst
+++ b/debian/postinst
@@ -13,41 +13,43 @@ CONF=/etc/xinetd.conf
 INETDCONF=/etc/inetd.conf
 DEFAULT=/etc/default/xinetd
 
-did_inetd_backup=0
-did_backup=0
 need_restart=0
+temp_files=
 
-# First, we do backup (inetd, xinetd)
+# cleanup()
 #
-if cp_check_and_backup "$INETDCONF"; then
-	did_inetd_backup=1
-fi
-if cp_check_and_backup "$CONF"; then
-	did_backup=1
-fi
+#   Cleanup all temp files.
+#
+cleanup () {
+
+	local item
+	if [ -n "$temp_files" ]; then
+		for item in $temp_files; do
+			if [ -e "$item" ]; then
+				rm -f $item
+			fi
+		done
+	fi
+}
 
-# Print backup information
+
+# Set trap for deleting all temp files.
+#
+trap cleanup 0 1 2 15
+
+# First, we do backup (inetd, xinetd)
 #
-cp_echo ""
-if [ $did_inetd_backup -eq 1 ]; then
+if cp_check_and_backup "$INETDCONF"; then
 	cp_echo "CN: Old $INETDCONF saved as /var/backups/`basename $INETDCONF`.bak."
 fi
-if [ $did_backup -eq 1 ]; then
+if cp_check_and_backup "$CONF"; then
 	cp_echo "CN: Old $CONF saved as /var/backups/`basename $CONF`.bak."
 fi
 
-# If there are some services enabled in /etc/inetd.conf, convert them to
-# temporary xinetd configuration file.
-#
-if [ -f "$DEFAULT" ]; then
-	inetd_compat="`egrep '^[[:space:]]*XINETD_OPTS[[:space:]]*=.*\-inetd_compat' "$DEFAULT"`" || true
-	if [ -z "$inetd_compat" ]; then
-		inetd_compat="`egrep '^[[:space:]]*INETD_COMPAT[[:space:]]*=[[:space:]]*[Yy][Ee][Ss]' "$DEFAULT"`" || true
-	fi
-fi
 CONFTMP=`mktemp $CONF.tmp.XXXXXX`
+temp_files="$CONFTMP"
 
-if [ -f "$INETDCONF" ] && [ -n "$inetd_compat" ]; then
+if [ -f "$INETDCONF" ]; then
 
 	# Convert inetd.conf to temporary xinetd.conf file using xconv.pl tool
 	/usr/sbin/xconv.pl < $INETDCONF > $CONFTMP
@@ -56,17 +58,23 @@ fi
 # Parse /etc/xinetd.conf file and convert services' configuration to
 # separated configurations in /etc/xinetd.d/ directory
 #
-services_list="`cat $CONF $CONFTMP | grep "^service " | uniq | sed 's/service //g'`" || true
+conffile_list="$CONFTMP"
+if [ -f "$CONF" ]; then
+	conffile_list="$CONF $conffile_list"
+fi
+services_list="`cat $conffile_list | grep "^service " | uniq | sed 's/service //g'`" || true
 
 if [ -n "$services_list" ]; then
+
     for service in $services_list; do
 
 	if [ -f "/etc/xinetd.d/$service" ]; then
-		cp_check_and_backup "/etc/xinetd.d/$service"
-		
-		if ! egrep -q "^# .* update by CARNet package" "/etc/xinetd.d/$service"; then
-			rm -f /etc/xinetd.d/$service
+
+		if cp_check_and_backup "/etc/xinetd.d/$service"; then
+			cp_echo "CN: Old /etc/xinetd.d/$service saved as /var/backups/`basename /etc/xinetd.d/$service`.bak."
 		fi
+
+	        rm -f /etc/xinetd.d/$service
 	fi
 
 	touch /etc/xinetd.d/$service || true
@@ -76,19 +84,25 @@ if [ -n "$services_list" ]; then
 	need_restart=1
     done
 
-    cp_echo ""
-    cp_echo "CN: All services were converted from $CONF file to separated configuration"
-    cp_echo "CN: files located in /etc/xinetd.d/ directory."
-
-    if [ -n "$inetd_compat" ]; then
-	cp_echo "CN: Since -inetd_compat option was active, all services in $INETDCONF"
-	cp_echo "CN: file were also moved to /etc/xinetd.d/ directory."
+    if egrep -q "service " "$CONFTMP"; then
+	cp_echo "CN: All services were converted from $INETDCONF file to separated"
+	cp_echo "CN: configuration files located in /etc/xinetd.d/ directory."
     fi
 
+    if [ -f "$CONF" ]; then
+	cp_echo "CN: All services were converted from $CONF file to separated"
+	cp_echo "CN: configuration files located in /etc/xinetd.d/ directory."
+    fi
 fi
 
 rm -f $CONFTMP
 
+# Configuration file /etc/xinetd.conf does not exist?
+#
+if [ ! -f "$CONF" ]; then
+	touch "$CONF"
+fi
+
 # Remove services from /etc/xinetd.conf file
 #
 cp_check_and_sed "^service " \
@@ -158,7 +172,6 @@ if [ $need_restart -eq 1 ]; then
 fi
 pgrep -u root -f /usr/sbin/xinetd > /dev/null || do=start
 
-cp_echo ""
 if [ -n "$do" ]; then
 
 	# Restart xinetd.
-- 
1.7.10.4