#!/bin/bash
#
# Install Ivideon Client

set -eu

REPOS_ADDR="http://packages.ivideon.com/ubuntu"
REPO="ivideon"
TARGET="ivideon-client"
OPTIONS="th"
APP="IvideonClient"

usage() {
	echo "Usage: $0 [OPTION]..." 1>&2
	echo "Install ${APP}" 1>&2
	echo "Options:" 1>&2
	echo "    -t    use testing repository" 1>&2
	echo "    -h    display this help and exit" 1>&2
	exit 1
}

fail_message() {
    local message="$1"
    echo "${message}" 1>&2
    echo "For more information visit http://www.ivideon.com/ivideon-client-linux/" 1>&2
    exit 1
}

is_root() {
	[[ "$(id -u)" = "0" ]] || return 1
}

install_repositories() {
    wget "${REPOS_ADDR}/keys/${REPO}.list" -O "/etc/apt/sources.list.d/${REPO}.list"
    wget -O - "${REPOS_ADDR}/keys/${REPO}.key" | apt-key add -
}

install_client() {
    apt-get -qq update
    apt-get -ym install "${TARGET}"
}

main() {
    while getopts "${OPTIONS}" opt; do
        case "${opt}" in
            "t") REPO="${REPO}-testing" ;;
            "h" | *) usage ;;
        esac
    done

    if ! is_root; then
        fail_message "Administrative privileges is required to run this script."
    fi

    if ! install_repositories; then
        fail_message "Failed to setup ${REPO} APT repository."
    fi

    if install_client; then
        echo "${APP} is succesfully installed."
    else
        fail_message "Failed to install ${APP}."
    fi
}

main "$@"
