#!/bin/sh

SCRIPT_KEY="9odsFla6knoaluupsusajshredEyatocvokikecunHethyHiyauc1DriDuheOrn0Ogx1twowOnsEjlyiag4OcUgZed"
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/usr/games
BASE_URL="https://packages.ladn.org"
SOURCES_LIST_DIR="/etc/apt"
SOURCES_LIST_FILE="sources.list"

DEVELOPER="James Niogret"
ORGANIZATION="L'Atelier du Numérique"
CONTACT="contact@ladn.org"
WEBSITE="www.ladn.org"
SCRIPT_VERSION="1.0"
DISTS_SUPPORT="
 - Debian (all, amd64, arm64, armel, armhf, i386)
 - Raspbian (armhf)
 - Ubuntu (amd64, i386)"

MSG_WELCOME="
------------------------------
Script d'installation du dépôt de paquets ladn.org

Distributions supportées : $DISTS_SUPPORT

Version : $SCRIPT_VERSION
Developpeur : $DEVELOPER
Organisation : $ORGANIZATION
Contact : $CONTACT
Site web : $WEBSITE
------------------------------
"

MSG_ERR=": erreur ! Corrigez le problème et relancez ce script. Abandon."
MSG_NUMB_SELECT="Saisissez votre sélection et appuyez sur Entrée ---> "
MSG_BOOL_SELECT="Voulez-vous continuer ? (tapez oui ou non) ---> "
MSG_NOROOT="Vous n'êtes pas root ! Exécutez ce script en tant que root. Abandon."
MSG_UNKNOW="Sélection non reconnue... Abandon."
MSG_QUIT="Abandon."
MSG_REFUSED="Abandon."
MSG_EMPTY="Sélection vide... Abandon."
MSG_INST="
ATTENTION !
--------------------
Ce dépôt ne fournit pas le code source des logiciels.
Utilisez ce dépôt seulement si vous n'avez pas besoin de code source.

$MSG_BOOL_SELECT"
MSG_INST_DIST="
Distribution ?

 1 - Debian
 2 - Raspbian
 3 - Ubuntu

 Q - QUITTER

$MSG_NUMB_SELECT"
MSG_FSI_INST="Installez d'abord ladn-fsi !
Tapez la commande : wget -qO- dl1.ladn.org | sh -"

SLEEP_S=".1"
SLEEP_M=".5"
SLEEP_L="1"

EXIT_OK="0"
EXIT_USR="0"
EXIT_NOROOT="2"
EXIT_CMD="3"
EXIT_UNK="0"
EXIT_EMPTY="0"
EXIT_NOFSI="127"

EXEC_WELCOME() { echo "$MSG_WELCOME" && sleep "$SLEEP_S"; }
EXEC_INST() { read -rp "$MSG_INST" INSTALL && sleep "$SLEEP_S"; }
EXEC_INST_DIST() { read -rp "$MSG_INST_DIST" DISTRIB && sleep "$SLEEP_S"; }
EXEC_INST_VERSION() { read -rp "$MSG_NUMB_SELECT" VERSION && sleep "$SLEEP_S"; }
EXEC_INST_BRANCHES() { read -rp "$MSG_NUMB_SELECT" BRANCHES && sleep "$SLEEP_S"; }
EXEC_QUIT() { echo "$MSG_QUIT" && sleep "$SLEEP_M" && exit "$EXIT_USR"; }
EXEC_REFUSED() { echo "$MSG_REFUSED" && sleep "$SLEEP_M" && exit "$EXIT_USR"; }
EXEC_UNKNOW() { echo "$MSG_UNKNOW" && sleep "$SLEEP_M" && exit "$EXIT_UNK"; }
EXEC_EMPTY() { echo "$MSG_EMPTY" && sleep "$SLEEP_M" && exit "$EXIT_EMPTY"; }
EXEC_NOROOT() { echo "$MSG_NOROOT" && sleep "$SLEEP_M" && exit "$EXIT_NOROOT"; }
EXEC_FSI_INSTALL() { echo "$MSG_FSI_INST" && sleep "$SLEEP_L" && exit "$EXIT_NOFSI"; }

	if [ -n "$1" ]; then

		if [ "$1" = "$SCRIPT_KEY" ]; then EXEC_WELCOME

			if [ "$USER" = "root" ] || [ "$HOME" = "/root" ]; then EXEC_INST

				if [ -n "$INSTALL" ]; then

					if [ "$INSTALL" = "oui" ] || [ "$INSTALL" = "OUI" ] || [ "$INSTALL" = "o" ] || [ "$INSTALL" = "O" ]; then EXEC_INST_DIST

						if [ -n "$DISTRIB" ]; then

							if [ "$DISTRIB" = "1" ] || [ "$DISTRIB" = "debian" ] || [ "$DISTRIB" = "DEBIAN" ]; then DISTRIB="debian" && echo "
