#!/usr/bin/env bash

SOURCES_FILE=/etc/apt/sources.list.d/yunohost.sources
LEVEL="testing"
COMPONENTS="stable testing"

set_sources_components() {
    cat << EOF
----
Patching $SOURCES_FILE to use $LEVEL repository...
----
EOF
    sed -i 's|^\(Components: \).*$|\1'"$COMPONENTS" "$SOURCES_FILE"
}

set_lists_components() {
    cat << EOF
----
Patching sources.list to enable testing repository...
----
EOF

    readarray -d '' apt_files < <(find /etc/apt/sources.list /etc/apt/sources.list.d -type f -print0)
    for FILE in "${apt_files[@]}"; do
        # (Append 'testing' at the end of lines starting with the yunohost repo..)
        sed -i 's@^deb.* http://forge.yunohost.org.*@& testing@' "$FILE"
    done
}

if [[ -f "$SOURCES_FILE" ]]; then
    set_sources_components
else
    set_lists_components
fi

cat << EOF
----
Running 'apt-get update'
----
EOF
apt-get update

cat << EOF
----
Running 'apt-get full-upgrade'
----
EOF
apt-get full-upgrade -y
