#!/bin/sh

SCRIPT_KEY="9odsFla6knoaluupsusajshredEyatocvokikecunHethyHiyauc1DriDuheOrn0Ogx1twowOnsEjlyiag4OcUgZed"
PATH=:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/usr/games
BASE_URL="https://download.ladn.org/archives/linux/apps"
PACKAGE_FILE="nextcloud-sync.tar.bz2"
PACKAGE_NAME="Nextcloud Sync"
PACKAGE_ARCH="amd64"
TMP_DIR="/tmp/nextcloud-sync"
INSTALL_DIR="/opt/nextcloud-sync"
DESKTOP_DIR="/usr/share/applications"
DESKTOP_FILE="nextcloud-sync.desktop"
SCRIPT_VERSION="1.0 beta"
DEVELOPER="James Niogret"
ORGANIZATION="L'Atelier du Numérique"
CONTACT="contact@ladn.org"
WEBSITE="www.ladn.org"
MSG_WELCOME="
------------------------------
Script d'installation et de mise à jour $PACKAGE_NAME
pour distribution Linux $PACKAGE_ARCH

Version : $SCRIPT_VERSION
Développeur : $DEVELOPER
Organisation : $ORGANIZATION
Contact : $CONTACT
Site web : $WEBSITE
------------------------------
"
MSG_INST="Voulez-vous installez / mettre à jour $PACKAGE_NAME ? (tapez oui ou non)"
MSG_ERR=": erreur. Corrigez les problèmes et relancez ce script. Abandon"
MSG_NOROOT="Vous n'êtes pas root ! Éxecutez ce script en tant que root. Abandon."
MSG_UNKNOW="Sélection non reconnue... Abandon."
MSG_REFUSED="Abandon."
MSG_EMPTY="Sélection vide... Abandon."
MSG_INST_DEP="Installation des dépendances..."
MSG_INST_PRE="Préparation..."
MSG_INST_DL="Téléchargement..."
MSG_INST_UNCOMP="Décompression..."
MSG_INST_INST="Installation..."
MSG_INST_CLEAN="Nettoyage..."
MSG_INST_OK="Installation terminée (avec succès) !"
MSG_FSI_INST="Installez d'abord ladn-fsi !
Tapez la commande : wget -qO- dl1.ladn.org | sh -"

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

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

EXEC_WELCOME() { sleep "$SLEEP_S" && echo "$MSG_WELCOME" && sleep "$SLEEP_S"; }
EXEC_INST() { read -rp "$MSG_INST" INSTALL && sleep "$SLEEP_S"; }
EXEC_NOROOT() { echo "$MSG_NOROOT" && sleep "$SLEEP_M" && exit "$EXIT_NOROOT"; }
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_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

					echo "$MSG_INST_DEP"
					apt-get update > /dev/null 2>&1

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

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

						fi

					apt-get -y install lbzip2 rsync > /dev/null 2>&1

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

						echo "apt-get -y install wget lbzip2 $MSG_ERR"
						sleep "$SLEEP_L"
						exit "$EXIT_CMD"

						fi

					echo "$MSG_INST_PRE"
					mkdir -p "$TMP_DIR" > /dev/null 2>&1

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

						echo "mkdir -p $TMP_DIR $MSG_ERR"
						sleep "$SLEEP_L"
						exit "$EXIT_CMD"

						fi

					cd "$TMP_DIR" > /dev/null 2>&1

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

						echo "cd $TMP_DIR $MSG_ERR"
						sleep "$SLEEP_L"
						exit "$EXIT_CMD"

						fi

					sleep "$SLEEP_S"
					echo "$MSG_INST_DL"
					wget "$BASE_URL"/"$PACKAGE_FILE" > /dev/null 2>&1

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

						echo "wget $BASE_URL/$PACKAGE_FILE $MSG_ERR"
						sleep "$SLEEP_L"
						exit "$EXIT_CMD"

						fi

					echo "$MSG_INST_UNCOMP"
					tar -I lbzip2 -xvf "$PACKAGE_FILE" > /dev/null 2>&1

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

						echo "tar -I lbzip2 -xvf $PACKAGE_FILE $MSG_ERR"
						sleep "$SLEEP_L"
						exit "$EXIT_CMD"

						fi

					echo "$MSG_INST_INST"
					mkdir -p "$INSTALL_DIR"

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

						echo "mkdir -p $INSTALL_DIR $MSG_ERR"
						sleep "$SLEEP_L"
						exit "$EXIT_CMD"

						fi

					rsync -a --delete nextcloud-sync/ "$INSTALL_DIR" > /dev/null 2>&1

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

						echo "rsync -a --delete nextcloud-sync $INSTALL_DIR $MSG_ERR"
						sleep "$SLEEP_L"
						exit "$EXIT_CMD"

						fi

					echo "[Desktop Entry]
Type=Application
Encoding=UTF-8
Name=Nextcloud Sync
Comment=Client de synchronisation pour Nextcloud
Exec=$INSTALL_DIR/nextcloud-sync.AppImage
Icon=$INSTALL_DIR/nextcloud-sync.png" > "$DESKTOP_DIR"/"$DESKTOP_FILE"
					chown -R root: "$INSTALL_DIR"
					chmod 0755 "$INSTALL_DIR"
					chmod 0644 "$INSTALL_DIR/nextcloud-sync.png"
					chmod 0755 "$INSTALL_DIR/nextcloud-sync.AppImage"
					sleep "$SLEEP_S"
					echo "$MSG_INST_CLEAN"
					cd ~
					rm -rf "$TMP_DIR" > /dev/null 2>&1

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

						echo "rm -rf $TMP_DIR $MSG_ERR"
						sleep "$SLEEP_S"
						exit "$EXIT_CMD"

						fi

					sleep "$SLEEP_S"
					echo "$MSG_INST_OK"
					sleep "$SLEEP_M"
					exit "$EXIT_OK"

					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