Version ?

1 - stretch
2 - oldoldstable
3 - buster
4 - oldstable
5 - bullseye
6 - stable
7 - bookworm
8 - testing

Q - QUITTER
"
							EXEC_INST_VERSION

								if [ -n "$VERSION" ]; then

									if [ "$VERSION" = "1" ] || [ "$VERSION" = "stretch" ] || [ "$VERSION" = "STRETCH" ]; then VERSION="stretch"

									elif [ "$VERSION" = "2" ] || [ "$VERSION" = "oldoldstable" ] || [ "$VERSION" = "OLDOLDSTABLE" ]; then VERSION="oldoldstable"

									elif [ "$VERSION" = "3" ] || [ "$VERSION" = "buster" ] || [ "$VERSION" = "BUSTER" ]; then VERSION="buster"

									elif [ "$VERSION" = "4" ] || [ "$VERSION" = "oldstable" ] || [ "$VERSION" = "OLDSTABLE" ]; then VERSION="oldstable"

									elif [ "$VERSION" = "5" ] || [ "$VERSION" = "bullseye" ] || [ "$VERSION" = "BULLSEYE" ]; then VERSION="bullseye"

									elif [ "$VERSION" = "6" ] || [ "$VERSION" = "stable" ] || [ "$VERSION" = "STABLE" ]; then VERSION="stable"

									elif [ "$VERSION" = "7" ] || [ "$VERSION" = "bookworm" ] || [ "$VERSION" = "BOOKWORM" ]; then VERSION="bookworm"
									
									elif [ "$VERSION" = "8" ] || [ "$VERSION" = "testing" ] || [ "$VERSION" = "TESTING" ]; then VERSION="testing"

									elif [ "$VERSION" = "Q" ] || [ "$VERSION" = "q" ] || [ "$VERSION" = "QUITTER" ] || [ "$VERSION" = "quitter" ]; then EXEC_QUIT

									else EXEC_UNKNOW

									fi

								else EXEC_EMPTY

								fi

							echo "
Branches ?

1 - main
2 - main contrib
3 - main contrib non-free

Q - QUITTER
"
							EXEC_INST_BRANCHES

								if [ -n "$BRANCHES" ]; then

									if [ "$BRANCHES" = "1" ] || [ "$BRANCHES" = "main" ] || [ "$BRANCHES" = "MAIN" ]; then BRANCHES="main"

									elif [ "$BRANCHES" = "2" ] || [ "$BRANCHES" = "main contrib" ] || [ "$BRANCHES" = "MAIN CONTRIB" ]; then BRANCHES="main contrib"

									elif [ "$BRANCHES" = "3" ] || [ "$BRANCHES" = "main contrib non-free" ] || [ "$BRANCHES" = "MAIN CONTRIB NON-FREE" ]; then BRANCHES="main contrib non-free"

									elif [ "$BRANCHES" = "Q" ] || [ "$BRANCHES" = "q" ] || [ "$BRANCHES" = "QUITTER" ] || [ "$BRANCHES" = "quitter" ]; then EXEC_QUIT

									else EXEC_UNKNOW

									fi

								else EXEC_EMPTY

								fi

							SOURCES_LIST="deb $BASE_URL/$DISTRIB $VERSION $BRANCHES
deb $BASE_URL/$DISTRIB $VERSION-updates $BRANCHES
deb $BASE_URL/$DISTRIB $VERSION-backports $BRANCHES
deb $BASE_URL/$DISTRIB-security $VERSION-security $BRANCHES"

							elif [ "$DISTRIB" = "2" ] || [ "$DISTRIB" = "raspbian" ] || [ "$DISTRIB" = "RASPBIAN" ]; then DISTRIB="raspbian" && echo "
Version ?

1 - stretch
2 - oldoldstable
3 - buster
4 - oldstable
5 - bullseye
6 - stable
7 - bookworm
8 - testing

