#!/bin/bash
if [ ! -f /etc/apt/sources.list.d/chimera.sources ] && [ ! -f /etc/apt/sources.list.d/electra.list ]; then
        sourcesDir="/etc/apt/sileo.list.d"
        mkdir -p $sourcesDir
        if [ -s /etc/apt/sources.list.d/sileo.sources ]; then
                mv /etc/apt/sources.list.d/sileo.sources $sourcesDir
                rm -f /etc/apt/sources.list.d/sileo-base.sources
        fi

        if ! grep -Fxq "Dir::Etc" /Applications/Sileo.app/sileo-apt.conf ;
then
        sed -i.bak '1s;^;Dir::Etc\
{\
        sourceparts "sileo.list.d/"\
}\
\
;' /Applications/Sileo.app/sileo-apt.conf
fi
rm -f /Applications/Sileo.app/sileo-apt.conf.bak 2> /dev/null || true

else
        sourcesDir="/etc/apt/sources.list.d"
fi

rm /etc/apt/sources.list.d/sileo.list 2> /dev/null || true
touch $sourcesDir/sileo.sources

cr="\n"

if ! [ -s $sourcesDir/sileo.sources ]; then
	echo -e "" > $sourcesDir/sileo.sources
fi

if ! grep -Fxq "URIs: https://repounclutter.coolstar.org/" $sourcesDir/sileo.sources ;
then
	echo "Installed BigBoss+ Repo"
	sed -i.bak '1s;^;Types: deb\
URIs: https://repounclutter.coolstar.org/\
Suites: ./\
Components:\
\
;' $sourcesDir/sileo.sources
fi

if ! grep -Fxq "URIs: https://repo.chariz.com/" $sourcesDir/sileo.sources ;
then
        echo "Installed Chariz Repo"
        sed -i.bak '1s;^;Types: deb\
URIs: https://repo.chariz.com/\
Suites: ./\
Components:\
\
;' $sourcesDir/sileo.sources
fi

if ! grep -Fxq "URIs: https://repo.dynastic.co/" $sourcesDir/sileo.sources ;
then
	echo "Installed Dynastic Repo"
        sed -i.bak '1s;^;Types: deb\
URIs: https://repo.dynastic.co/\
Suites: ./\
Components:\
\
;' $sourcesDir/sileo.sources
fi

if ! grep -Fxq "URIs: https://repo.packix.com/" $sourcesDir/sileo.sources ;
then
        echo "Installed Packix Repo"
        sed -i.bak '1s;^;Types: deb\
URIs: https://repo.packix.com/\
Suites: ./\
Components:\
\
;' $sourcesDir/sileo.sources
fi
 
if [ ! -f /etc/apt/sources.list.d/chimera.sources ] && [ ! -f /etc/apt/sources.list.d/electra.list ]; then
        if ! [ -s $sourcesDir/sileo-base.sources ]; then
        echo -e "" > $sourcesDir/sileo-base.sources
        fi
        
        if ! grep -Fxq "URIs: https://apt.bingner.com/" $sourcesDir/sileo-base.sources ;
        then
                echo "Installed Elucubratus"
                sed -i.bak '1s;^;Types: deb\
URIs: https://apt.bingner.com/\
Suites: ./\
Components:\
\
;' $sourcesDir/sileo-base.sources
	fi

        if ! grep -Fxq "URIs: https://repo.getsileo.app/" $sourcesDir/sileo-base.sources ;
        then
                echo "Installed GetSileo Repo"
                sed -i.bak '1s;^;Types: deb\
URIs: https://repo.getsileo.app/\
Suites: ./\
Components:\
\
;' $sourcesDir/sileo-base.sources
        fi
fi

rm -f $sourcesDir/sileo.sources.bak 2> /dev/null || true
rm -f $sourcesDir/sileo-base.sources.bak 2> /dev/null || true

function finish() {
        f="${1}"

        # No control fd: bail out
        [[ -z "${f}" || -z "${SILEO}" ]] && return
        sileo=(${SILEO})

        # Sileo control fd version < 1: bail out
        [[ ${sileo[1]} -ge 1 ]] || return

        echo "finish:${f}" >&${sileo[0]}
}

finish uicache

if [[ -z ${SILEO} ]]; then echo "Not running in Sileo. Trigger UICache"; fi
if [[ -z ${SILEO} ]]; then uicache -p /Applications/Sileo.app; fi

exit 0
