#!/bin/sh

NAME=2gis
VERSION=4.15.2~shv280
ALT_VER=4.15.2-0trusty1+shv280+r339
ARCH=amd64

POST_MESSAGE="/var/adm/update-messages/__NAME-4.15.2~shv280-7.1-1"

SERVER="http://deb.2gis.ru/pool/non-free/2/$NAME"
FILE="${NAME}_${ALT_VER}_${ARCH}.deb"

CURL_OPTIONS="-L -s --speed-limit 3500 --speed-time 15"

if ( ! which id > /dev/null ) || ( ! which tar > /dev/null ); then
    echo "Running in non-chrooted (install into directory) mode... Exit safely."
    exit 0
fi

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

. "/etc/sysconfig/proxy"

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

if [ -z $http_proxy ]; then
    echo
    echo "note: No proxy is used. Please set the environment variable \"http_proxy\""
    echo "note: to your favourite proxy if you want to use a proxy for the download."
    echo "note:"
    echo "note:   sh: export http_proxy=\"http://proxy.example.org:3128/\""
    echo "note:   csh: setenv http_proxy \"http://proxy.example.org:3128/\""
fi

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

cd "$tmpdir"

for server in $useserver $SERVER; do
    archive="$server/$FILE"
    rm -f "$FILE"
    echo "$FILE ($archive):"
    echo -n "  Fetching   ... "
    curl $CURL_OPTIONS -o "$FILE" "$archive"
    if [ $? -ne 0 ]; then
        rm -f "$FILE"
        echo "failed ... deleted!"
        success=false
        continue
    fi
    success=true
    echo "done"
    break
done

if [ "$success" != false ]; then
    # Extract.
    ar -x "$FILE"
    tar -xf data.tar.xz
    # Install into system root.
    install -Dpm 0755 "./usr/bin/$NAME" "/usr/bin/$NAME"
    cp -aT "./usr/lib/2GIS/v4" "/usr/lib64/$NAME/v4"
    rm -f "/usr/lib/2GIS"
    for lib in libpthread.so.0 libm.so.6 libdl.so.2; do
        rm -f "/usr/lib64/2gis/v4/lib/$lib"
    done
    ln -s "/usr/lib64/$NAME" "/usr/lib/2GIS"
    cp -aT "./usr/share/2GIS" "/usr/share/2GIS"
    install -Dpm 0644 "./usr/share/applications/$NAME.desktop" "/usr/share/applications/$NAME.desktop"

    install -Dpm 0644 "./usr/share/icons/hicolor/48x48/apps/$NAME.png" \
      "/usr/share/icons/hicolor/48x48/apps/$NAME.png"
    install -Dpm 0644 "./usr/share/icons/hicolor/scalable/apps/$NAME.svg" \
      "/usr/share/icons/hicolor/scalable/apps/$NAME.svg"

    install -Dpm 0644 "./usr/share/man/man1/$NAME.1.gz" "/usr/share/man/man1/$NAME.1.gz"

    install -Dpm 0644 "./usr/share/doc/$NAME/copyright" "/usr/share/doc/packages/$NAME/COPYING"
    gzip -d "./usr/share/doc/$NAME/changelog.Debian.gz"
    install -Dpm 0644 "./usr/share/doc/$NAME/changelog.Debian" "/usr/share/doc/packages/$NAME/ChangeLog"

    chown root:doublegis "/usr/bin/$NAME"
    chmod g+s "/usr/bin/$NAME"

    gtk-update-icon-cache --quiet --force "/usr/share/icons/hicolor/"

    echo "*** 2GIS files installed. ***" | tee $POST_MESSAGE
else
    echo "*** No 2GIS files installed. ***" | tee $POST_MESSAGE
fi

if [ -f "/usr/share/doc/packages/$NAME/COPYING" ]; then
    cat "/usr/share/doc/packages/$NAME/COPYING" | tee $POST_MESSAGE
fi
