Backup konfiguracijskih datoteka u /var/backups/xinetd-cn/.
[xinetd-cn.git] / debian / postinst
index 84b2ca9..b7878b6 100644 (file)
@@ -12,42 +12,46 @@ PKG=xinetd-cn
 CONF=/etc/xinetd.conf
 INETDCONF=/etc/inetd.conf
 DEFAULT=/etc/default/xinetd
+BACKUPDIR=/var/backups/xinetd-cn
 
-did_inetd_backup=0
-did_backup=0
 need_restart=0
+temp_files=
+xinetd_conf_did=0
 
-# 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.
 #
-cp_echo ""
-if [ $did_inetd_backup -eq 1 ]; then
-       cp_echo "CN: Old $INETDCONF saved as /var/backups/`basename $INETDCONF`.bak."
-fi
-if [ $did_backup -eq 1 ]; then
-       cp_echo "CN: Old $CONF saved as /var/backups/`basename $CONF`.bak."
-fi
+trap cleanup 0 1 2 15
 
-# If there are some services enabled in /etc/inetd.conf, convert them to
-# temporary xinetd configuration file.
+# First, we do backup (inetd, xinetd)
 #
-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
+if cp_backup_conffile -r -d $BACKUPDIR -p $INETDCONF; then
+       cp_echo "CN: Old $INETDCONF saved as $BACKUPDIR/`basename $INETDCONF`.bak."
+fi
+if cp_backup_conffile -r -d $BACKUPDIR -p $CONF; then
+       cp_echo "CN: Old $CONF saved as $BACKUPDIR/`basename $CONF`.bak."
 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,43 +60,58 @@ 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
+       if egrep -q "^service[[:space:]]+" "$CONF"; then
+               conffile_list="$CONF $conffile_list"
+               xinetd_conf_did=1
+       fi
+fi
+services_list="`cat $conffile_list | egrep "^service[[:space:]]+" | sed -r 's/service[[:space:]]+//g' | uniq`" || 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_backup_conffile -r -d $BACKUPDIR -p /etc/xinetd.d/$service; then
+                       cp_echo "CN: Old /etc/xinetd.d/$service saved as $BACKUPDIR/`basename /etc/xinetd.d/$service`.bak."
                fi
+
+               rm -f /etc/xinetd.d/$service
        fi
 
        touch /etc/xinetd.d/$service || true
        # cat "$CONF" "$CONFTMP" | sed -n "/^service $service/,/^}/p" | cp-update "$PKG" "/etc/xinetd.d/$service"
-       cat "$CONF" "$CONFTMP" | sed -n "/^service $service/,/^}/p" >> "/etc/xinetd.d/$service"
+       cat $conffile_list | sed -rn "/^service[[:space:]]+$service/,/^}/p" >> "/etc/xinetd.d/$service"
 
        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[[:space:]]+" "$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 [ $xinetd_conf_did -eq 1 ]; 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 " \
-       "/^service/,/^}/d" \
+cp_check_and_sed "^service[[:space:]]+" \
+       "/^service[[:space:]]/,/^}/d" \
        "$CONF" && need_restart=1 || true
 
 # Check if there is no defaults block in /etc/xinetd.conf
@@ -158,7 +177,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.