1 # by ddzeko & ico, Fri, 18 Mar 2005 14:44:08 +0100
3 [ "$CP_SCRIPT_DEBUG" ] && set -vx
7 if [ -z "$interface" ]; then
10 [ -z "$interface" ] && interface=lo
13 if ! ifconfig $interface 2> /dev/null >> /dev/null; then
14 echo "cp_get_ifaddr: $interface: No such interface" 1>&2
18 ifaddr="`/sbin/ifconfig $interface | awk '/inet/{ printf("%s\n",substr($2,index($2,":")+1)) }'`"
20 if [ -z $ifaddr ]; then
21 echo "cp_get_ifaddr: $interface: No such ipaddress" 1>&2
26 [ -z "$CP_ECHO_RETURN" ] || echo $RET
29 # by ddzeko & ico, Fri, 18 Mar 2005 14:44:08 +0100
31 [ "$CP_SCRIPT_DEBUG" ] && set -vx
32 local ifmask interface
35 if [ -z "$interface" ]; then
38 [ -z "$interface" ] && interface=lo
41 if ! ifconfig $interface 2> /dev/null >> /dev/null; then
42 echo "cp_get_ifmask: $interface: No such interface" 1>&2
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))}'`"
48 if [ -z "$ifmask" ]; then
49 echo "cp_get_ifmask: $interface: No such netmask" 1>&2
54 [ -z "$CP_ECHO_RETURN" ] || echo $RET
57 # by ddzeko, Mon, 21 Mar 2005 07:00:22 +0100
59 [ "$CP_SCRIPT_DEBUG" ] && set -vx
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 ;;
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
71 # by ico, Tue, 15 Mar 2005 14:04:21 +0100
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
81 if [ -z "$interface" ]; then
84 [ -z "$interface" ] && interface=lo
87 if ! ifconfig $interface 2> /dev/null >> /dev/null; then
88 echo "cp_get_netaddr: $interface: No such interface" 1>&2
92 cp_get_ifaddr "$interface"
94 if [ -z $ipaddress ]; then
95 echo "cp_get_netaddr: $interface: No such ipaddress" 1>&2
98 cp_get_ifmask "$interface"
100 if [ -z $netmask ]; then
101 echo "cp_get_netaddr: $interface: No such netmask" 1>&2
104 if [ "$netmask" = "255.255.255.255" ]; then
107 cidr="`ipcalc -ncb $ipaddress $netmask | awk '/^Network:/{print $2}'`"
111 [ -z "$echo_return" ] || echo $RET
112 CP_ECHO_RETURN="$echo_return"
116 CP_ECHO_RETURN="$echo_return"
120 # by ico, Tue, 15 Mar 2005 14:04:21 +0100
121 cp_backup_conffile() {
122 [ "$CP_SCRIPT_DEBUG" ] && set -vx
126 echo "Usage: cp_backup_conffile <file>" 1>&2
129 if [ ! -f "$1" ]; then
130 echo "cp_backup_conffile: $1: No such file" 1>&2
134 file_bak="/var/backups/`basename $1`.bak"
136 if [ ! -f $file_bak ]; then
137 cp -pf "$1" $file_bak
139 if ! cmp -s "$1" $file_bak; then
140 /usr/bin/savelog -p -c 7 $file_bak > /dev/null 2> /dev/null
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
153 [ "$CP_SCRIPT_DEBUG" ] && set -vx
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"
176 # by jelly, Sun, 20 Mar 2005 20:12:19 +0100
178 [ "$CP_SCRIPT_DEBUG" ] && set -vx
179 if [ "x$1" = "x-mailonly" -o "x$1" = "x-m" ]; then
184 CP_NOTICE="$CP_NOTICE$1"
185 if [ `echo -n "$*" | wc -l` -eq 0 ]; then
186 CP_NOTICE="$CP_NOTICE
191 # by jelly, Sun, 20 Mar 2005 20:12:19 +0100
193 [ "$CP_SCRIPT_DEBUG" ] && set -vx
194 local pkg version quiet
195 if [ "x$1" = "x-q" ]; then
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>
205 Subject: $pkg$version package install log
206 Date: $(/bin/date +'%a, %d %b %Y %H:%M:%S %z')
210 The $pkg package has been successfully installed on your computer.
211 For your convenience, a partial output of the last $pkg installation
215 ${CP_NOTICE}----------"
217 echo "$CP_NOTICE" | /usr/sbin/sendmail -t &
222 # by ddzeko, Mon, 21 Mar 2005 11:31:59 +0100
223 cp_safe_file_replace () {
224 [ "$CP_SCRIPT_DEBUG" ] && set -vx
228 if [ -e "$old" ]; then
229 chown --reference "$old" "$new"
230 chmod --reference "$old" "$new"