+#!/bin/sh
+
+set -e
+
+[ "$1" = "configure" ] || exit 0
+[ "$DEBIAN_SCRIPT_DEBUG" ] && set -vx
+
+# Load CARNet Tools
+. /usr/share/carnet-tools/functions.sh
+
+PKG=xinetd-cn
+CONF=/etc/xinetd.conf
+INETDCONF=/etc/inetd.conf
+DEFAULT=/etc/default/xinetd
+
+did_inetd_backup=0
+did_backup=0
+need_restart=0
+
+# First, we do backup (inetd, xinetd)
+#
+if cp_check_and_backup "$INETDCONF"; then
+ did_inetd_backup=1
+fi
+if cp_check_and_backup "$CONF"; then
+ did_backup=1
+fi
+
+# Print backup information
+#
+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
+
+# If there are some services enabled in /etc/inetd.conf, convert them to
+# temporary xinetd configuration file.
+#
+[ -f "$DEFAULT" ] && inetd_compat="`grep '^XINETD_OPTS.*=.*\-inetd_compat' "$DEFAULT"`" || true
+CONFTMP=`mktemp $CONF.tmp.XXXXXX`
+
+if [ -f "$INETDCONF" ] && [ -n "$inetd_compat" ]; then
+
+ # Convert inetd.conf to temporary xinetd.conf file using xconv.pl tool
+ /usr/sbin/xconv.pl < $INETDCONF > $CONFTMP
+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
+
+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
+ fi
+ 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"
+
+ 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."
+ fi
+
+fi
+
+rm -f $CONFTMP
+
+# Remove services from /etc/xinetd.conf file
+#
+cp_check_and_sed "^service " \
+ "/^service/,/^}/d" \
+ "$CONF" && need_restart=1 || true
+
+# Check if there is no defaults block in /etc/xinetd.conf
+#
+if ! egrep -q '^defaults' "$CONF"; then
+
+ echo -e "defaults\n{\n\n}" | cp-update "$PKG" "$CONF"
+ cp_echo "CN: Added defaults block to $CONF"
+
+ need_restart=1
+fi
+
+# Check if /etc/xinetd.d/ directory is included or not
+#
+if ! egrep -q '^includedir /etc/xinetd.d' "$CONF"; then
+
+ echo "includedir /etc/xinetd.d" | cp-update "$PKG" "$CONF"
+ cp_echo "CN: Added \"includedir /etc/xinetd.d\" line to $CONF"
+
+ need_restart=1
+fi
+
+# Remove -inetd_compat option from /etc/default/xinetd
+#
+if [ -f "$DEFAULT" ]; then
+
+ # File exists, remove -inetd_compat option
+ cp_check_and_sed "^XINETD_OPTS.*\-inetd_compat" \
+ "s/\-inetd_compat//g" \
+ "$DEFAULT" && need_restart=1 || true
+
+ # Just in case there is no active XINETD_OPTS line
+ if ! egrep -q '^XINETD_OPTS' "$DEFAULT"; then
+
+ echo 'XINETD_OPTS="-stayalive"' | cp-update "$PKG" "$DEFAULT"
+ cp_echo "CN: Added -stayalive option to $DEFAULT."
+
+ need_restart=1
+ fi
+else
+ # File wasn't there at all?
+ touch $DEFAULT
+ echo 'XINETD_OPTS="-stayalive"' | cp-update "$PKG" "$DEFAULT"
+ cp_echo "CN: Created $DEFAULT file with -stayalive option."
+
+ need_restart=1
+fi
+
+# Restart xinetd if needed
+#
+if [ $need_restart -eq 1 ]; then
+ do=restart
+fi
+pgrep -u root -f /usr/sbin/xinetd > /dev/null || do=start
+
+cp_echo ""
+if [ -n "$do" ]; then
+
+ # Restart xinetd.
+ if [ -x "/etc/init.d/xinetd" ]; then
+ if [ -x "`which invoke-rc.d 2>/dev/null`" ]; then
+ invoke-rc.d xinetd "$do" || exit $?
+ else
+ /etc/init.d/xinetd "$do" || exit $?
+ fi
+ fi
+fi
+
+# Mail root
+#
+cp_mail "$PKG"
+
+exit 0