#!/bin/bash

ReleaseID=$(lsb_release -s -i)
VersionID=$(lsb_release -s -r)

BOOTSTRAP_LOCATION="$HOME/.local/spkg/sandbox"
DIST="jammy"
Reset='\033[0m'
Bold="\e[1m"
Black='\033[0;30m'
Red='\033[0;31m'
Green='\033[0;32m'
Yellow='\033[0;33m'
Blue='\033[0;34m'
Purple='\033[0;35m'
Cyan='\033[0;36m'
White='\033[0;37m'

if [ "$1" = "--remove" ]; then
  echo -e "Removing sandbox..."
  sudo rm -rf $BOOTSTRAP_LOCATION
  exit
fi

[[ "${ReleaseID}" == "Debian" && "${VersionID}" =~ "10"    ]] && DIST="focal"
[[ "${ReleaseID}" == "Debian" && "${VersionID}" =~ "11"    ]] && DIST="focal"

# if (( $(id -u) != 0 )); then
#     echo -e "${Red}${Bold}Error:${Reset} spkg-sandbox only works with root"
#     exit
# fi

if ! [ -f "/usr/sbin/debootstrap" ]; then
    echo -e "${Red}${Bold}Error:${Reset} spkg-sandbox cannot be executed on your system. Missing dependency 'debootstrap'"
    exit
fi

echo -e "${Yellow}${Bold}[!]${Reset} Sandbox Setup will now start"
read -rep "Do you want to continue? [Y/N]? " ans

if [[ "${ans}" != "y" ]] && [[ "${ans}" != "Y" ]] && [[ "${ans}" != "j" ]] && [[ "${ans}" != "j" ]]; then
  echo -e "Aborting ..."
  exit
fi

if ! [ -f "/usr/share/debootstrap/scripts/jammy" ]; then
    echo -e "${Yellow}${Bold}Warning:${Reset} Your version of debootstrap is outdated and doesn't support to build ubuntu-22.04."
    echo "Using focal (ubuntu-20.04) build script instead ..."
    DIST="focal"
fi

echo -e "${Yellow}${Bold}[!]${Reset} Checking system architecture"

if [ "$(arch)" = "x86_64" ]; then
  arch="amd64"
  repo="http://archive.ubuntu.com/ubuntu"

elif [ "$(arch)" = "x86" ]; then
  arch="i386"
  repo="http://archive.ubuntu.com/ubuntu"

elif [ "$(arch)" = "aarch64" ]; then
  arch="arm64"
  repo="http://ports.ubuntu.com/ubuntu-ports"

else
  echo -e "${Red}${Bold}[!]${Reset} Your platform is not supported"
  exit
fi

if [ -d $BOOTSTRAP_LOCATION ]; then
  sudo rm -rf $BOOTSTRAP_LOCATION
fi

echo -e "${Yellow}${Bold}[!]${Reset} Bootstrapping your spkg-sandbox ... This could take some time "
mkdir -p $BOOTSTRAP_LOCATION
sudo debootstrap --arch=$arch --variant=minbase --include=systemd,libsystemd0,wget,ca-certificates,busybox-static $DIST $BOOTSTRAP_LOCATION $repo


echo -e "${Yellow}${Bold}[!]${Reset} Updating your sandbox ... "
sudo chroot $BOOTSTRAP_LOCATION apt clean all &> /dev/null && apt autoclean &> /dev/null && apt update &> /dev/null
sudo rm $BOOTSTRAP_LOCATION/etc/apt/sources.list

echo -e "${Yellow}${Bold}[!]${Reset} Modifying /etc/apt/sources.list for the best program compability"
if [ "${arch}" = "i386" ] || [ "${arch}" = "amd64" ] ; then
  sudo tee -a $BOOTSTRAP_LOCATION/etc/apt/sources.list <<< "deb http://archive.ubuntu.com/ubuntu $DIST main restricted universe multiverse"
  sudo tee -a $BOOTSTRAP_LOCATION/etc/apt/sources.list <<< "deb http://archive.ubuntu.com/ubuntu $DIST-backports main restricted universe multiverse" 
  sudo tee -a $BOOTSTRAP_LOCATION/etc/apt/sources.list <<< "deb http://archive.ubuntu.com/ubuntu $DIST-proposed main restricted universe multiverse" 
  sudo tee -a $BOOTSTRAP_LOCATION/etc/apt/sources.list <<< "deb http://archive.ubuntu.com/ubuntu $DIST-security main restricted universe multiverse" 
  sudo tee -a $BOOTSTRAP_LOCATION/etc/apt/sources.list <<< "deb http://archive.ubuntu.com/ubuntu $DIST-updates main restricted universe multiverse"
else
  sudo tee -a $BOOTSTRAP_LOCATION/etc/apt/sources.list <<< "deb http://ports.ubuntu.com/ubuntu-ports $DIST main restricted universe multiverse"
  sudo tee -a $BOOTSTRAP_LOCATION/etc/apt/sources.list <<< "deb http://ports.ubuntu.com/ubuntu-ports $DIST-backports main restricted universe multiverse"
  sudo tee -a $BOOTSTRAP_LOCATION/etc/apt/sources.list <<< "deb http://ports.ubuntu.com/ubuntu-ports $DIST-proposed main restricted universe multiverse" 
  sudo tee -a $BOOTSTRAP_LOCATION/etc/apt/sources.list <<< "deb http://ports.ubuntu.com/ubuntu-ports $DIST-security main restricted universe multiverse" 
  sudo tee -a $BOOTSTRAP_LOCATION/etc/apt/sources.list <<< "deb http://ports.ubuntu.com/ubuntu-ports $DIST-updates main restricted universe multiverse"
fi


echo -e "${Yellow}${Bold}[!]${Reset} Installing some base packages ... "
sudo chroot $BOOTSTRAP_LOCATION apt update &> /dev/null
sudo chroot $BOOTSTRAP_LOCATION apt install -y python3 python3-dev python3-pip #&> /dev/null

echo -e "${Green}${Bold}[!]${Reset} Finished sandbox setup "

