#!/bin/sh -e

os=$(cat "$__global/explorer/os")
os_version=$(cat "$__global/explorer/os_version")

require=""
case $os in
    debian|devuan)
        case $os_version in
            8*|jessie)
                # Differntation not needed anymore
                apt_source_distribution=stable
                ;;
            9*|ascii/ceres|ascii)
                # Differntation not needed anymore
                apt_source_distribution=stable
                ;;
            10*)
                # Differntation not needed anymore
                apt_source_distribution=stable
                ;;
            *)
                echo "Don't know how to install Grafana on $os $os_version. Send us a pull request!" >&2
                exit 1
                ;;
        esac

        __apt_key_uri grafana \
            --name 'Grafana Release Signing Key' \
            --uri https://packages.grafana.com/gpg.key

        require="$require __apt_key_uri/grafana" __apt_source grafana \
                    --uri https://packages.grafana.com/oss/deb \
                    --distribution $apt_source_distribution \
                    --component main
        __package apt-transport-https
        require="$require __apt_source/grafana" __apt_update_index
        require="$require  __package/apt-transport-https __apt_update_index" __package grafana
        require="$require __package/grafana" __start_on_boot grafana-server
        require="$require __start_on_boot/grafana-server" __process grafana-server --start "service grafana-server start"
        ;;
    *)
        echo "Don't know how to install Grafana on $os. Send us a pull request!" >&2
        exit 1
        ;;
esac
