#!/bin/sh
#
#  Copyright (C) 2002-2017 RealVNC Ltd.
#

#
# vncviewer_install - install VNC Viewer, man page and app icon
#

# Set up defaults and platform-specific overrides
dst=/usr/bin
mandst=/usr/share/man
share=/usr/share

if [ $# -gt 3 ]; then
    echo "usage: $0 [<installation-directory> [<man-page-directory> ] ]"
    exit 1
fi

# Installation directory
if [ $# -gt 0 ]; then
    dst=$1
    shift
fi

# Check installation directory exists
if [ '!' -d "$dst" ]; then
    echo "Installation directory $dst does not exist."
    exit 1
fi

# Man page directories
if [ $# -gt 0 ]; then
    mandst="$1"
    shift
fi
if [ '!' -d "$mandst" ] || [ '!' -w "$mandst" ]; then
    echo "Can't install manual pages to $mandst"
    mandst=""
else
    mandst="$mandst"/man1
fi

app=vncviewer
if [ '!' -f $app ]; then
    echo "Couldn't find $app"
else
  if cmp -s $app "$dst"/$app; then
          echo "$app hasn't changed"
    else
        echo "Copying $app to $dst"
        cp -f $app "$dst"
        chmod 0555 "$dst"/$app
    fi

    if [ -f $app.1 ]; then
        if [ -n "$mandst" ]; then
            if cmp -s $app.1 "$mandst"/$app.1; then
                echo "$app.1 hasn't changed"
            else
                echo "Copying $app.1 to $mandst/$app.1"
                cp -f $app.1 "$mandst"/$app.1
                chmod 0444 "$mandst"/$app.1
            fi
        fi
    fi
fi

icon="${app}48x48.png"
if [ '!' -f "icons/${icon}" ]; then
    echo "Couldn't find icon"
else
    mkdir -p "$share/icons/hicolor/48x48/apps/"
    cp -f "icons/${icon}" "$share/icons/hicolor/48x48/apps/${icon}"
    chmod 0444 "$share/icons/hicolor/48x48/apps/${icon}"
fi
if which gtk-update-icon-cache >/dev/null 2>&1; then
  gtk-update-icon-cache --quiet "$share/icons/hicolor" || true
fi