Q - QUITTER
"
							EXEC_INST_VERSION

								if [ -n "$VERSION" ]; then

									if [ "$VERSION" = "1" ] || [ "$VERSION" = "stretch" ] || [ "$VERSION" = "STRETCH" ]; then VERSION="stretch"

									elif [ "$VERSION" = "2" ] || [ "$VERSION" = "oldoldstable" ] || [ "$VERSION" ="OLDOLDSTABLE" ]; then VERSION="oldoldstable"

									elif [ "$VERSION" = "3" ] || [ "$VERSION" = "buster" ] || [ "$VERSION" = "BUSTER" ]; then VERSION="buster"

									elif [ "$VERSION" = "4" ] || [ "$VERSION" = "oldstable" ] || [ "$VERSION" = "OLDSTABLE" ]; then VERSION="oldstable"

									elif [ "$VERSION" = "5" ] || [ "$VERSION" = "bullseye" ] || [ "$VERSION" = "BULLSEYE" ]; then VERSION="bullseye"

									elif [ "$VERSION" = "6" ] || [ "$VERSION" = "stable" ] || [ "$VERSION" = "STABLE" ]; then VERSION="stable"
									
									elif [ "$VERSION" = "7" ] || [ "$VERSION" = "bookworm" ] || [ "$VERSION" = "BOOKWORM" ]; then VERSION="bookworm"
									
									elif [ "$VERSION" = "8" ] || [ "$VERSION" = "testing" ] || [ "$VERSION" = "TESTING" ]; then VERSION="testing"

									elif [ "$VERSION" = "Q" ] || [ "$VERSION" = "q" ] || [ "$VERSION" = "QUITTER" ] || [ "$VERSION" = "quitter" ]; then EXEC_QUIT

									else EXEC_UNKNOW

									fi

								else EXEC_EMPTY

								fi

							echo "
Branches ?

1 - main
2 - main contrib
3 - main contrib non-free

Q - QUITTER
"
							EXEC_INST_BRANCHES

								if [ -n "$BRANCHES" ]; then

									if [ "$BRANCHES" = "1" ] || [ "$BRANCHES" = "main" ] || [ "$BRANCHES" = "MAIN" ]; then BRANCHES="main"

									elif [ "$BRANCHES" = "2" ] || [ "$BRANCHES" = "main contrib" ] || [ "$BRANCHES" = "MAIN CONTRIB" ]; then BRANCHES="main contrib"

									elif [ "$BRANCHES" = "3" ] || [ "$BRANCHES" = "main contrib non-free" ] || [ "$BRANCHES" = "MAIN CONTRIB NON-FREE" ]; then BRANCHES="main contrib non-free"

									elif [ "$BRANCHES" = "Q" ] || [ "$BRANCHES" = "q" ] || [ "$BRANCHES" = "QUITTER" ] || [ "$BRANCHES" = "quitter" ]; then EXEC_QUIT

									else EXEC_UNKNOW

									fi

								else EXEC_EMPTY

								fi

							SOURCES_LIST="deb $BASE_URL/$DISTRIB $VERSION $BRANCHES
deb $BASE_URL/$DISTRIB $VERSION-staging $BRANCHES"

							elif [ "$DISTRIB" = "3" ]; then DISTRIB="ubuntu" && echo "
Version ?

1 - xenial
2 - bionic
3 - focal

Q - QUITTER
"
							EXEC_INST_VERSION

								if [ -n "$VERSION" ]; then

									if [ "$VERSION" = "1" ] || [ "$VERSION" = "xenial" ] || [ "$VERSION" = "XENAIL" ]; then VERSION="xenial"

									elif [ "$VERSION" = "2" ] || [ "$VERSION" = "bionic" ] || [ "$VERSION" = "BIONIC" ]; then VERSION="bionic"

									elif [ "$VERSION" = "3" ] || [ "$VERSION" = "focal" ] || [ "$VERSION" = "FOCAL" ]; then VERSION="focal"

									elif [ "$VERSION" = "Q" ] || [ "$VERSION" = "q" ] || [ "$VERSION" = "QUITTER" ] || [ "$VERSION" = "quitter" ]; then EXEC_QUIT

									else EXEC_UNKNOW

									fi

								else EXEC_EMPTY

								fi

							echo "
Branches ?

1 - main
2 - main restricted
3 - main restricted universe
4 - main restricted universe multiverse

