r1: [svn-inject] Installing original source of xinetd-cn
[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 [ -f "$DEFAULT" ] && inetd_compat="`grep '^XINETD_OPTS.*=.*\-inetd_compat' "$DEFAULT"`" || true
43 CONFTMP=`mktemp $CONF.tmp.XXXXXX`
44
45 if [ -f "$INETDCONF" ] && [ -n "$inetd_compat" ]; then
46
47         # Convert inetd.conf to temporary xinetd.conf file using xconv.pl tool
48         /usr/sbin/xconv.pl < $INETDCONF > $CONFTMP
49 fi
50
51 # Parse /etc/xinetd.conf file and convert services' configuration to
52 # separated configurations in /etc/xinetd.d/ directory
53 #
54 services_list="`cat $CONF $CONFTMP | grep "^service " | uniq | sed 's/service //g'`" || true
55
56 if [ -n "$services_list" ]; then
57     for service in $services_list; do
58
59         if [ -f "/etc/xinetd.d/$service" ]; then
60                 cp_check_and_backup "/etc/xinetd.d/$service"
61                 
62                 if ! egrep -q "^# .* update by CARNet package" "/etc/xinetd.d/$service"; then
63                         rm -f /etc/xinetd.d/$service
64                 fi
65         fi
66
67         touch /etc/xinetd.d/$service || true
68         # cat "$CONF" "$CONFTMP" | sed -n "/^service $service/,/^}/p" | cp-update "$PKG" "/etc/xinetd.d/$service"
69         cat "$CONF" "$CONFTMP" | sed -n "/^service $service/,/^}/p" >> "/etc/xinetd.d/$service"
70
71         need_restart=1
72     done
73
74     cp_echo ""
75     cp_echo "CN: All services were converted from $CONF file to separated configuration"
76     cp_echo "CN: files located in /etc/xinetd.d/ directory."
77
78     if [ -n "$inetd_compat" ]; then
79         cp_echo "CN: Since -inetd_compat option was active, all services in $INETDCONF"
80         cp_echo "CN: file were also moved to /etc/xinetd.d/ directory."
81     fi
82
83 fi
84
85 rm -f $CONFTMP
86
87 # Remove services from /etc/xinetd.conf file
88 #
89 cp_check_and_sed "^service " \
90         "/^service/,/^}/d" \
91         "$CONF" && need_restart=1 || true
92
93 # Check if there is no defaults block in /etc/xinetd.conf
94 #
95 if ! egrep -q '^defaults' "$CONF"; then
96
97         echo -e "defaults\n{\n\n}" | cp-update "$PKG" "$CONF"
98         cp_echo "CN: Added defaults block to $CONF"
99         
100         need_restart=1
101 fi
102
103 # Check if /etc/xinetd.d/ directory is included or not
104 #
105 if ! egrep -q '^includedir /etc/xinetd.d' "$CONF"; then
106
107         echo "includedir /etc/xinetd.d" | cp-update "$PKG" "$CONF"
108         cp_echo "CN: Added \"includedir /etc/xinetd.d\" line to $CONF"
109         
110         need_restart=1
111 fi
112
113 # Remove -inetd_compat option from /etc/default/xinetd
114 #
115 if [ -f "$DEFAULT" ]; then
116         
117         # File exists, remove -inetd_compat option
118         cp_check_and_sed "^XINETD_OPTS.*\-inetd_compat" \
119                 "s/\-inetd_compat//g" \
120                 "$DEFAULT" && need_restart=1 || true
121
122         # Just in case there is no active XINETD_OPTS line
123         if ! egrep -q '^XINETD_OPTS' "$DEFAULT"; then
124
125                 echo 'XINETD_OPTS="-stayalive"' | cp-update "$PKG" "$DEFAULT"
126                 cp_echo "CN: Added -stayalive option to $DEFAULT."
127
128                 need_restart=1
129         fi
130 else
131         # File wasn't there at all?
132         touch $DEFAULT
133         echo 'XINETD_OPTS="-stayalive"' | cp-update "$PKG" "$DEFAULT"
134         cp_echo "CN: Created $DEFAULT file with -stayalive option."
135         
136         need_restart=1
137 fi
138
139 # Restart xinetd if needed
140 #
141 if [ $need_restart -eq 1 ]; then
142     do=restart
143 fi
144 pgrep -u root -f /usr/sbin/xinetd > /dev/null || do=start
145
146 cp_echo ""
147 if [ -n "$do" ]; then
148
149         # Restart xinetd.
150         if [ -x "/etc/init.d/xinetd" ]; then
151                 if [ -x "`which invoke-rc.d 2>/dev/null`" ]; then
152                     invoke-rc.d xinetd "$do" || exit $?
153                 else
154                     /etc/init.d/xinetd "$do" || exit $?
155                 fi
156         fi
157 fi
158
159 # Mail root
160 #
161 cp_mail "$PKG"
162
163 exit 0