#!/bin/bash

NETWORK=false

# базовые функции 
# shellcheck disable=SC1091
. "$(dirname "$0")/common"

OPTS=$(getopt -o h,d:,r: --long help,net,network,dir:,repos: -- "$@") || {
    print_error "Ошибка обработки опций."
    exit 1
}

# Применение параметров
eval set -- "$OPTS"

while true; do
    case "$1" in
        -h|--help)
            printf "\n"
            printf "Использование: %s [опции]\n\n" "$0"
            printf "Опции:\n"
            printf "    -h | --help         Вывод этой справки\n"
            printf "    -d | --dir          Путь к tmp\n"
            printf "    -r | --repos        Установить используемые репозитории\n"
            printf "                        (По умолчанию: sisyphus,ximper)\n"
            printf "                        (репозитории используются тольок во время сборки)\n"
            printf "    --net | --network   Использовать сетевые репозитории\n\n"
            printf "    Репозитории:\n"
            printf "        - sisyphus\n"
            printf "        - ximper\n"
            printf "        - hasher\n"
            exit 0
            ;;
        -d|--dir) 
            buildertmp="$2"
            shift
            ;;
        -r|--repos) 
            REPOS="${2//,/ }"
            shift
            ;;
        --)
            shift
            break
            ;;
        *)
            shift
            ;;
    esac
done

APTCONF="$buildertmp/apt.conf.ximper"
SOURCES="$buildertmp/sources.list.ximper"

gen_sources() {
    for repo in $REPOS; do
        case "$repo" in
            *sisyphus*)
                base_url="http://ftp.altlinux.org/pub/distributions/ALTLinux Sisyphus"
                local_path="file:/var/ftp/ pub/ALTLinux/Sisyphus"
                if [ "$NETWORK" == "true" ]; then
                    repo_path="$base_url"
                else
                    repo_path="$local_path"
                fi
                cat <<EOF >> "$SOURCES"
rpm [alt] $repo_path/x86_64 classic
rpm [alt] $repo_path/x86_64-i586 classic
rpm [alt] $repo_path/noarch classic
EOF
                ;;
            *ximper*)
                base_url="https://download.etersoft.ru/pub/download/ximper XimperLinuxRepository"
                local_path="file:/var/ftp/pub/download/ximper XimperLinuxRepository"
                if [ "$NETWORK" == "true" ]; then
                    repo_path="$base_url"
                else
                    repo_path="$local_path"
                fi
                cat <<EOF >> "$SOURCES"
rpm $repo_path/x86_64 addon
rpm $repo_path/noarch addon
EOF
                ;;
            *hasher*)
                cat <<EOF >> "$SOURCES"
rpm-dir file:/tmp/.private/$USER/hasher-sisyphus-64/ repo/x86_64 hasher
EOF
                ;;
        esac
    done
}

gen_config() {
    cat <<EOF >> "$APTCONF"
/*
 * This is the main configuration file for the APT suite of tools,
 * see apt.conf(5) for details.
 */

Dir::Etc::main "/dev/null";
Dir::Etc::parts "/var/empty";

Dir::Etc::SourceParts "/var/empty";

Dir::Etc::SourceList "$SOURCES";
EOF
}

gen_sources
gen_config

echo "$APTCONF"