r18:
[carnet-tools-cn.git] / functions.sh
1 # by ddzeko & ico, Fri, 18 Mar 2005 14:44:08 +0100
2 cp_get_ifaddr() {
3   [ "$CP_SCRIPT_DEBUG" ] && set -vx
4   local ifaddr interface
5
6   interface="$1"
7   if [ -z "$interface" ]; then
8     cp_get_ifdefault dev
9     interface="$RET"
10     [ -z "$interface" ] && interface=lo
11   fi
12
13   if ! ifconfig $interface 2> /dev/null >> /dev/null; then
14     echo "cp_get_ifaddr: $interface: No such interface" 1>&2
15     return 1
16   fi
17
18   ifaddr="`/sbin/ifconfig $interface | awk '/inet/{ printf("%s\n",substr($2,index($2,":")+1)) }'`"
19
20   if [ -z $ifaddr ]; then
21     echo "cp_get_ifaddr: $interface: No such ipaddress" 1>&2
22     return 1
23   fi
24
25   RET="$ifaddr"
26   [ -z "$CP_ECHO_RETURN" ] || echo $RET
27 }
28
29 # by ddzeko & ico, Fri, 18 Mar 2005 14:44:08 +0100
30 cp_get_ifmask() {
31   [ "$CP_SCRIPT_DEBUG" ] && set -vx
32   local ifmask interface
33
34   interface="$1"
35   if [ -z "$interface" ]; then
36     cp_get_ifdefault dev
37     interface="$RET"
38     [ -z "$interface" ] && interface=lo
39   fi
40
41   if ! ifconfig $interface 2> /dev/null >> /dev/null; then
42     echo "cp_get_ifmask: $interface: No such interface" 1>&2
43     return 1
44   fi
45
46   ifmask="`/sbin/ifconfig $interface | awk '/Mask/{if($3~/Mask/)a=$3;else if ($4~/Mask/)a=$4;printf ("%s\n", substr(a,index(a,":")+1))}'`"
47
48   if [ -z "$ifmask" ]; then
49     echo "cp_get_ifmask: $interface: No such netmask" 1>&2
50     return 1
51   fi
52
53   RET="$ifmask"
54   [ -z "$CP_ECHO_RETURN" ] || echo $RET
55 }
56
57 # by ddzeko, Mon, 21 Mar 2005 07:00:22 +0100
58 cp_get_ifdefault() {
59   [ "$CP_SCRIPT_DEBUG" ] && set -vx
60   RET=""
61   case $1 in
62     dev)  RET=`route -n | awk '/^0.0.0.0/{print $8}'` ;;
63     addr) RET=`route -n | awk '/^0.0.0.0/{print $2}'` ;;
64     *)    echo "cp_get_ifdefault: Argument required (dev or addr)" >&2 ;;
65   esac
66   [ -z "$RET" -a "$1" -a "$CP_VERBOSE" ] && echo "cp_get_ifdefault: No default route" >&2
67   [ -z "$CP_ECHO_RETURN" ] || echo $RET
68   [ -z "$RET" ] && return 1
69 }
70
71 # by ico, Tue, 15 Mar 2005 14:04:21 +0100
72 cp_get_netaddr() {
73   [ "$CP_SCRIPT_DEBUG" ] && set -vx
74   local netmask ipaddress interface cidr echo_return
75   echo_return="$CP_ECHO_RETURN" # since we're making our own calls
76   # in our own way we need to set CP_ECHO_RETURN the way we want it
77   # but preserving initial state so it could be used afterwards
78   CP_ECHO_RETURN=""
79
80   interface="$1"
81   if [ -z "$interface" ]; then
82     cp_get_ifdefault dev
83     interface="$RET"
84     [ -z "$interface" ] && interface=lo
85   fi
86
87   if ! ifconfig $interface 2> /dev/null >> /dev/null; then
88     echo "cp_get_netaddr: $interface: No such interface" 1>&2
89     goto return1
90   fi
91
92   cp_get_ifaddr "$interface"
93   ipaddress="$RET"
94   if [ -z $ipaddress ]; then
95     echo "cp_get_netaddr: $interface: No such ipaddress" 1>&2
96     goto return1
97   fi
98   cp_get_ifmask "$interface"
99   netmask="$RET"
100   if [ -z $netmask ]; then
101     echo "cp_get_netaddr: $interface: No such netmask" 1>&2
102   fi
103
104   if [ "$netmask" = "255.255.255.255" ]; then
105     cidr="$ipaddress/32"
106   else 
107     cidr="`ipcalc -ncb $ipaddress $netmask | awk '/^Network:/{print $2}'`"
108   fi
109   
110   RET="$cidr"
111   [ -z "$echo_return" ] || echo $RET
112   CP_ECHO_RETURN="$echo_return"
113   return 0
114   
115   return1:
116     CP_ECHO_RETURN="$echo_return"
117     return 1
118 }
119
120 # by ico, Tue, 15 Mar 2005 14:04:21 +0100
121 cp_backup_conffile() {
122   [ "$CP_SCRIPT_DEBUG" ] && set -vx
123   local file_bak
124
125   if [ -z "$1" ]; then
126     echo "Usage: cp_backup_conffile <file>" 1>&2
127     return 1
128   fi
129   if [ ! -f "$1" ]; then
130     echo "cp_backup_conffile: $1: No such file" 1>&2
131     return 1
132   fi
133
134   file_bak="/var/backups/`basename $1`.bak"
135
136   if [ ! -f $file_bak ]; then
137     cp -pf "$1" $file_bak
138   else
139     if ! cmp -s "$1" $file_bak; then
140       /usr/bin/savelog -p -c 7 $file_bak > /dev/null 2> /dev/null
141       cp -pf $1 $file_bak
142     fi
143   fi
144 }
145
146 # by jelly, Tue, 15 Mar 2005 14:04:21 +0100
147 # A sed wrapper, to use instead of perl -pi -e
148 #  - relatively safe in-place s///g
149 #  - takes care of symlinks and ownership
150 # returns true if changed, false if nothing happened
151 #
152 cp_check_and_sed() {
153   [ "$CP_SCRIPT_DEBUG" ] && set -vx
154   local s sedcmd ret i
155   s="$1"
156   shift
157   sedcmd="$1"
158   shift
159   ret=2
160   for i in "$@"
161   do
162     [ -e "$i" ]        || continue
163     egrep -q "$s" "$i" || continue
164     [ -h "$i" ]        && i=$(readlink -f "$i")
165     sed "$sedcmd" "$i" > "$i.dpkg-tmp"
166     if ! cmp -s "$i" "$i.dpkg-tmp" 2>&1 >/dev/null; then
167       cp_safe_file_replace "$i.dpkg-tmp" "$i"
168     else
169       rm "$i.dpkg-tmp"
170     fi
171     ret=0
172   done
173   return $ret
174 }
175
176 # by jelly, Sun, 20 Mar 2005 20:12:19 +0100
177 cp_echo () {
178   [ "$CP_SCRIPT_DEBUG" ] && set -vx
179   if [ "x$1" = "x-mailonly" -o "x$1" = "x-m" ]; then
180     shift
181   else   
182     echo "$*"
183   fi
184   CP_NOTICE="$CP_NOTICE$1"
185   if [ `echo -n "$*" | wc -l` -eq 0 ]; then
186     CP_NOTICE="$CP_NOTICE
187 "
188   fi
189 }   
190
191 # by jelly, Sun, 20 Mar 2005 20:12:19 +0100
192 cp_mail () {
193   [ "$CP_SCRIPT_DEBUG" ] && set -vx
194   local pkg version quiet
195   if [ "x$1" = "x-q" ]; then
196     quiet=1
197     shift
198   fi
199   [ -n "$1" ] && pkg="$1"       # barf if no package
200   [ -n "$2" ] && version=" $2" || true
201   if [ "$(echo $CP_NOTICE|wc -w)" -gt 0 ]; then
202     [ -n "$quiet" ] || echo "Mailing upgrade output to root."
203     CP_NOTICE="From: $pkg postinst script <root>
204 To: root
205 Subject: $pkg$version package install log
206 Date: $(/bin/date +'%a, %d %b %Y %H:%M:%S %z')
207  
208 Hello!
209
210 The $pkg package has been successfully installed on your computer.
211 For your convenience, a partial output of the last $pkg installation
212 is included below.
213
214 ----------
215 ${CP_NOTICE}----------"
216   
217     echo "$CP_NOTICE" | /usr/sbin/sendmail -t &
218     sleep 1
219   fi
220 }
221
222 # by ddzeko, Mon, 21 Mar 2005 11:31:59 +0100
223 cp_safe_file_replace () {
224   [ "$CP_SCRIPT_DEBUG" ] && set -vx
225   local new old
226   if [ -z "$2" ]; then
227     echo "Usage: cp_safe_file_replace <new-file> <target-file>" 1>&2
228     return 1
229   fi
230   new="$1"
231   old="$2"
232   if [ -e "$old" ]; then
233     chown --reference "$old" "$new"
234     chmod --reference "$old" "$new"
235   fi
236   mv "$new" "$old"
237 }