#!/bin/bash

set -e

# Install python alias
if [ "$(which python3)" ]; then
    if ! update-alternatives --list python | grep -q python3; then
        update-alternatives --install /usr/bin/python python /usr/bin/python3 50
    fi
fi


mkdir -p /etc/dconf/db/local.d/
mkdir -p /etc/skel/.config/dconf/
cp -r /usr/lib/parrot-skel/etc/dconf/db/local.d/* /etc/dconf/db/local.d/

if [ ! -f /etc/its-a-pwnbox ]; then
    rsync -a --delete \
        --exclude='/.config/lxqt' \
        --exclude='/.config/pcmanfm-qt' \
        /usr/lib/parrot-skel/etc/skel/ /etc/skel/
    rsync -a /etc/skel/ /root/
fi

if [ ! -f /usr/lib/parrot-skel/etc/dconf-htb/db/local.d/htb-config ]; then
	dconf compile /etc/dconf/db/local /etc/dconf/db/local.d/
	dconf compile /etc/skel/.config/dconf/user /etc/dconf/db/local.d/
fi
if [ -f /usr/lib/parrot-skel/etc/dconf-htb/db/local.d/htb-config ]; then
	dconf compile /etc/dconf/db/local /usr/lib/parrot-skel/etc/dconf-htb/db/local.d/
	dconf compile /etc/skel/.config/dconf/user /usr/lib/parrot-skel/etc/dconf-htb/db/local.d/
fi

if [ "$1" = "configure" ] && dpkg --compare-versions "$2" le "7.5.0"; then
	echo "##########################################################
## How to configure mirror servers for faster downloads ##
##########################################################
## The Parrot system is configured by default to use the Parrot servers.
## They collect all the requests of the users and redirect them to the nearest
## download server (mirror) available in the region of the user.
##
## The Parrot Mirrors are a network of independent servers that host copies of
## the whole parrot repository. Digital signatures prevent the individual mirrors
## from injecting fake updates, so they are safe to use.
## If an evil mirror tries to inject a fake package, Parrot OS will
## automatically refuse to download and install it.
##
## To configure a mirror, you have to replace the original URL in the
## config below with the URL of the mirror.
##
## Example:
## If you live in Italy and you want to take advantage of the GARR servers,
## Replace
##  URIs: https://deb.parrot.sh/parrot/
## With
##  URIs: https://parrot.mirror.garr.it/mirrors/parrot/
##
## IMPORTANT NOTES:
## do NOT use mirrors for the parrot-security repository. use them
## exclusively for 'parrot', 'parrot-backports' and 'parrot-updates'
##
## LIST:
## A small selection of commonly used mirrors is left commented in this config
## for your conveniente.
## Comment the original 'URIs:' line and uncomment the one corresponding
## to the mirror to use it.
##
## A full and updated list of official mirrors is available here:
##      https://www.parrotsec.org/docs/mirrors/mirrors-list/
##

Types: deb
## Parrot infrastructure
# With mirror redirection
URIs: https://deb.parrot.sh/parrot/
# Withoud mirror redirection
#URIs: https://deb.parrot.sh/direct/parrot/
## Worldwide CDN Services
# bunny
#URIs: https://bunny.deb.parrot.sh/parrot/
# cloudflare
#URIs: https://cloudflare.parrot.sh/direct/parrot/
# aliyun
#URIs: https://mirrors.aliyun.com/parrot/
## USA
# East Coast
#URIs: https://mirrors.mit.edu/parrot
# West Coast
#URIs: https://mirrors.ocf.berkeley.edu/parrot
## LATAM
# ecuador
#URIs: https://mirror.cedia.org.ec/parrot
# brazil
#URIs: https://sft.if.usp.br/parrot
## EUROPE
# italy
#URIs: https://parrot.mirror.garr.it/parrot
# germany
#URIs: https://ftp-stud.hs-esslingen.de/pub/Mirrors/archive.parrotsec.org/
# netherlands
#URIs: https://ftp.nluug.nl/os/Linux/distr/parrot
# belgium
#URIs: https://ftp.belnet.be/archive.parrotsec.org
#URIs: https://mirror.telepoint.bg/parrot
# sweden
#URIs: https://mirror.accum.se/mirror/parrotsec.org/parrot
#URIs: https://https://mirror.bahnhof.net/pub/parrot/
# spain
#URIs: https://matojo.unizar.es/parrot
# denmark
#URIs: https://mirrors.dotsrc.org/parrot
# greece
#URIs: https://ftp.cc.uoc.gr/mirrors/linux/parrot
## ASIA
# russia
#URIs: https://mirror.yandex.ru/mirrors/parrot
#URIs: https://mirror.truenetwork.ru/parrot
# iran
#URIs: https://mirror.famaserver.com/parrot/
# china
#URIs: https://mirrors.ustc.edu.cn/parrot
#URIs: https://mirrors.tuna.tsinghua.edu.cn/parrot
#URIs: https://mirrors.sjtug.sjtu.edu.cn/parrot
# india
#URIs: https://https://mirrors.nxtgen.com/parrot-mirror
## PACIFIC
# taiwan
#URIs: https://free.nchc.org.tw/parrot
# japan
#URIs: https://mirror.hashy0917.net/parrot
# thailand
#URIs: https://mirror.kku.ac.th/parrot
# indonesia
#URIs: https://kartolo.sby.datautama.net.id/parrot
Suites: echo echo-backports
Components: main contrib non-free non-free-firmware
Enabled: yes
Signed-By: /usr/share/keyrings/parrot-archive-keyring.gpg



########################
## Optimized packages ##
########################
## Parrot recompiles several programs and libraries using optimization flags.
## Such build flags, when used on software written in C, CPP, Rust and similar languages
## can result in binary files that make use of modern cpu instructions.
## Such optimized programs can run up to 40% faster, but will be incompatible
## with old CPUs that don't support those instructions.
##
## These repos target the amd64v3 and arm64v8.2 optimization levels.
## Enable only the relevant architecture. Don't enable both!
##
## DON'T ENABLE THEM UNLESS YOU ARE SURE YOUR CPU SUPPORTS THEM

# amd64v3
Types: deb
URIs: https://deb.parrot.sh/parrot/
Suites: echo
Components: amd64v3
Architectures: amd64
Enabled: no
Signed-By: /usr/share/keyrings/parrot-archive-keyring.gpg

# arm64v8.2
Types: deb
URIs: https://deb.parrot.sh/parrot/
Suites: echo
Components: arm64v8.2
Architectures: arm64
Enabled: no
Signed-By: /usr/share/keyrings/parrot-archive-keyring.gpg





######################
## Security Updates ##
######################
## The 'parrot-security' repository provides time sensitive security updates
## that have to be delivered to end users as fast as possible
##
## always keep this repo enabled!
##
## we strongly recommend to NOT use mirrors for this repo, but only
## access security updates from our official domains or CDNs.
##
## a mirror server could take hours or days before the next synchronization,
## leaving you without security updates for a timespan outside of our control

Types: deb
URIs: https://mirror.parrot.sh/direct/parrot/
Suites: echo-security
Components: main contrib non-free non-free-firmware
Enabled: yes
Signed-By: /usr/share/keyrings/parrot-archive-keyring.gpg

" > /etc/apt/sources.list.d/parrot.sources
    mv /etc/apt/sources.list /etc/apt/sources.list.old || true
    

    echo "# This file is empty, feel free to
# add here your custom APT repositories


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

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

# The default Parrot repositories
# are NOT here. If you want to
# edit them, take a look into
#    /etc/apt/sources.list.d/parrot.sources

# if the default servers are too slow
# open /etc/apt/sources.list.d/parrot.sources
# and follow the instructions to choose faster servers
# or read https://www.parrotsec.org/docs/mirrors/mirrors-list/ for more info
" > /etc/apt/sources.list

fi


# migrate home config from parrot 6.4 and 7.0 to 7.x
if [ "$1" = "configure" ] && dpkg --compare-versions "$2" le "7.1.4"; then
	echo "parrot-core: [SKEL] Syncing new configs to home folder"
	for user in $(cd /home; ls --ignore=lost+found); do
		if ! id "$user" &>/dev/null; then
		  continue
		fi
		TEMP=$(mktemp -d) || exit 100
		rsync -a /etc/skel/ "$TEMP/" || exit 101
		chown "$user:$user" -R "$TEMP/" || exit 102
		if [ -n "$user" ]; then
			rsync -ah --ignore-existing "$TEMP/" "/home/$user/" || exit 103
		fi
		rm -rf "$TEMP"
	done
	echo "parrot-core: [SKEL] Parrot configs updated"
fi
