#!/bin/bash
set -e
function print_centered {
     [[ $# == 0 ]] && return 1

     declare -i TERM_COLS="$(tput cols)"
     declare -i str_len="${#1}"
     [[ $str_len -ge $TERM_COLS ]] && {
          echo "$1";
          return 0;
     }

     declare -i filler_len="$(( (TERM_COLS - str_len) / 2 ))"
     [[ $# -ge 2 ]] && ch="${2:0:1}" || ch=" "
     filler=""
     for (( i = 0; i < filler_len; i++ )); do
          filler="${filler}${ch}"
     done

     printf "%s%s%s" "$filler" "$1" "$filler"
     [[ $(( (TERM_COLS - str_len) % 2 )) -ne 0 ]] && printf "%s" "${ch}"
     printf "\n"

     return 0
}

function configuration {
if [ ! -f config.dat ]
then
echo ""
print_centered "Fichier de config source.dat manquant !"
print_centered "Sortie"
exit 0
else
source config.dat
print_centered "Disque source : $hdsource"
print_centered "Disque destination: $hddest"
fi
}

clear
print_centered "$(tput bold)$(tput setaf 1)"
print_centered " ▄▄▄▄    ▄▄▄       ▄████▄   ██ ▄█▀ █    ██  ██▓███  "
print_centered "▓█████▄ ▒████▄    ▒██▀ ▀█   ██▄█▒  ██  ▓██▒▓██░  ██▒"
print_centered "▒██▒ ▄██▒██  ▀█▄  ▒▓█    ▄ ▓███▄░ ▓██  ▒██░▓██░ ██▓▒"
print_centered "▒██░█▀  ░██▄▄▄▄██ ▒▓▓▄ ▄██▒▓██ █▄ ▓▓█  ░██░▒██▄█▓▒ ▒"
print_centered "░▓█  ▀█▓ ▓█   ▓██▒▒ ▓███▀ ░▒██▒ █▄▒▒█████▓ ▒██▒ ░  ░"
print_centered "░▒▓███▀▒ ▒▒   ▓▒█░░ ░▒ ▒  ░▒ ▒▒ ▓▒░▒▓▒ ▒ ▒ ▒▓▒░ ░  ░"
print_centered "▒░▒   ░   ▒   ▒▒ ░  ░  ▒   ░ ░▒ ▒░░░▒░ ░ ░ ░▒ ░     "
print_centered " ░    ░   ░   ▒   ░        ░ ░░ ░  ░░░ ░ ░ ░░       "
print_centered " ░            ░  ░░ ░      ░  ░      ░              "
print_centered "      ░           ░                                 "
print_centered "$(tput setaf 3)$(tput bold)"
print_centered "Windows Users Backup Script"

if [ ! -f /home/wareck/.pass1 ]
then
print_centered "$(tput bold)$(tput setaf 164)"
print_centered "Executer d'abord le script de build !"
print_centered "$(tput setaf 7)"
exit
else
echo "$(tput setaf 7)"
configuration
sleep 5
fi

sudo umount /mnt/stockage
sudo umount /mnt/hybrid
sudo umount /mnt/swap
echo -e "\n$(tput bold)$(tput setaf 164)Nettoyage des disques avant montage :$(tput setaf 7)"
sudo ntfsfix $hdsource
sudo ntfsfix $ĥddest
#sudo ntfsfix /dev/sdd2
if ! [ -d /mnt/stockage ];then sudo mkdir /mnt/stockage;fi
if ! [ -d /mnt/hybrid ];then sudo mkdir /mnt/hybrid;fi
if ! [ -d /mnt/swap ];then sudo mkdir /mnt/swap;fi
sleep 0.5
echo -e "\n$(tput bold)$(tput setaf 164)Montage du disque Stockage dans /mnt/swap :$(tput setaf 7)"
sudo mount $hddest /mnt/swap
sleep 0.5
echo -e "Ok."
echo -e "\n$(tput bold)$(tput setaf 164)Montage du disque Hybrid dans /mnt/hybrid :$(tput setaf 7)"
sudo mount $hdsource /mnt/hybrid
sleep 0.5
echo -e "Ok."
echo -e "\n$(tput bold)$(tput setaf 164)Delai avant le lancement de copie automatique :$(tput setaf 7)"
for i in {30..1}
do
sleep 0.5
echo -n "."
done
echo -e "\n$(tput bold)$(tput setaf 164)Début de la copie : $(tput setaf 7)"
rsync -v --append --progress --recursive /mnt/hybrid/ /mnt/swap/Backup_Users/
echo -e ""
echo -e "Terminé..."