1cdab408826b1234af0898d179546080d17f9b2b
[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
10 trap '
11 echo "Upgrade script finished" >> $log
12 cd $wd
13 [ "x"$temp != "x" ] && rm -rf $temp
14 exit
15 ' INT EXIT
16
17 echo "$0 started: $*" > $log
18
19 wd=`pwd -P`
20 echo "Working directory: $wd" >> $log
21
22 _mktemp() {
23 d=/tmp/nxupdate.$$.UniQ
24 mkdir $d && echo $d || false
25 }
26
27 if [ "x`echo test | md5 2>/dev/null | tr A-Z a-z | cut -b1-32`" = 'xd8e8fca2dc0f896fd7cb4cb0031ba249' ]; then
28 md5="md5 | tr A-Z a-z | cut -b1-32"
29 fi
30 if [ "x$md5" = "x" ] && [ "x`echo test | md5sum 2>/dev/null | tr A-Z a-z | cut -b1-32`" = 'xd8e8fca2dc0f896fd7cb4cb0031ba249' ]; then
31 md5="md5sum | tr A-Z a-z | cut -b1-32"
32 fi
33 if [ "x$md5" = "x" ] && [ "x`echo test | csum -h MD5 - 2>/dev/null | tr A-Z a-z | cut -b1-32`" = 'xd8e8fca2dc0f896fd7cb4cb0031ba249' ]; then
34 md5="csum -h MD5 - | tr A-Z a-z | cut -b1-32"
35 fi
36 if [ "x$md5" = "x" ] && [ "x`echo test | openssl md5 2>/dev/null | tr A-Z a-z`" = 'xd8e8fca2dc0f896fd7cb4cb0031ba249' ]; then
37 md5="openssl md5 | tr A-Z a-z"
38 fi
39 if [ "x$md5" = "x" ] && [ "x`echo test | openssl md5 2>/dev/null | tr A-Z a-z | rev | cut -b1-32 | rev`" = 'xd8e8fca2dc0f896fd7cb4cb0031ba249' ]; then
40 md5="openssl md5 | tr A-Z a-z | rev | cut -b1-32 | rev"
41 fi
42
43 tail="tail -n"
44 case `uname -s` in
45 SunOS)
46 tail="tail"
47 mktemp() {
48 _mktemp $*
49 }
50 ;;
51 HP-UX)
52 alias mktemp=_mktemp
53 ;;
54 *)
55 ;;
56 esac
57
58 tmp=`which mktemp 2>/dev/null`
59 if [ $? = 0 ]; then
60 echo $tmp | grep "no mktemp in " >/dev/null 2>&1
61 [ $? = 0 ] && alias mktemp=_mktemp
62 else
63 alias mktemp=_mktemp
64 fi
65
66 if [ "x$md5" != "x" ]; then
67 if [ "X"`head -n $skip $0 |
68 $tail +5 |
69 $md5` != "X"$hash1 ];
70 then
71 echo "Script MD5 mismach; upgrade aborted" >> $log
72 exit
73 fi
74
75 if [ "X"`$tail +$skip1 $0 |
76 $md5` != "X"$hash2 ];
77 then
78 echo "Payload MD5 mismach; upgrade aborted" >> $log
79 exit
80 fi
81 else
82 echo "No md5/md5sum/csum/openssl found, can't check integrity" >> $log
83 fi
84
85 temp=`mktemp -d /tmp/nxinst.XXXXXX 2>/dev/null`
86 if [ $? != 0 ]; then
87 echo "Can't create temp dir" >> $log
88 exit
89 fi
90 cd $temp
91 $tail +$skip1 $wd/$0 | gzip -dc 2>/dev/null | tar xf -
92 if [ $? != 0 ]; then
93 echo "Can't unpack" >> $log
94 exit;
95 else
96 chmod +x ./$command
97 if [ $? != 0 ]; then
98 echo "Can't chmod $command" >> $log
99 exit
100 fi
101 echo Starting $command $*... >> $log
102 ./$command $* 2>&1 >> $log
103 fi
104
105 exit
106 ###################################