#!/bin/sh -e
# This program is free software: you can redistribute it and/or modify it
# under the terms of the the GNU General Public License version 3, as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
# PURPOSE.  See the applicable version of the GNU General Public
# License for more details.
#.
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Copyright (C) 2014 Canonical, Ltd.

usage () {
cat <<EOF
usage: $0 COMMAND SILO-NUMBER [DEVICE-PASSWORD] [DISTRIBUTION]
$1

COMMANDS:
    host-install        Deprecated. Please use host-upgrade instead.
    device-install      Deprecated. Please use device-upgrade instead.
    host-upgrade        Upgrades your host machine with packages from the silo.
    device-upgrade      Upgrades your connected device with packages from the silo.
    host-purge          Uses ppa-purge to uninstall the silo contents from the host machine.
    device-purge        Not implemented.

DISTRIBUTION: (optional)
    ubuntu
    ubuntu-rtm
EOF
exit 1
}

[ -x /usr/bin/sudo               ] || { echo "Please install 'sudo'"; exit 1; }
[ -x /usr/bin/add-apt-repository ] || { echo "Please install 'software-properties-common'"; exit 1; }

if [ -f "$(dirname $0)/shell-adb-common.sh" ]; then
    . "$(dirname $0)/shell-adb-common.sh"
else
    . "/usr/share/phabletutils/shell-adb-common.sh"
fi

# Defaults
PPA="ppa:ci-train-ppa-service"

# Read the first positional argument.
COMMAND="$1"
[ $# -gt 0 ] && shift || usage "Missing COMMAND"

# Check that silo number is really a number.
echo "$1" | egrep -q "^[0-9]{1,3}$" || usage "SILO-NUMBER not a number."

# Read the second positional argument.
SILO=landing-$(echo "000$1" | rev | cut -c -3 | rev)
[ $# -gt 0 ] && shift || usage "Missing SILO-NUMBER."

case "$COMMAND" in
    host-upgrade)
        set -x
        sudo add-apt-repository $PPA/$SILO
        sudo apt-get update -qq
        sudo apt-get dist-upgrade --yes
        ;;

    device-upgrade)
        check_devices
        # Read the third positional argument.
        PASSWORD="$1"
        [ $# -gt 0 ] && shift || usage "Missing DEVICE-PASSWORD"

        set -x
        adb shell egrep ^deb /etc/apt/sources.list.d/\*.list
        DISTRO="$1"
        if [ -z $DISTRO ]; then
            # In case no distro specified, guess
            if adb shell system-image-cli -i | grep -q ubuntu-rtm; then
                DISTRO="ubuntu-rtm"
            else
                DISTRO="ubuntu"
            fi
        fi
        adb shell "echo -e '#\x21/bin/sh\necho $PASSWORD' >/tmp/askpass.sh"
        adb shell chmod +x /tmp/askpass.sh
        adb shell SUDO_ASKPASS=/tmp/askpass.sh sudo -A touch /userdata/.adb_onlock
        adb shell SUDO_ASKPASS=/tmp/askpass.sh sudo -A mount -o remount,rw /
        adb shell SUDO_ASKPASS=/tmp/askpass.sh sudo -A add-apt-repository -y $PPA/$DISTRO/$SILO

        adb shell "test -e /usr/sbin/policy-rc.d && cp /usr/sbin/policy-rc.d /tmp/policy-rc.d"
        adb shell "echo 'exit 101' | SUDO_ASKPASS=/tmp/askpass.sh sudo -A tee /usr/sbin/policy-rc.d"
        adb shell SUDO_ASKPASS=/tmp/askpass.sh sudo -A chmod +x /usr/sbin/policy-rc.d
        adb shell "echo -e 'Package: *\nPin: release o=LP-PPA-ci-train-ppa-service-$SILO\nPin-Priority: 1100' | SUDO_ASKPASS=/tmp/askpass.sh sudo -A tee /etc/apt/preferences.d/silo-$SILO.pref"
        adb shell SUDO_ASKPASS=/tmp/askpass.sh sudo -A apt-get -o Dir::Etc::SourceList=/dev/null update

        # We need to put lxc-android-config on hold here as it's the only
        # package which must be upgrade by hand through the recovery. See
        # https://wiki.ubuntu.com/Touch/Testing/lxc-android-config for details.
        adb shell SUDO_ASKPASS=/tmp/askpass.sh sudo -A apt-mark hold lxc-android-config

        adb shell SUDO_ASKPASS=/tmp/askpass.sh sudo -A apt-get dist-upgrade --yes
        adb shell SUDO_ASKPASS=/tmp/askpass.sh sudo -A apt-get update
        adb shell SUDO_ASKPASS=/tmp/askpass.sh sudo -A apt-get autoremove --yes

        adb shell "test ! -e /tmp/policy-rc.d && SUDO_ASKPASS=/tmp/askpass.sh sudo -A rm /usr/sbin/policy-rc.d"
        adb shell "test -e /tmp/policy-rc.d && SUDO_ASKPASS=/tmp/askpass.sh sudo -A mv /tmp/policy-rc.d /usr/sbin/policy-rc.d"
        adb shell SUDO_ASKPASS=/tmp/askpass.sh sudo -A apt-mark unhold lxc-android-config
        adb shell "echo 'sleep 1 ; reboot' > /tmp/reboot.sh"
        adb shell SUDO_ASKPASS=/tmp/askpass.sh sudo -A /bin/sh /tmp/reboot.sh &
        adb shell SUDO_ASKPASS=/tmp/askpass.sh sudo -A rm /userdata/.adb_onlock && rm -f /tmp/askpass.sh
        ;;

    host-purge)
        set -x
        sudo ppa-purge $PPA/$SILO
        ;;

    device-purge)
        echo "Unfortunately purging from the device is unsupported because"
        echo "ppa-purge is not installed by default in the images."
        echo "However, the silo packages go away next time you flash the device."
        ;;

    *)
        usage "Unknown COMMAND."
        ;;
esac
