#!/bin/bash
#Allow for external definiton of status of cache
StatusFlag=$1
#UserSettings
ThisScriptConfigured=no
UseCache=no

#Cache enable (using apt-cacher-NG)
CacheAlpineRepo="http://192.168.2.83/alpine/"
CacheDebianCache="http://192.168.2.83:80"
CacheDebianCore="http://192.168.2.83/deb.debian.org/debian"

#Cache enable (using Official MirrorServers) 
DefaultAlpineRepo="http://dl-4.alpinelinux.org/alpine/"
DefaultDebianCore="http://deb.debian.org/debian"

###Do not Edit underneath###



#Check if using StatusFlag if not chek if scipt is configured
if [ -n "$StatusFlag" ]; then
    if [ $StatusFlag = "-e" ]; then
        UseCache=yes
    elif [ $StatusFlag = "-d" ]; then
        UseCache=no
    fi
elif
[ $ThisScriptConfigured = no ]; then
    echo  "Script is not configured"
    exit
fi

#Check if using Cache
if [ $UseCache = yes ]; then
    echo "Using cache server"
    AlpineRepo=$CacheAlpineRepo
    DebianCache='Acquire::http::Proxy "'$CacheDebianCache'";'
    DebianCore=$CacheDebianCore
elif [ $UseCache = no ]; then
    echo "Not using cache server"
    AlpineRepo=$DefaultAlpineRepo
    DebianCache='#CacheServer not set'
    DebianCore=$DefaultDebianCore
fi

#Goto ProjectRoot
cd "$( cd "$( dirname "$0" )" &> /dev/null && pwd )/.."

#AlpineBaseRepo
sed -i -e '/#InitDownloadREPO/{n;d}' CT-Build/Alpine.yaml
sed -i -e '/#InitDownloadREPO/a \  url: '$AlpineRepo'' CT-Build/Alpine.yaml

#AlpineRepo
sed -i -n '/#StartREPOS/{:a;N;/#EndREPOS/!ba;N;s/.*\n/#HereREPO\n/};p' CT-Build/Alpine.yaml
sed -i -e 's#\#HereREPO#\      \#StartREPOS\n\      '$AlpineRepo/latest-stable/main'\n\      '$AlpineRepo'latest-stable/community\n\      \#EndREPOS#g' CT-Build/Alpine.yaml

#DebianBaseRepo
sed -i -e '/#InitDownloadREPO/{n;d}' CT-Build/Debian.yaml
sed -i -e '/#InitDownloadREPO/a \  url: '$DebianCore'' CT-Build/Debian.yaml

#DebianProxyCache
sed -i -e '/#ProxyCacheSetting/{n;d}' CT-Build/Debian.yaml
sed -i -e "/#ProxyCacheSetting/a \    echo \'$DebianCache\' > /etc/apt/apt.conf.d/01prox" CT-Build/Debian.yaml
