Binary upgrade script fixed (MD5 verification on AIX)
[public/netxms.git] / tools / sharIt
1 #!/usr/local/bin/bash
2 #
3 # vim:ts=3 sw=3
4
5 trap '
6 rm -f tmp
7 ' INT EXIT
8
9 if [ "x"$3 = "x" ]; then
10 echo "Usage: $0 input.tar.gz scriptname output.sh"
11 exit
12 fi
13
14 if [ ! -r $1 ]; then
15 echo "Can't read $1"
16 exit
17 fi
18
19 stub=`dirname \`echo $0\``/stub.sh
20 if [ ! -r $stub ]; then
21 echo "$stub not found!"
22 exit
23 fi
24
25 if [ "x`echo test | md5 2>/dev/null | tr A-Z a-z | cut -b1-32`" = 'xd8e8fca2dc0f896fd7cb4cb0031ba249' ]; then
26 md5="md5 | tr A-Z a-z | cut -b1-32"
27 fi
28 if [ "x$md5" = "x" ] && [ "x`echo test | md5sum 2>/dev/null | tr A-Z a-z | cut -b1-32`" = 'xd8e8fca2dc0f896fd7cb4cb0031ba249' ]; then
29 md5="md5sum | tr A-Z a-z | cut -b1-32"
30 fi
31 if [ "x$md5" = "x" ] && [ "x`echo test | csum -h MD5 - 2>/dev/null | tr A-Z a-z | cut -b1-32`" = 'xd8e8fca2dc0f896fd7cb4cb0031ba249' ]; then
32 md5="csum -h MD5 - | tr A-Z a-z | cut -b1-32"
33 fi
34 if [ "x$md5" = "x" ] && [ "x`echo test | openssl md5 2>/dev/null | tr A-Z a-z`" = 'xd8e8fca2dc0f896fd7cb4cb0031ba249' ]; then
35 md5="openssl md5 | tr A-Z a-z"
36 fi
37 if [ "x$md5" = "x" ] && [ "x`echo test | openssl md5 2>/dev/null | tr A-Z a-z | rev | cut -b1-32 | rev`" = 'xd8e8fca2dc0f896fd7cb4cb0031ba249' ]; then
38 md5="openssl md5 | tr A-Z a-z | rev | cut -b1-32 | rev"
39 fi
40
41 if [ -z "$md5" ]; then
42 echo "Can't calculate MD5, exiting"
43 exit
44 fi
45
46 skip=`echo \`wc -l $stub|awk '{ print $1 }'\``
47
48 if [ "x$BASH" == "x" ]; then
49 skip1=`let $skip+1`
50 else
51 skip1=$skip
52 let skip1++
53 fi
54
55 sed "s,__SKIP__,$skip,g" < $stub |
56 sed "s,__SKIP1__,$skip1,g" |
57 sed "s,__COMMAND__,$2,g" > tmp
58 hash1=`tail -n +5 tmp | $md5`
59 hash2=`cat $1 | $md5`
60 sed "s,__HASH1__,$hash1,g" < tmp | sed "s,__HASH2__,$hash2,g" > $3
61
62 cat $1 >> $3