#!/bin/bash
sourcesDir="/etc/apt/sources.list.d"

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 ModMyTTD+ 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 ! grep -Fxq "URIs: http://apt.thebigboss.org/repofiles/cydia/" $sourcesDir/sileo.sources ;
then
        echo "Installed BigBoss Repo"
        sed -i.bak '1s;^;Types: deb\
URIs: http://apt.thebigboss.org/repofiles/cydia/\
Suites: stable\
Components: main\
\
;' $sourcesDir/sileo.sources
fi
 
echo "Package: *" > /etc/apt/preferences.d/odyssey
echo "Pin: release n=odyssey-ios" >> /etc/apt/preferences.d/odyssey
echo "Pin-Priority: 1001" >> /etc/apt/preferences.d/odyssey

echo "Package: *" > /etc/apt/preferences.d/chimera
echo "Pin: release o=Chimera Repo" >> /etc/apt/preferences.d/chimera
echo "Pin-Priority: 1000" >> /etc/apt/preferences.d/chimera

# Kill bingner repo with fire
rm -f /etc/apt/preferences.d/checkra1n 2> /dev/null || true
ln -s /dev/null /etc/apt/preferences.d/checkra1n

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
