- TODO updated
[public/netxms.git] / tools / stub.sh
1 #!/bin/sh
2
3 hash1=__HASH1__
4 hash2=__HASH2__
5 skip=__SKIP__
6 command=__COMMAND__
7 log="/tmp/agent_upgrade_log"
8 elog="/tmp/agent_upgrade_log.stderr"
9
10 trap '
11 echo "Upgrade script finished" >> $log
12 [ "x"$temp != "x" ] && rm -rf $temp
13 exit
14 ' INT EXIT
15
16 rm -f $log $elog
17
18 md5=`which md5 2>/dev/null`
19 if [ $? != 0 ]; then
20 md5=`which md5sum 2>/dev/null`
21 if [ $? != 0 ]; then
22 md5=`which openssl 2>/dev/null`
23 if [ $? != 0 ]; then
24 echo "Can't calculate MD5, exiting" >> $log
25 exit
26 else
27 md5="$md5 md5"
28 fi
29 fi
30 fi
31
32 if [ "X"`head -n$skip $0 | tail -n +5 | $md5 | cut -b1-32 | tr A-Z a-z` != "X"$hash1 ]; then
33 echo "Script MD5 mismach" >> $log
34 exit
35 fi
36
37 let "skip=skip+1" 2>/dev/null
38 if [ $? != 0 ]; then
39 # real sh?
40 skip=`let "$skip+1"`
41 fi
42
43 if [ "X"`tail -n +$skip $0 | $md5 | cut -b1-32 | tr A-Z a-z` != "X"$hash2 ]; then
44 echo "Payload MD5 mismach" >> $log
45 exit
46 fi
47
48 temp=`mktemp -d /tmp/nxinst.XXXXXX 2>/dev/null`
49 if [ $? != 0 ]; then
50 echo "Can't create temp dir" >> $log
51 exit 0
52 fi
53 tail -n +$skip $0 | gzip -dc 2>/dev/null | tar xf - -C $temp
54 if [ $? != 0 ]; then
55 echo "Can't unpack" >> $log
56 exit;
57 else
58 cd $temp
59 chmod +x ./$command
60 if [ $? != 0 ]; then
61 echo "Can't chmod $command" >> $log
62 exit
63 fi
64 ./$command >> $log 2> $elog
65 fi
66
67 exit
68 ###################################