#!/bin/sh

CHROOT=$(mount | grep proc | grep calamares | awk '{print $3}' | sed -e "s#/proc##g")
MEDIUM_PATH="/run/live/medium"

if [ "$1" = "-u" ]; then
    umount $CHROOT/$MEDIUM_PATH
    rm $CHROOT/etc/apt/sources.list.d/parrot-live-media.list || true
    mv /etc/apt/parrot.disabled /etc/apt/sources.list.d/parrot.list || true
    cat << EOF > $CHROOT/etc/apt/sources.list
# This file is empty, feel free to
# add here your custom APT repositories

# The default Parrot repositories are NOT here. Take a look at
#    /etc/apt/sources.list.d/parrot.list


#############
#  WARNING  #
#############

# ADDING EXTERNAL REPOSITORIES MAY HARM YOUR SYSTEM,
# MAY INTRODUCE MALWARE, CAUSE INSTABILITY OR
# CAUSE PERMANENT DATA LOSS
#    DO IT WISELY

# if the default servers are unavailable or too slow
# open /etc/apt/sources.list.d/parrot.list
# and follow the instructions to choose a faster server
# or read https://www.parrotsec.org/docs/mirrors/mirrors-list/ for more info
EOF
    chroot $CHROOT apt-get update || true
    # Attempt safest way to remove cruft
    rmdir $CHROOT/run/live/medium || true
    rmdir $CHROOT/run/live || true
    exit 0
fi

# Remove the base sources, we will configure sources in a later phase
rm -f $CHROOT/etc/apt/sources.list.d/base.list || true
mv /etc/apt/sources.list.d/parrot.list /etc/apt/parrot.disabled

mkdir -p $CHROOT/$MEDIUM_PATH
mount --bind $MEDIUM_PATH $CHROOT/$MEDIUM_PATH
echo "deb [trusted=yes] file:$MEDIUM_PATH lory main" > $CHROOT/etc/apt/sources.list.d/parrot-live-media.list
chroot $CHROOT apt-get update

exit 0
