#!/bin/bash

export pty=True
export NEEDRESTART_MODE=a
export DEBIAN_FRONTEND=noninteractive

if [ -n "$SUDO_USER" ]; then
  source /home/$SUDO_USER/.index.sh
else
  source $HOME/.index.sh
fi

destpath=$(sed -n '2p' /srv/scripts/config/backupcont)

[ -d "$destpath" ] || {
  clear; echo "ERRO: A pasta de backups definida em $destpath não existe. Verifique!"; sleep 3
  exit 0
}

serverip="172.20.0.46"
lanhost="http://$serverip"
wanhost="http://z.net-freaks.com:4646"

# Absolute Random!
usern4me="c841af1850c240cea68d18878d435bed"
passw0rd="d41d8cd98f00b204e9800998ecf8427e"

if ping -c 1 $serverip >/dev/null; then
  webadress="$lanhost"
else
  webadress="$wanhost"
fi

# More Random!
hash1="cded81e2523899b1f981bab084bcf1c4"
hash2="e7ad599887b1baf90b830435dac14ba3"
hash3="4a45c115f438d5da4b6ea856153bc9d9"
hash4="445b6c94326839fcb988a096760f0e13"
hash5="f9c88e3c538ce7379fdd4bcc2c71f103"

clear; echo ""; read -p "Digite seu Token: " -r "token0"

function init0 {
  [ -z "$token0" ] && {
    dialog --title "ERROR" --msgbox "É necessário digitar seu Token para continuar!" 6 40
    return
  } || {
    [ "$token0" = "$hash1" ] && {
      empresa="brunobr"
      linkcloud "$empresa"
      listfolders0 "BR Info Store!"
      linkcloud "$empresa" "$folderchoice"
      listfolders0 "BR Info Store!"
      downloadcloud0
      return
    } || {
      [ "$token0" = "$hash2" ] && {
        empresa="david"
        linkcloud "$empresa"
        listfolders0 "DH Tech Info!"
        linkcloud "$empresa" "$folderchoice"
        listfolders0 "DH Tech Info!"
        downloadcloud0
        return
      } || {
        [ "$token0" = "$hash3" ] && {
          empresa="brunowr"
          linkcloud "$empresa"
          listfolders0 "Bruno!"
          linkcloud "$empresa" "$folderchoice"
          listfolders0 "Bruno!"
          downloadcloud0
          return
        } || {
          [ "$token0" = "$hash4" ] && {
            empresa="excelsior"
            linkcloud "$empresa"
            listfolders0 "Eu mesmo uhul!"
            linkcloud "$empresa" "$folderchoice"
            listfolders0 "Eu mesmo uhul!"
            downloadcloud0
            return
          } || {
            [ "$token0" = "$hash5" ] && {
              empresa="suitit"
              linkcloud "$empresa"
              listfolders0 "SuitIT!"
              linkcloud "$empresa" "$folderchoice"
              listfolders0 "SuitIT!"
              downloadcloud0
              return
            } || {
              clear
              dialog --title "ERROR" --msgbox "Token incorreto. \nTente novamente!" 6 30
              timeout=$((timeout+1)); sleep $timeout
              return
            }
          }
        }
      }
    }
  }
}

function linkcloud {
  linkparameter="$webadress/$1/$2"
  URLG1=$(curl -ku "$usern4me":"$passw0rd" "$linkparameter" | grep -o 'href=".*">' | sed -e "s/href=\"//g" | sed -e 's/">//g' | sort)
}

function listfolders0 {
IFS=$'\n' read -rd '' -a options <<< "$(printf "%s\n" $URLG1 | sort)"
cmd=(dialog --keep-tite --backtitle "Bem-Vindo $1" --menu "Selecione uma opcao:" 0 0 0)
menu_options=()
for ((i = 0; i < ${#options[@]}; i++)); do
    menu_options+=("$((i + 1))" "${options[i]}")
done
choice=$("${cmd[@]}" "${menu_options[@]}" 2>&1 >/dev/tty)
[ $? -ne 0 ] && return

folderchoice="${options[choice - 1]}"

}

function downloadcloud0 {
  sudo wget --user $usern4me --password $passw0rd "$linkparameter"/"$folderchoice" -O "$destpath"/"$folderchoice"
}

init0
exit 1
