Various fixes
[public/netxms.git] / tools / sharIt
1 #!/usr/local/bin/bash
2
3 #trap '
4 # rm -f tmp
5 #' INT EXIT
6
7 if [ "x"$3 = "x" ]; then
8 echo "Usage: $0 input.tar.gz scriptname output.sh"
9 exit
10 fi
11
12 if [ ! -r $1 ]; then
13 echo "Can't read $1"
14 exit
15 fi
16
17 md5=`which md5 2>/dev/null`
18 if [ $? != 0 ]; then
19 md5=`which md5sum 2>/dev/null`
20 if [ $? != 0 ]; then
21 md5=`which openssl 2>/dev/null`
22 if [ $? != 0 ]; then
23 echo "Can't calculate MD5, exiting"
24 exit
25 else
26 md5="$md5 md5"
27 fi
28 fi
29 fi
30
31 case `uname -s` in
32 *BSD)
33 skip=`echo \`wc -l stub.sh|cut -b1-8\``
34 ;;
35 *)
36 skip=`cat stub.sh | wc -l | sed 's,^ *,,'`
37 ;;
38 esac
39 if [ "x$BASH" == "x" ]; then
40 skip1=`let $skip+1`
41 else
42 skip1=$skip
43 let skip1++
44 fi
45
46 sed "s,__SKIP__,$skip,g" < stub.sh |
47 sed "s,__SKIP1__,$skip1,g" |
48 sed "s,__COMMAND__,$2,g" > tmp
49 hash1=`tail -n +5 tmp | $md5 | cut -b1-32 | tr A-Z a-z`
50 hash2=`cat $1 | $md5 | cut -b1-32 | tr A-Z a-z`
51 sed "s,__HASH1__,$hash1,g" < tmp | sed "s,__HASH2__,$hash2,g" > $3
52
53 cat $1 >> $3