#!/bin/bash
#Script assistant à l'installation en chroot d'archlinux

## Localization
## Feel free to add translations for your own language
## and send them to me : calimeroteknik at free dot fr

case "$LANG" in
 fr*)
  _archtitle="Une distribution linux simple et légère."
  _chroottitle="Installation d'ArchLinux par chroot"
  _destdir="Dossier de destination"
  _destinfo1="Tapez le chemin du dossier de destination -- par défaut : /mnt"
  _destinfo2="Vous devez déjà y avoir monté la/les partitions si besoin."
  _destinfo3="Cette installation, cache compris, nécessite ~600Mo libres."
  _instdir="Dossier d'installation"
  _archi="Architecture"
  _archi1="Choisissez l'architecture du système à installer."
  _archi2="Attention ! On ne peut installer que du 32bit depuis un système 32bit !"
  _archi3="Tapez 1 pour i686 (32bit) -- par défaut"
  _archi4="Tapez 2 pour x86_64 (64bit)"
  _archiend="Architecture choisie"
  _dlmirror="Miroir de téléchargement"
  _mirror1="Indiquez le serveur d'où télécharger les paquets."
  _mirror2="Tapez son URL ou appuyez sur entrée pour utiliser mir.archlinux.fr."
  defaultmirror="http://mir.archlinux.fr"
  _mirrorend="Serveur choisi"
  _preconf="Préconfiguration terminée !"
  _preconf1="Appuyez sur entrée pour lancer l'installation, ou Ctrl+C pour annuler."
  _chrootdl="Téléchargement du chroot minimal..."
  _tgzfound="Archive trouvée dans"
  _dlerr="Erreur lors du téléchargement de"
  _dwnlded="téléchargé."
  _untar="Décompression..."
  _untarok="fait."
  _installbase="Installation du système de base..."
  _basesysdone="Système de base installé."
  _mounterr="Ne peut pas monter"
  _mountok="monté."
  _chrooting="Chroot dans le nouveau système !"
  _rootpass="Entrez le mot de passe root pour le nouveau système"
  _plzconfig="Configurez votre Arch - rc.conf, fstab, hosts, locale.gen... et installez grub. Tapez exit quand terminé."
  _umounted="démonté."
  _chrooterr="Erreur lors du chroot"
  _installok="Installation terminée !"
 ;;
 it*)
  _archtitle="Una distribuzione linux semplice e leggera."
  _chroottitle="Installazione di ArchLinux con chroot"
  _destdir="Directory di destinazione"
  _destinfo1="Digitare il directory di destinazione -- predefinito: /mnt"
  _destinfo2="Dove già avere montato la/le partizioni se necessario."
  _destinfo3="Quest'installazione, cache inclusa, richiede ~600Mo liberi."
  _instdir="Directory di installazione"
  _archi="Architettura"
  _archi1="Sceglie l'architettura del sistema per l'installazione."
  _archi2="Attenzione! Si può installare soltanto del 32bit da un sistema 32bit !"
  _archi3="Digitare 1 per i686 (32bit) - per difetto"
  _archi4="Digitare 2 per x86_64 (64bit)"
  _archiend="Architettura scelta"
  _dlmirror="Server di scarica"
  _mirror1="Specificare il server da dove scaricare i pacchetti."
  _mirror2="Digitare la sua URL o premere entrata per utilizzare mi.mirror.garr.it."
  defaultmirror="http://mi.mirror.garr.it/mirrors/archlinux/"
  _mirrorend="Server scelto"
  _preconf="Preconfigurazione terminata !"
  _preconf1="Premere entrata per cominciare l'installazione, o Ctrl+C per annullare."
  _chrootdl="Download del mini-chroot..."
  _tgzfound="Archiva trovata in"
  _dlerr="Errore durante download di"
  _dwnlded="fatto."
  _untar="Decompressione..."
  _untarok="fatto."
  _installbase="Installazione del sistema di base..."
  _basesysdone="Sistema di base installato."
  _mounterr="Non può montare"
  _mountok="montato."
  _chrooting="Chroot nel nuovo sistema !"
  _rootpass="Digitare il password di root per il nuovo sistema"
  _plzconfig="Configurare la sua Arch - rc.conf, fstab, hosts, locale.gen... et installare grub. Digitare exit quando finito."
  _umounted="smontato."
  _chrooterr="Errore durante il chroot"
  _installok="Installazione terminata !"
 ;;
 de*)
  _archtitle="Eine einfache und leichtgewichtige Linux-Distribution"
  _chroottitle="ArchLinux-Installation per chroot"
  _destdir="Installations-Zielverzeichnis"
  _destinfo1="Geben Sie das Installations-Zielverzeichnis ein -- Vorgabe: /mnt"
  _destinfo2="Das Installations-Zielverzeichnis muss sich auf einer bereits eingehaengten Partition befinden."
  _destinfo3="Die Installation benoetigt inkl. Cache ca. 600MB freien Platz."
  _instdir="Installationsverzeichnis"
  _archi="Prozessor-Architektur"
  _archi1="Legen Sie die Prozessor-Architektur fuer das zu installierende System fest."
  _archi2="Achtung! Von einem 32-Bit-System kann nur ein 32-Bit-System installiert werden"
  _archi3="Geben Sie 1 fuer ein i686 (32bit)-System ein (Vorgabe)"
  _archi4="Geben Sie 2 fuer ein x86_64 (64bit)-System ein"
  _archiend="ausgewaehlte Architektur"
  _dlmirror="Download-Mirror"
  _mirror1="Geben Sie den Download-Mirror fuer den Paket-Download an."
  _mirror2="Geben Sie seine URL ein oder nehmen Sie einfach archlinux.limun.org durch Druecken der Enter-Taste."
  defaultmirror="http://archlinux.limun.org"
  _mirrorend="Ausgewaehlter Server"
  _preconf="Vorkonfiguration beendet!"
  _preconf1="Enter-Taste druecken, um die Installation zu starten, oder Strg+C um sie abzubrecken."
  _chrootdl="Download der minimalen chroot-Umgebung..."
  _tgzfound="Archiv gefunden in"
  _dlerr="Fehler beim Herunterladen von"
  _dwnlded="heruntergeladen."
  _untar="Entpacken..."
  _untarok="fertig."
  _installbase="Installation des Basis-Systems..."
  _basesysdone="Basis-System installiert."
  _mounterr="Kann nicht einhaengen"
  _mountok="eingehaengt."
  _chrooting="Mit chroot ins neue System wechseln!"
  _rootpass="Geben Sie das Passwort fuer das neue System ein"
  _plzconfig="Konfigurieren Sie Ihr Arch - rc.conf, fstab, hosts, locale.gen... und installieren Sie grub. Wenn Sie fertig sind, exit eingeben."
  _umounted="ausgehaengt."
  _chrooterr="chroot-Fehler"
  _installok="Installation beendet!"
 ;;
 *)
  _archtitle="A simple, lightweight linux distribution."
  _chroottitle="ArchLinux installation by chroot"
  _destdir="Destination directory"
  _destinfo1="Type the path to the destination directory -- by default : /mnt"
  _destinfo2="Your partitions must already be mounted there. (if needed)"
  _destinfo3="This installation, cache included, needs ~600Mio of disk space."
  _instdir="Installation directory"
  _archi="Architecture"
  _archi1="Choose the architecture of the system to install."
  _archi2="Warning! From a 32bit system, you can only install a 32bit system!"
  _archi3="Type 1 for i686 (32bit) -- by default"
  _archi4="Type 2 for x86_64 (64bit)"
  _archiend="Architecture chosen"
  _dlmirror="Download mirror"
  _mirror1="Indicate the server to download packages from."
  _mirror2="Type its URL or press enter to use archlinux.unixheads.org."
  defaultmirror="http://archlinux.unixheads.org"
  _mirrorend="Server chosen"
  _preconf="Preconfiguration finished!"
  _preconf1="Press enter to start the installation, or Ctrl+C to cancel."
  _chrootdl="Downloading minimal chroot environment..."
  _tgzfound="Archive found in"
  _dlerr="Error downloading"
  _dwnlded="downloaded."
  _untar="Decompressing..."
  _untarok="done."
  _installbase="Installating base system..."
  _basesysdone="Base system installed."
  _mounterr="Cannot mount"
  _mountok="mounted."
  _chrooting="Chrooting in the new system!"
  _rootpass="Enter the root password for the new system"
  _plzconfig="Configure your Arch - rc.conf, fstab, hosts, locale.gen... and install grub. Type exit when finished."
  _umounted="unmounted."
  _chrooterr="Error when chrooting"
  _installok="Installation finished!"
