#!/bin/sh

# Automatic online Hamachi installer.
# Copyright (C) 2009, 2010 Adam Mizerski <adam@mizerski.pl>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

name="hamachi"
version="0.9.9.9-20-lnx"
source_filename="$name-$version.tar.gz"
sha1_sum="c3dfb636ec6abbd8438dc8737fea957e3a854d14"

POST_MESSAGE="/var/adm/update-messages/hamachi-0.9.9.9-20.1.2-1"

if [ "`id -u`" != "0" ]; then
    echo "error: You must be root to use this program!"
    exit 1
fi

. /etc/sysconfig/proxy

if test "$PROXY_ENABLED" != "no"; then
    if test -n "$HTTP_PROXY" ; then
        export http_proxy="$HTTP_PROXY"
    fi
fi

tmpname=`basename $0`
tmpdir=`mktemp -d /tmp/$tmpname.XXXXXX`
trap "rm -rf $tmpdir" EXIT
if [ $? -ne 0 ]; then
    echo "$0: Can't create temp dir, exiting..."
    exit 4
fi

pushd $tmpdir &> /dev/null

cat > $source_filename.sha1 <<EOF
$sha1_sum  $source_filename
EOF

echo "Trying do download hamachi."

curl -s --speed-limit 3500 --speed-time 15 http://files.hamachi.cc/linux/$source_filename -o $source_filename

sha1sum -c $source_filename.sha1
if [ $? -eq 1 ]; then
    echo "Download failed."
    echo "*** Hamachi not installed. ***" | tee $POST_MESSAGE
    exit 1
fi

echo "Hamachi downloaded. Installing..."

tar -xf $source_filename
cd "$name-$version"

rm -f /usr/bin/hamachi{,-init}
install -m 755 hamachi /usr/bin
upx -d -qqq /usr/bin/hamachi
ln -sf /usr/bin/hamachi /usr/bin/hamachi-init

rm -rf /usr/share/doc/packages/hamachi
mkdir /usr/share/doc/packages/hamachi
cp CHANGES LICENSE LICENSE.openssh LICENSE.openssl README /usr/share/doc/packages/hamachi/

echo "*** Hamachi installed. ***" | tee $POST_MESSAGE

popd &> /dev/null
