r12:
[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"; then
167       chown --reference "$i" "$i.dpkg-tmp"
168       chmod --reference "$i" "$i.dpkg-tmp"
169       mv "$i.dpkg-tmp" "$i"
170     else
171       rm "$i.dpkg-tmp"
172     fi
173     ret=0
174   done
175   return $ret
176 }
177
178 # by jelly, Sun, 20 Mar 2005 20:12:19 +0100
179 cp_echo () {
180   [ "$CP_SCRIPT_DEBUG" ] && set -vx
181   if [ "x$1" = "x-mailonly" -o "x$1" = "x-m" ]; then
182     shift
183   else   
184     echo "$*"
185   fi
186   CP_NOTICE="$CP_NOTICE$1"
187   if [ `echo -n "$*" | wc -l` -eq 0 ]; then
188     CP_NOTICE="$CP_NOTICE
189 "
190   fi
191 }   
192
193 # by jelly, Sun, 20 Mar 2005 20:12:19 +0100
194 cp_mail () {
195   [ "$CP_SCRIPT_DEBUG" ] && set -vx
196   local pkg version quiet
197   if [ "x$1" = "x-q" ]; then
198     quiet=1
199     shift
200   fi
201   [ -n "$1" ] && pkg="$1"       # barf if no package
202   [ -n "$2" ] && version=" $2" || true
203   if [ "$(echo $CP_NOTICE|wc -w)" -gt 0 ]; then
204     [ -n "$quiet" ] || echo "Mailing upgrade output to root."
205     CP_NOTICE="From: $pkg postinst script <root>
206 To: root
207 Subject: $pkg$version package install log
208 Date: $(/bin/date +'%a, %d %b %Y %H:%M:%S %z')
209  
210 Hello!
211
212 The $pkg package has been successfully installed on your computer.
213 For your convenience, a partial output of the last $pkg installation
214 is included below.
215
216 ----------
217 ${CP_NOTICE}----------"
218   
219     echo "$CP_NOTICE" | /usr/sbin/sendmail -t &
220     sleep 1
221   fi
222 }