#!/bin/sh

AUTH_CONF_PATH="/etc/apt/auth.conf.d/php-els.conf"
PHP_REPO_PATH="/etc/apt/sources.list.d/php-els.list"

migrate_from_cloudlinux_to_tuxcare() {
    if [ -f "$AUTH_CONF_PATH" ]; then
        sed -i 's|repo.cloudlinux.com/php-els|repo.alt.tuxcare.com/alt-php-els|g' "$AUTH_CONF_PATH"
    fi
}

create_php_repo() {
    OS_CODENAME=$(grep '^VERSION_CODENAME=' /etc/os-release | cut -d'=' -f2 | sed -e 's@"@@g')
    OS_VER=$(grep '^VERSION_ID=' /etc/os-release | cut -d'=' -f2 | sed -e 's@"@@g')
    OS_NAME=$(grep '^ID=' /etc/os-release | cut -d'=' -f2 | sed -e 's@"@@g')
    ARCH=$(dpkg --print-architecture)

    cat <<EOF > "$PHP_REPO_PATH"
### THIS FILE IS AUTOMATICALLY CONFIGURED ###
# You may comment out this entry, but any other modifications may be lost.
deb [arch=${ARCH}] https://repo.alt.tuxcare.com/alt-php-els/deb/${OS_NAME}/${OS_VER}/stable ${OS_CODENAME} main
EOF
}

create_php_repo
migrate_from_cloudlinux_to_tuxcare