works on linux
authorAlex Kirhenshtein <alk@netxms.org>
Tue, 1 Feb 2005 18:46:00 +0000 (18:46 +0000)
committerAlex Kirhenshtein <alk@netxms.org>
Tue, 1 Feb 2005 18:46:00 +0000 (18:46 +0000)
tools/sharIt
tools/stub.sh

index b8e3318..60e0d0a 100755 (executable)
@@ -9,28 +9,41 @@ if [ "x"$3 = "x" ]; then
        exit
 fi
 
-[ -r $1 ] || echo "Can't read $1" && exit
+if [ ! -r $1 ]; then
+       echo "Can't read $1"
+       exit
+fi
 
-md5=`which md5`
+md5=`which md5 2>/dev/null`
 if [ $? != 0 ]; then
-       md5=`which md5sum`
+       md5=`which md5sum 2>/dev/null`
        if [ $? != 0 ]; then
-               md5=`which openssl`
+               md5=`which openssl 2>/dev/null`
                if [ $? != 0 ]; then
                        echo "Can't calculate MD5, exiting"
                        exit
                else
                        md5="$md5 md5"
                fi
-       else
-               md5="$md5 | cut -d' ' -f1"
        fi
 fi
 
-skip=`echo \`wc -l stub.sh|cut -b1-8\``
+case `uname -s` in
+       Linux)
+               skip=`wc -l stub.sh|cut -d' ' -f1`
+               ;;
+       *BSD)
+               skip=`echo \`wc -l stub.sh|cut -b1-8\``
+               ;;
+       *)
+               echo unknown system, duh
+               exit;
+               ;;
+esac
+
 sed "s,__SKIP__,$skip,g" < stub.sh | sed "s,__COMMAND__,$2,g" > tmp
-hash1=`tail -n+5 tmp | $md5`
-hash2=`cat $1 | $md5`
+hash1=`tail -n+5 tmp | $md5 | cut -b1-32 | tr A-Z a-z`
+hash2=`cat $1 | $md5 | cut -b1-32 | tr A-Z a-z`
 sed "s,__HASH1__,$hash1,g" < tmp | sed "s,__HASH2__,$hash2,g" > $3
 
 cat $1 >> $3
index 5a380d3..1c35a6b 100755 (executable)
@@ -7,32 +7,30 @@ command=__COMMAND__
 
 trap '
        echo Cleanup...
-       #[ "x"$temp != "x" ] && rm -rf $temp
+       [ "x"$temp != "x" ] && rm -rf $temp
        exit
 ' INT EXIT
 
-md5=`which md5`
+md5=`which md5 2>/dev/null`
 if [ $? != 0 ]; then
-       md5=`which md5sum`
+       md5=`which md5sum 2>/dev/null`
        if [ $? != 0 ]; then
-               md5=`which openssl`
+               md5=`which openssl 2>/dev/null`
                if [ $? != 0 ]; then
                        echo "Can't calculate MD5, exiting"
                        exit
                else
                        md5="$md5 md5"
                fi
-       else
-               md5="$md5 | cut -d' ' -f1"
        fi
 fi
 
-if [ "X"`head -n$skip $0 | tail -n +5 | $md5 ` != "X"$hash1 ]; then
+if [ "X"`head -n$skip $0 | tail -n +5 | $md5 | cut -b1-32 | tr A-Z a-z` != "X"$hash1 ]; then
        echo Script MD5 mismach
        exit
 fi
-skip=`let "$skip+1"`
-if [ "X"`tail -n +$skip $0 | $md5 ` != "X"$hash2 ]; then
+let "skip=skip+1" >/dev/null
+if [ "X"`tail -n +$skip $0 | $md5 | cut -b1-32 | tr A-Z a-z` != "X"$hash2 ]; then
        echo Payload MD5 mismach
        exit
 fi
@@ -48,7 +46,11 @@ if [ $? != 0 ]; then
        exit;
 else
        cd $temp
-       chmod +x ./$command || echo "Can't chmod $command" && exit
+       chmod +x ./$command
+       if [ $? != 0 ]; then
+               echo "Can't chmod $command"
+               exit
+       fi
        ./$command
 fi