#!/usr/bin/env bash

. /usr/share/dosis2/bin/shell-support-functions

enforce_root

TRUE=1
FALSE=0

# Default repo if no option is chosen is main
MAIN=$TRUE
TESTING=$FALSE
LATEST=$FALSE
WHICH_REPO="main"
UBUNTU_DOSIS_REPO="/etc/apt/sources.list.d/dosis.list"

echo_help() {
echo "change-dosis-repo [-h] [-s] [-l] -[-t]
-h | --help     show help
-s | --stable   set dosis-repo to stable (default) 
-l | --latest   set dosis-repo to latest
-t | --testing  set dosis-repo to testing
"
}

if [ "$1" != "" ]; then
    case $1 in 
        -s | --stable )       shift
                            WHICH_REPO="main"
                            MAIN=$TRUE
                            TESTING=$FALSE
                            LATEST=$FALSE
                            ;;
        -l | --latest )     shift
                            WHICH_REPO="latest"
                            MAIN=$FALSE
                            TESTING=$FALSE
                            LATEST=$TRUE
                            ;;
        -t | --testing )    shift
                            WHICH_REPO="testing"
                            MAIN=$FALSE
                            TESTING=$TRUE
                            LATEST=$FALSE
                            ;;
        -h | --help )       echo_help
                            exit
                            ;;
        * )                 echo_help
                            exit 1
    esac
fi

set_ubuntu() {
    if [ -f "${UBUNTU_DOSIS_REPO}" ]; then
        if grep -q "main" "${UBUNTU_DOSIS_REPO}"; then
            sed -i "s/main/${WHICH_REPO}/" ${UBUNTU_DOSIS_REPO}
        elif grep -q "testing" "${UBUNTU_DOSIS_REPO}"; then
            sed -i "s/testing/${WHICH_REPO}/" ${UBUNTU_DOSIS_REPO}
        elif grep -q "latest" "${UBUNTU_DOSIS_REPO}"; then
            sed -i "s/latest/${WHICH_REPO}/" ${UBUNTU_DOSIS_REPO}
        else
            echo "no default repo found, check ${UBUNTU_DOSIS_REPO} for errors then run setup-repo if needed"
        fi
    else
        echo "${UBUNTU_DOSIS_REPO} not found, run setup-repo if needed"
        exit 1
    fi
}

set_ubuntu