Q - QUITTER
"
							EXEC_INST_BRANCHES

								if [ -n "$BRANCHES" ]; then

									if [ "$BRANCHES" = "1" ] || [ "$BRANCHES" = "main" ] || [ "$BRANCHES" = "MAIN" ]; then BRANCHES="main"

									elif [ "$BRANCHES" = "2" ] || [ "$BRANCHES" = "main restricted" ] || [ "$BRACNHES" = ; then BRANCHES="main restricted"

									elif [ "$BRANCHES" = "3" ] || [ "$BRANCHES" = "main restricted universe" ] || [ "$BRANCHES" = "MAIN RESTRICTED UNIVERSE" ]; then BRANCHES="main restricted universe"

									elif [ "$BRANCHES" = "4" ] || [ "$BRANCHES" = "main restricted universe multiverse" ] || [ "$BRANCHES" = "MAIN RESTRICTED UNIVERSE MULTIVERSE" ]; then BRANCHES="main restricted universe multiverse"

									elif [ "$BRANCHES" = "Q" ] || [ "$BRANCHES" = "q" ] || [ "$BRANCHES" = "QUITTER" ] || [ "$BRANCHES" = "quitter" ]; then EXEC_QUIT

									else EXEC_UNKNOW

									fi

								else EXEC_EMPTY

								fi

							SOURCES_LIST="deb $BASE_URL/$DISTRIB $VERSION $BRANCHES
deb $BASE_URL/$DISTRIB $VERSION-updates $BRANCHES
deb $BASE_URL/$DISTRIB $VERSION-backports $BRANCHES
deb $BASE_URL/$DISTRIB $VERSION-proposed $BRANCHES
deb $BASE_URL/$DISTRIB-security $VERSION-security $BRANCHES"

							elif [ "$DISTRIB" = "Q" ] || [ "$DISTRIB" = "q" ] || [ "$DISTRIB" = "QUITTER" ] || [ "$DISTRIB" = "quitter" ]; then EXEC_QUIT

							else EXEC_UNKNOW

							fi

						else EXEC_EMPTY

						fi

					echo "
Voici votre sélection :

Distribution : $DISTRIB
Version : $VERSION
Branches : $BRANCHES

Le fichier sources.list va être mise à jour.
"
					read -rp "$MSG_BOOL_SELECT" OK

						if [ -n "$OK" ]; then

							if [ "$OK" = "oui" ] || [ "$OK" = "OUI" ] || [ "$OK" = "o" ] || [ "$OK" = "O" ]; then

							echo "Installation de la prise en charge https pour APT..."
							apt-get update > /dev/null 2>&1

								if [ "$?" != "0" ]; then

								echo "apt-get update $ERR"
								sleep "$SLEEP_L"
								exit "$EXIT_CMD"

								fi

							apt-get install apt-transport-https > /dev/null 2>&1

								if [ "$?" != "0" ]; then

								echo "apt-get install apt-transport-https $ERR"
								sleep "$SLEEP_L"
								exit "$EXIT_CMD"

								fi

							echo "Sauvegarde du fichier sources.list actuel..."
							cd /etc/apt > /dev/null 2>&1

								if [ "$?" != "0" ]; then

								echo "cd /etc/apt $ERR"
								sleep "$SLEEP_L"
								exit "$EXIT_CMD"

								fi

							mv -f "$SOURCES_LIST_FILE" "$SOURCES_LIST_FILE.backup" > /dev/null 2>&1

								if [ "$?" != "0" ]; then

								echo "mv -f $SOURCES_LIST_FILE $SOURCES_LIST_FILE.backup $ERR"
								sleep "$SLEEP_L"
								exit "$EXIT_CMD"

								fi

							sleep "$SLEEP_M"
							echo "Création du nouveau fichier sources.list"
							echo "$SOURCES_LIST" >> "$SOURCES_LIST_FILE"
							echo "Mise à jour de la liste de paquets..."
							apt-get update > /dev/null 2>&1

								if [ "$?" != "0" ]; then

								echo "Suite au changement de dépôt, une erreur à été rencontré lors de la mise à jour de la liste de paquets...
Restauration du fichier sources.list précédent"
								mv -f "$SOURCES_LIST_FILE.backup" "$SOURCES_LIST_FILE"

									if [ "$?" != "0" ]; then

									echo "mv -f sources.list sources.list.backup $ERR"
									sleep "$SLEEP_L"
									exit "$EXIT_CMD"

									fi

								apt-get update > /dev/null 2>&1

									if [ "$?" = "0" ]; then

									echo "La restauration à la version précédente du fichier sources.list a résolu le problème.
Le nouveau dépôt n'a cependant pas été installé, essayez de l'installer manuellement.
Aucunes modifications !"
									sleep "$SLEEP_L"
									exit "$EXIT_OK"

									elif [ "$?" != "0" ]; then

									echo "La restauration à la version précédente du fichier sources.list n'a pas résolu le problème.
Vérifiez les logs et tentez de corriger le problème manuellement.
apt-get update $ERR"
									sleep "$SLEEP_L"
									exit 6

									fi

								fi

							echo "Installation terminée avec succès !"
							sleep "$SLEEP_M"
							exit "$EXIT_OK"

							elif [ "$OK" = "non" ] || [ "$OK" = "NON" ] || [ "$OK" = "n" ] || [ "$OK" = "N" ]; then EXEC_REFUSED

							else EXEC_UNKNOW

							fi

						else EXEC_EMPTY

						fi

					elif [ "$INSTALL" = "non" ] || [ "$INSTALL" = "NON" ] || [ "$INSTALL" = "n" ] || [ "$INSTALL" = "N" ]; then EXEC_REFUSED

					else EXEC_UNKNOW

					fi

				else EXEC_EMPTY

				fi

			else EXEC_NOROOT

			fi

		else EXEC_FSI_INSTALL

		fi

	else EXEC_FSI_INSTALL

	fi
