e65d51036b051900cff9b1d3d70e49ee3c572382
[public/netxms.git] / tools / stub.sh
1 #!/bin/sh
2
3 hash1=__HASH1__
4 hash2=__HASH2__
5 skip=__SKIP__
6 skip1=__SKIP1__
7 command=__COMMAND__
8 log=/tmp/nxagentupdate.log
9 md5found=no
10 postproc1="cut -b1-32"
11 postproc2="cat"
12
13 trap '
14 echo "Upgrade script finished" >> $log
15 cd $wd
16 [ "x"$temp != "x" ] && rm -rf $temp
17 exit
18 ' INT EXIT
19
20 echo "$0 started: $*" > $log
21
22 wd=`pwd -P`
23 echo "Working directory: $wd" >> $log
24
25 _mktemp() {
26 d=/tmp/nxupdate.$$.UniQ
27 mkdir $d && echo $d || false
28 }
29
30 for app in openssl md5sum md5; do
31 tmp=`which $app 2>/dev/null`
32 if [ $? = 0 ]; then
33 echo $tmp | grep "no $app in " >/dev/null 2>&1
34 if [ $? != 0 ]; then
35 md5=$tmp
36 md5found=yes
37 break
38 fi
39 fi
40 done
41
42 if [ -z "$md5" ]; then
43 for dir in /bin /sbin /usr/bin /usr/local/bin /opt/openssl*/bin /opt/freeware/bin; do
44 if [ -d $dir ]; then
45 for app in openssl md5sum md5; do
46 if [ -x "$dir/$app" ]; then
47 md5="$dir/$app"
48 md5found=yes
49 break
50 fi
51 done
52 [ "x" != "x$md5" ] && break
53 fi
54 done
55 fi
56
57 if [ "`basename $md5`" = "openssl" ]; then
58 md5="$md5 md5"
59 postproc1="cut -d = -f 2"
60 postproc2="cut -b2-33"
61 fi
62
63 tail="tail -n"
64 case `uname -s` in
65 SunOS)
66 tail="tail"
67 mktemp() {
68 _mktemp $*
69 }
70 ;;
71 HP-UX)
72 alias mktemp=_mktemp
73 ;;
74 *)
75 ;;
76 esac
77
78 tmp=`which mktemp 2>/dev/null`
79 if [ $? = 0 ]; then
80 echo $tmp | grep "no mktemp in " >/dev/null 2>&1
81 [ $? = 0 ] && alias mktemp=_mktemp
82 else
83 alias mktemp=_mktemp
84 fi
85
86 if [ "$md5found" = "yes" ]; then
87 if [ "X"`head -n $skip $0 |
88 $tail +5 |
89 $md5 |
90 $postproc1 |
91 $postproc2 |
92 tr A-Z a-z` != "X"$hash1 ];
93 then
94 echo "Script MD5 mismach; upgrade aborted" >> $log
95 exit
96 fi
97
98 if [ "X"`$tail +$skip1 $0 |
99 $md5 |
100 $postproc1 |
101 $postproc2 |
102 tr A-Z a-z` != "X"$hash2 ];
103 then
104 echo "Payload MD5 mismach; upgrade aborted" >> $log
105 exit
106 fi
107 else
108 echo "No md5/md5sum/openssl found, can't check integrity" >> $log
109 fi
110
111 temp=`mktemp -d /tmp/nxinst.XXXXXX 2>/dev/null`
112 if [ $? != 0 ]; then
113 echo "Can't create temp dir" >> $log
114 exit
115 fi
116 cd $temp
117 $tail +$skip1 $wd/$0 | gzip -dc 2>/dev/null | tar xf -
118 if [ $? != 0 ]; then
119 echo "Can't unpack" >> $log
120 exit;
121 else
122 chmod +x ./$command
123 if [ $? != 0 ]; then
124 echo "Can't chmod $command" >> $log
125 exit
126 fi
127 echo Starting $command $*... >> $log
128 ./$command $* 2>&1 >> $log
129 fi
130
131 exit
132 ###################################