#!/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/

dconf compile /etc/dconf/db/local /etc/dconf/db/local.d/
dconf compile /etc/skel/.config/dconf/user /etc/dconf/db/local.d/

if [ ! -f /etc/its-a-pwnbox ]; then
    rsync -a --delete /usr/lib/parrot-skel/etc/skel/ /etc/skel/
    rsync -a /etc/skel/ /root/
fi

if [ "$1" = "configure" ] && dpkg --compare-versions "$2" le "7.1.0"; then
	echo "## This file provides the default APT repositories for Parrot Security
## These repositories are used to fetch new software and system updates

###############################
## Default Parrot Repository ##
###############################
## The 'parrot' repository provides all the Parrot software considered
## stable and tested.
##
## the main suite provides the base system plus all the software released
## under a free and open source license compatible with the Debian guidelines.
##
## the contrib suite provides additional free software that depends on third
## party software not provided by the main suite. it usually means that such
## packages depend on non-free software.
##
## the non-free suite provides additional packages that don't comply with
## the Debian Free Software Guidelines. They are mostly proprietary software.
deb https://deb.parrot.sh/parrot echo main contrib non-free non-free-firmware

######################
## Security Updates ##
######################
## The 'parrot-security' repository provides time sensitive security updates
## that have to be delivered to end users as fast as possible
##
## this repo should always be enabled in your system
##
## 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 too much time to fetch an important update,
## leaving you without security updates for a timespan outside of our control
deb https://deb.parrot.sh/direct/parrot echo-security main contrib non-free non-free-firmware

###############
## Backports ##
###############
## The stable repository contains software that is considered stable the day of
## the release. such packages receive only security and stability updates that
## don't introduce new features and don't drop old ones.
##
## the 'backports' repository provides feature updates to many packages, and
## even external software that was originally excluded from the stable release.
##
## disable it if you prefer stability and reliability over bleeding edge features
deb https://deb.parrot.sh/parrot echo-backports main contrib non-free non-free-firmware

#######################
## Updates / Testing ##
#######################
## The 'parrot-updates' repository provides updates before they are made available
## to 'parrot'. this repo is mostly meant to be used by developers and beta testers 
## to extensively test updates before they are migrated to the main repository
##
## we suggest not to enable it, as it may introduce untested bugs and make the
## system unstable. updates are delivered as fast as possible (within a week)
## so you are not missing anything important with this disabled (unless you are a dev)
#deb https://deb.parrot.sh/parrot echo-updates main contrib non-free non-free-firmware


##############################
## Source Code Repositories ##
##############################
## These repositories provide the debian source artifacts of the packages
## in the corresponding repos in the form of dsc files and the corresponding
## tarballs
##
## you only need these repositories if you intend to download the source code
## of a package and re-compile it yourself with debian-compliant build tools
#deb-src https://deb.parrot.sh/parrot echo main contrib non-free non-free-firmware
#deb-src https://deb.parrot.sh/parrot echo-security main contrib non-free non-free-firmware
#deb-src https://deb.parrot.sh/parrot echo-backports main contrib non-free non-free-firmware
#deb-src https://deb.parrot.sh/parrot echo-updates main contrib non-free non-free-firmware



##########################################################
## How to configure mirror servers for faster downloads ##
##########################################################
## The Parrot system is configured by default to use the central
## parrot archive directors. The Parrot directors are special servers that
## collect all the requests of the users and redirect them to the nearest
## download server available for the user who made the request.
##
## The Parrot Mirror Network is secured by centralized digital signatures
## and the mirrors can't inject fake updates. 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 with the URL
## Provided by the mirror
##
## Example:
## If you live in Italy and you want to take advantage of the GARR mirror,
## Replace
##  deb https://deb.parrot.sh/parrot parrot main contrib non-free non-free-firmware
## With
##  deb https://parrot.mirror.garr.it/mirrors/parrot echo main contrib non-free non-free-firmware
##
## IMPORTANT NOTES:
## do NOT use mirrors for the parrot-security repository. use them
## exclusively for 'echo', 'echo-backports' and 'echo-updates'
##
## LIST:
## A full and updated list of official mirrors is available here:
##      https://www.parrotsec.org/docs/mirrors/mirrors-list/
##
##

