* Tranzicijski paket za buduci prelazak na Debian Lenny.
[xinetd-cn.git] / debian / postinst
1 #!/bin/sh
2
3 set -e
4
5 [ "$1" = "configure" ] || exit 0
6 [ "$DEBIAN_SCRIPT_DEBUG" ] && set -vx
7
8 # Load CARNet Tools
9 . /usr/share/carnet-tools/functions.sh
10
11 PKG=xinetd-cn
12 CONF=/etc/xinetd.conf
13 INETDCONF=/etc/inetd.conf
14 DEFAULT=/etc/default/xinetd
15
16 did_inetd_backup=0
17 did_backup=0
18 need_restart=0
19
20 # First, we do backup (inetd, xinetd)
21 #
22 if cp_check_and_backup "$INETDCONF"; then
23         did_inetd_backup=1
24 fi
25 if cp_check_and_backup "$CONF"; then
26         did_backup=1
27 fi
28
29 # Print backup information
30 #
31 cp_echo ""
32 if [ $did_inetd_backup -eq 1 ]; then
33         cp_echo "CN: Old $INETDCONF saved as /var/backups/`basename $INETDCONF`.bak."
34 fi
35 if [ $did_backup -eq 1 ]; then
36         cp_echo "CN: Old $CONF saved as /var/backups/`basename $CONF`.bak."
37 fi
38
39 # If there are some services enabled in /etc/inetd.conf, convert them to
40 # temporary xinetd configuration file.
41 #
42 if [ -f "$DEFAULT" ]; then
43         inetd_compat="`egrep '^[[:space:]]*XINETD_OPTS[[:space:]]*=.*\-inetd_compat' "$DEFAULT"`" || true
44         if [ -z "$inetd_compat" ]; then
45                 inetd_compat="`egrep '^[[:space:]]*INETD_COMPAT[[:space:]]*=[[:space:]]*[Yy][Ee][Ss]' "$DEFAULT"`" || true
46         fi
47 fi
48 CONFTMP=`mktemp $CONF.tmp.XXXXXX`
49
50 if [ -f "$INETDCONF" ] && [ -n "$inetd_compat" ]; then
51
52         # Convert inetd.conf to temporary xinetd.conf file using xconv.pl tool
53         /usr/sbin/xconv.pl < $INETDCONF > $CONFTMP
54 fi
55
56 # Parse /etc/xinetd.conf file and convert services' configuration to
57 # separated configurations in /etc/xinetd.d/ directory
58 #
59 services_list="`cat $CONF $CONFTMP | grep "^service " | uniq | sed 's/service //g'`" || true
60
61 if [ -n "$services_list" ]; then
62     for service in $services_list; do
63
64         if [ -f "/etc/xinetd.d/$service" ]; then
65                 cp_check_and_backup "/etc/xinetd.d/$service"
66                 
67                 if ! egrep -q "^# .* update by CARNet package" "/etc/xinetd.d/$service"; then
68                         rm -f /etc/xinetd.d/$service
69                 fi
70         fi
71
72         touch /etc/xinetd.d/$service || true
73         # cat "$CONF" "$CONFTMP" | sed -n "/^service $service/,/^}/p" | cp-update "$PKG" "/etc/xinetd.d/$service"
74         cat "$CONF" "$CONFTMP" | sed -n "/^service $service/,/^}/p" >> "/etc/xinetd.d/$service"
75
76         need_restart=1
77     done
78
79     cp_echo ""
80     cp_echo "CN: All services were converted from $CONF file to separated configuration"
81     cp_echo "CN: files located in /etc/xinetd.d/ directory."
82
83     if [ -n "$inetd_compat" ]; then
84         cp_echo "CN: Since -inetd_compat option was active, all services in $INETDCONF"
85         cp_echo "CN: file were also moved to /etc/xinetd.d/ directory."
86     fi
87
88 fi
89
90 rm -f $CONFTMP
91
92 # Remove services from /etc/xinetd.conf file
93 #
94 cp_check_and_sed "^service " \
95         "/^service/,/^}/d" \
96         "$CONF" && need_restart=1 || true
97
98 # Check if there is no defaults block in /etc/xinetd.conf
99 #
100 if ! egrep -q '^defaults' "$CONF"; then
101
102         echo -e "defaults\n{\n\n}" | cp-update -m "$PKG" "$CONF"
103         cp_echo "CN: Added defaults block to $CONF"
104         
105         need_restart=1
106 fi
107
108 # Check if /etc/xinetd.d/ directory is included or not
109 #
110 if ! egrep -q '^includedir /etc/xinetd.d' "$CONF"; then
111
112         echo "includedir /etc/xinetd.d" | cp-update -m "$PKG" "$CONF"
113         cp_echo "CN: Added \"includedir /etc/xinetd.d\" line to $CONF"
114         
115         need_restart=1
116 fi
117
118 # Remove -inetd_compat option and set INETD_COMPAT to 'No' in /etc/default/xinetd
119 #
120 if [ -f "$DEFAULT" ]; then
121         
122         # File exists, set INETD_COMPAT to 'No'
123         cp_check_and_sed "^[[:space:]]*INETD_COMPAT.*[Yy][Ee][Ss]" \
124                 "s/^[[:space:]]*INETD_COMPAT.*[Yy][Ee][Ss]/INETD_COMPAT=No/g" \
125                 "$DEFAULT" && need_restart=1 || true
126
127         # File exists, remove -inetd_compat option
128         cp_check_and_sed "^[[:space:]]*XINETD_OPTS.*\-inetd_compat" \
129                 "s/\-inetd_compat//g" \
130                 "$DEFAULT" && need_restart=1 || true
131
132         # No INETD_COMPAT line?
133         if ! egrep -q '^[[:space:]]*INETD_COMPAT' "$DEFAULT"; then
134                 echo 'INETD_COMPAT=No' | cp-update -m "$PKG" "$DEFAULT"
135                 cp_echo "CN: Added 'INETD_COMPAT=No' to $DEFAULT."
136                 need_restart=1
137         fi
138
139         # Just in case there is no active XINETD_OPTS line
140         if ! egrep -q '^[[:space:]]*XINETD_OPTS' "$DEFAULT"; then
141                 echo 'XINETD_OPTS="-stayalive"' | cp-update -m "$PKG" "$DEFAULT"
142                 cp_echo "CN: Added -stayalive option to $DEFAULT."
143                 need_restart=1
144         fi
145 else
146         # File wasn't there at all?
147         touch $DEFAULT
148         echo -e 'INETD_COMPAT=No\nXINETD_OPTS="-stayalive"' | cp-update "$PKG" "$DEFAULT"
149         cp_echo "CN: Created $DEFAULT file with -stayalive enabled and -inetd_compat disabled."
150
151         need_restart=1
152 fi
153
154 # Restart xinetd if needed
155 #
156 if [ $need_restart -eq 1 ]; then
157     do=restart
158 fi
159 pgrep -u root -f /usr/sbin/xinetd > /dev/null || do=start
160
161 cp_echo ""
162 if [ -n "$do" ]; then
163
164         # Restart xinetd.
165         if [ -x "/etc/init.d/xinetd" ]; then
166                 if [ -x "`which invoke-rc.d 2>/dev/null`" ]; then
167                     invoke-rc.d xinetd "$do" || exit $?
168                 else
169                     /etc/init.d/xinetd "$do" || exit $?
170                 fi
171         fi
172 fi
173
174 # Mail root
175 #
176 cp_mail "$PKG"
177
178 exit 0