esac

## taratatapouet
clear
echo -e "           \E[1;36m,\E[1;36m                       _     _ _"
echo -e "          \E[1;36m/#\\ \E[1;36m       __ _ _ __ ___| |__ | (_)_ __  _   ___  __"
echo -e "         \E[1;36m,###\\ \E[1;36m     / _\` | '__/ __| '_ \| | | '_ \| | | \ \/ /"
echo -e "        \E[1;36m/#####\\ \E[1;36m   | (_| | | | (__| | | | | | | | | |_| |>  <"
echo -e "       \E[1;36m/##\E[0;36m,-,##\\ \E[1;36m   \\__,_|_|  \\___|_| |_|_|_|_| |_|\\__,_/_/\\_\\"
echo -e "      \E[0;36m/##(   )##\\"
echo -e "     \E[0;36m/#.--   --.#\\ \E[1;37m $_archtitle"
echo -e "    \E[0;36m/\`           \`\\ \E[0m"
echo
echo -e '          \E[37;44m'"\033[1m ===== $_chroottitle ===== \033[0m"
echo

## Variables de fonctionnement
echo -e '\E[37;44m'"\033[1m $_destdir \033[0m"
echo "$_destinfo1"
echo
echo "$_destinfo2"
echo "$_destinfo3"
echo
echo -en '\E[33;1m⇒ \E[0m' && read install_path
[ -z "$install_path" ] && install_path=/mnt
#on met le chemin en absolu
[ "$(echo "$install_path" |grep -oE [/]{1}.*)" ] || install_path="$(pwd)/$install_path"
echo -en "\E[0m"
echo -e "$_instdir : \E[33;1m$install_path\E[0m"

