#!/bin/sh
for file in /root/config/*; do [ -r $file ] && . $file; done

echo "Info: mirror is ${LB_MIRROR_CHROOT} or ${LB_PARENT_MIRROR_CHROOT}"

sed -i -e "s#@MIRRORDUMMY@#${LB_MIRROR_CHROOT}#"g /etc/apt/sources.list

for x in /etc/apt/sources.list.d/*.list; do
   if grep -q '### THIS FILE IS AUTOMATICALLY CONFIGURED ###' $x; then
      rm -f $x
   fi
done

if [ -f /etc/apt/sources.list.d/steam.list ]; then
    rm -f /etc/apt/sources.list.d/steam.list
fi

if [ -f /etc/apt/sources.list.d/skype-stable.list ]; then
    rm -f /etc/apt/sources.list.d/skype-stable.list
fi

sed -i -e "s|@DISTRIBUTION@|${LB_DISTRIBUTION}|g" /etc/apt/sources.list

# old names
case "${LB_DISTRIBUTION}" in
    wheezy|jessie|stretch|buster) sed -i -e "s|-security||g" /etc/apt/sources.list ;;
esac

# since bookworm we have the new component "non-free-firmware" in archive
case "${LB_DISTRIBUTION}" in
    wheezy|jessie|stretch|buster|bullseye) sed -i -e "s|non-free-firmware||g" /etc/apt/sources.list ;;
esac

# fix for archive
if [ "$LB_DISTRIBUTION" = "wheezy" ]; then
   sed -i -e "s#${LB_MIRROR_CHROOT}#http://archive.debian.org/debian#g" /etc/apt/sources.list
fi