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