##########################
## Some popular mirrors ##
##########################
## This is a non-comprehensive, and not-updated list of mirrors
## that we leave here as a quick reference. some of them are
## worldwide, meaning that they have several nodes around the world
## and the fastest one is picked automatically (CDN).
## others are bound to a specific region of the world.


## Worldwide - Parrot - nearest parrot server with no redirection
#deb https://deb.parrot.sh/direct/parrot/ echo main contrib non-free non-free-firmware

## Wordlwide - Bunny - Uses the Bunny CDN platform
#deb https://bunny.deb.parrot.sh/parrot/ echo main contrib non-free non-free-firmware

## Wordlwide - GCore - Uses the G-Core CDN platform
#deb https://gcore.deb.parrot.sh/parrot/ echo main contrib non-free non-free-firmware

## Wordlwide - Azure - Uses the Microsoft Azure CDN platform
#deb https://azure.deb.parrot.sh/parrot/ echo main contrib non-free non-free-firmware

## Wordlwide - Aliyun - Uses the AlibabaCloud mirrors behind their worldwide CDN
#deb deb https://mirrors.aliyun.com/parrot echo main contrib non-free non-free-firmware

## US Massachussetts - SIPB MIT University
#deb http://mirrors.mit.edu/parrot/ echo main contrib non-free non-free-firmware

## US California - OCF Berkekey University
#deb https://mirrors.ocf.berkeley.edu/parrot/ echo main contrib non-free non-free-firmware

## US Virginia - Leaseweb
#deb https://mirror.wdc1.us.leaseweb.net/parrot echo main contrib non-free non-free-firmware

## Germany - Leaseweb
#deb https://mirror.fra10.de.leaseweb.net/parrot echo main contrib non-free non-free-firmware

## Germany - Halifax RWTH-Aachen University
#deb https://ftp.halifax.rwth-aachen.de/parrotsec/ echo main contrib non-free non-free-firmware

## Italy - GARR Consortium
#deb https://parrot.mirror.garr.it/mirrors/parrot/ echo main contrib non-free non-free-firmware

## Netherlands - Nluug
#deb https://ftp.nluug.nl/os/Linux/distr/parrot/ echo main contrib non-free non-free-firmware

## Russia - Yandex
#deb https://mirror.yandex.ru/mirrors/parrot/ echo main contrib non-free non-free-firmware

## Brazil - Sao Paulo University
#deb http://sft.if.usp.br/parrot/ echo main contrib non-free non-free-firmware

## Taiwan - NCHC
#deb https://free.nchc.org.tw/parrot/ echo main contrib non-free non-free-firmware

## Singapore - 0x
#deb https://mirror.0x.sg/parrot/ echo main contrib non-free non-free-firmware

## China - USTC University
#deb https://mirrors.ustc.edu.cn/parrot/ echo main contrib non-free non-free-firmware

## India - NxtGen 
#deb https://mirrors.nxtgen.com/parrot-mirror echo main contrib non-free non-free-firmware


## We strongly invite you to visit https://www.parrotsec.org/docs/mirrors/mirrors-list/
## to find the mirror that better fits your needs" > /etc/apt/sources.list.d/parrot.list
    cat /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.list

# if the default servers are too slow
# open /etc/apt/sources.list.d/parrot.list
# 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); do
		TEMP=$(mktemp -d) || exit 100
		rsync -a /etc/skel/ $TEMP/ || exit 101
		chown $user:$user -R $TEMP/ || exit 102
		if [ -n "$user" ]; then
			rsync -Pahv "$TEMP/" "/home/$user/" || exit 103
		fi
			rm -r $TEMP
	done
	echo "parrot-core: [SKEL] Parrot configs updated"
fi
