#!/bin/bash
set -x

SOURCES_FILE=/etc/apt/sources.list.d/zulip.list
zulip_source_hash=`shasum $SOURCES_FILE`

apt-get install -y lsb-release

SCRIPTS_PATH="$(dirname $(dirname $0))"
# Add the apt keys
apt-key add "$SCRIPTS_PATH"/setup/zulip-ppa.asc
apt-key add "$SCRIPTS_PATH"/setup/pgroonga-ppa.asc

release=$(lsb_release -sc)
if [ "$release" = "trusty" ] || [ "$release" = "xenial" ]; then
    cat >/etc/apt/sources.list.d/zulip.list <<EOF
deb http://ppa.launchpad.net/groonga/ppa/ubuntu $release main
deb http://ppa.launchpad.net/tabbott/zulip/ubuntu $release main
deb-src http://ppa.launchpad.net/groonga/ppa/ubuntu $release main
deb-src http://ppa.launchpad.net/tabbott/zulip/ubuntu $release main
EOF
else
    echo "Unsupported release $release."
    exit 1
fi

if [ "$zulip_source_hash" = "`shasum $SOURCES_FILE`" ]; then
    echo "zulip.list file did not change; skipping apt-get update"
else
    apt-get update
fi
