#!/bin/sh

TEMPLATE_PATH="/usr/share/els-php-release/php-els.list.template"
PHP_REPO_PATH="/etc/apt/sources.list.d/php-els.list"
AUTH_CONF_PATH="/etc/apt/auth.conf.d/php-els.conf"

if [ -f "$TEMPLATE_PATH" ]; then
    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')

    sed -e "s/__CODENAME__/${OS_CODENAME}/g" \
        -e "s/__VER__/${OS_VER}/g" \
        -e "s/__NAME__/${OS_NAME}/g" \
        "$TEMPLATE_PATH" > "$PHP_REPO_PATH"
fi

if [ ! -f "$AUTH_CONF_PATH" ]; then
    if [ -f "$PHP_REPO_PATH" ]; then
        sed -i '/^deb/ s/^/#/' "$PHP_REPO_PATH"
    fi
fi