yet-yet-another solaris8 hack
[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 md5=`which md5 2>/dev/null`
26 if [ $? != 0 ]; then
27 md5=`which md5sum 2>/dev/null`
28 if [ $? != 0 ]; then
29 md5=`which openssl 2>/dev/null`
30 if [ $? != 0 ]; then
31 echo "Can't calculate MD5, exiting"
32 exit
33 else
34 md5="$md5 md5"
35 fi
36 fi
37 fi
38
39 skip=`echo \`wc -l stub.sh|cut -d " " -f 1\``
40
41 if [ "x$BASH" == "x" ]; then
42 skip1=`let $skip+1`
43 else
44 skip1=$skip
45 let skip1++
46 fi
47
48 sed "s,__SKIP__,$skip,g" < $stub |
49 sed "s,__SKIP1__,$skip1,g" |
50 sed "s,__COMMAND__,$2,g" > tmp
51 hash1=`tail -n +5 tmp | $md5 | cut -b1-32 | tr A-Z a-z`
52 hash2=`cat $1 | $md5 | cut -b1-32 | tr A-Z a-z`
53 sed "s,__HASH1__,$hash1,g" < tmp | sed "s,__HASH2__,$hash2,g" > $3
54
55 cat $1 >> $3