echo
echo -e '\E[37;44m'"\033[1m $_archi \033[0m"
echo "$_archi1"
echo
echo "$_archi2"
echo "$_archi3"
echo "$_archi4"
echo
echo -en '\E[33;1m⇒ \E[0m' && read
[ "$REPLY" == "2" ] && arch=x86_64
if [ -z "$arch" ];then
 arch=i686
 [ "$(arch)" == "x86_64" ] && linux32opt=linux32
fi
echo -en "\E[0m"
echo -e "$_archiend : \E[33;1m$arch\E[0m"

echo
echo -e '\E[37;44m'"\033[1m $_dlmirror \033[0m"
echo "$_mirror1"
echo "$_mirror2"
echo
echo -en '\E[33;1m⇒ \E[0m' && read mirror
[ -z "$mirror" ] && mirror="$defaultmirror"
[ -z "$(echo $mirror |grep -Eo '^http://|^ftp://')" ] && mirror="http://$mirror"
echo -en "\E[0m"
echo -e "$_mirrorend : \E[33;1m$mirror\E[0m"

echo
echo -e '     \E[37;44m'"\033[1m     $_preconf    \033[0m"
echo
echo "$_preconf1"
read

#pour quitter proprement
umountexit() {
 umount "$install_path"/dev
 umount "$install_path"/proc
 umount "$install_path"/sys
 exit 0
}

## C’est parti
[ -e "$install_path" ] || mkdir -p "$install_path"
cd "$install_path"
[ -e tmp ] || mkdir tmp
chmod 1777 tmp

## Téléchargement du chroot minimal
minichrooturl="http://mir.archlinux.fr/~tuxce/chroot"
minichrootarchive="archlinux.chroot.${arch}.tgz"
echo
cd tmp
echo -e '\E[37;44m'"\033[1m $_chrootdl \033[0m"
if [ -e "${minichrootarchive}" ];then
 echo "$_tgzfound $install_path/tmp"
else
 if wget -q "${minichrooturl}/${minichrootarchive}";then
  echo "$minichrootarchive : $_dwnlded"
 else
  echo "$_dlerr ${minichrooturl}/${minichrootarchive} !"
  exit 1
 fi
fi
cd ..
echo

echo -e '\E[37;44m'"\033[1m $_untar \033[0m"
tar xf "tmp/${minichrootarchive}"  && echo "${minichrootarchive} : $_untarok" || exit
echo

## Appliquer le miroir
[ -e "$install_path/etc/pacman.d" ] || mkdir -p "$install_path"/etc/pacman.d
echo "Server = $mirror/\$repo/os/$arch/" > "$install_path"/etc/pacman.d/mirrorlist

## C'est reparti
echo -e '\E[37;44m'"\033[1m $_installbase \033[0m"
echo
# Montage des dossiers système pour chrooter
mount -o bind /proc "$install_path"/proc || (echo "$_mounterr /proc";umountexit) && echo "/proc $_mountok"
mount -o bind /dev "$install_path"/dev || (echo "$_mounterr /dev";umountexit) && echo "/dev $_mountok"
mount -o bind /sys "$install_path"/sys || (echo "$_mounterr /sys";umountexit) && echo "/sys $_mountok"
echo
#DNS
[ -e "$install_path/etc" ] || mkdir "$install_path"/etc
cp /etc/resolv.conf "$install_path"/etc/
# Installation de la base
chroot "$install_path" $linux32opt pacman -Syf base --noconfirm || umountexit
echo
echo "$_basesysdone"

echo
echo -e '     \E[37;44m'"\033[1m     $_chrooting     \033[0m"
echo

## on prépare un script
echo '#!/bin/bash' > root/install
echo "echo Server = $mirror/'\$repo'/os/$arch/ >> /etc/pacman.d/mirrorlist
cp /usr/lib/grub/i386-pc/* /boot/grub/
echo
echo $_rootpass
while true;do passwd root && break;done
echo
echo $_plzconfig
bash" >> root/install
chmod +x root/install

chroot "$install_path" $linux32opt /root/install
chrooterr=$?

echo
umount sys && echo "/sys $_umounted"
umount proc && echo "/proc $_umounted"
umount dev && echo "/dev $_umounted"

rm root/install
rm tmp/*

echo
if [ "$chrooterr" == "125" -o  "$chrooterr" == "126" -o "$chrooterr" == "127" ];then
 echo "$_chrooterr"
else
 echo -e '     \E[37;44m'"\033[1m     $_installok     \033[0m"
fi
echo
