#!/bin/bash
#
#swpc=2
#swp="-$(echo $swpc $(free | grep Mem) | awk '{print ($3/1024)+((($3/1024)*$1)/100)}' | tr '.,' ' ' | cut -d ' ' -f1)MiB" 
#echo $swp
#exit
###############################
# Disco da Instalação ex.:sda #
###############################
clear
echo -e "\a"
echo "###########################################"
echo "# Disco que sera Usado para a Instalação! #"
echo "###########################################"
lsblk | grep 'disk'
read -p "Digite o 'Disco' que sera usado na Instalação! ex.: sda >" di
#
if ! [[ -e "/dev/$di" ]]; then
    echo -e "\a"
    echo "########################################"
    echo "# Disco da Instalação '$di' Não Existe #"
    echo "########################################"
    exit
fi
echo "Disco escolhido $di"
################
# Nome do Host #
################
hostn=""
echo -e ""
echo "############################################################"
echo "# Nome do Host 'Maquina' que Sera Usado para a Instalação! #"
echo "############################################################"
read -p "Digite o Nome do 'Host' que sera usado na Instalação! ex.: maquina1 >" hostn
#
if ! [[ "$hostn" ]]; then
    echo -e "\a"
    echo "#############################################"
    echo "# Nome do 'Host' da Instalação esta 'Vazio' #"
    echo "#############################################"
    exit
fi
echo "Nome do Host escolhido $hostn"
####################################
# Sistema de Arquivo Ext4 ou Btrfs #
####################################
sf=""
echo -e ""
echo "#####################################################################"
echo "# Escolha o 'Sistema de Arquivos' que Sera Usado para a Instalação! #"
echo "# ex.: Ext4 ou Btrfs                                                #"
echo "#####################################################################"
read -p "Digite o 'Sistema de Arquivos' que sera usado na Instalação! ex.: Ext4 >" sf
#
if ! [[ "$sf" == "Ext4" || "$sf" == "Btrfs" ]]; then
    echo -e "\a"
    echo "#######################################################"
    echo "# O 'Sistema de Arquivos' da Instalação esta 'Errado' #"
    echo "#######################################################"
    exit
fi
echo "O Sistema de Arquivos escolhido $sf"
###########################
# Tamanho da Memoria Swap #
###########################
swp=""
echo -e ""
echo "############################################################################"
echo "# Escolha a 'Quantidade de Memoria Swap' que Sera Usado para a Instalação! #"
echo "# Igual a Memoria do Host '1' O dobro Da memoria do Host '2x'              #"
echo "# A quantidade em Porcentagem da Memoria do Host '%' ou Nenhuma '0'        #"
echo "############################################################################"
read -p "Digite a 'Quantidade de Memoria Swap' que sera usado na Instalação! ex.: 1 >" swp
#
if ! [[ "$swp" == "1" || "$swp" == "2x" || "$swp" == "0" || "$swp" == "%" ]]; then
    echo -e "\a"
    echo "##############################################################"
    echo "# A 'Quantidade de Memoria Swap' da Instalação esta 'Errado' #"
    echo "##############################################################"
    exit
fi

if  [[ "$swp" == "1" ]]; then
    swp="-$(free | grep Mem | awk '{print ($2/1024)}' | tr '.,' ' ' | cut -d ' ' -f1)MiB"
fi

if  [[ "$swp" == "2x" ]]; then
    swp="-$(free | grep Mem | awk '{print ($2/1024)*2}' | tr '.,' ' ' | cut -d ' ' -f1)MiB"
fi

if  [[ "$swp" == "%" ]]; then
    read -p "Digite a 'Quantidade de Memoria Swap' em % que sera usado na Instalação! ex.: 5 >" swpc
    echo " aqui --> $swpc"
    swp="-$(echo $swpc $(free | grep Mem) | awk '{print ($3/1024)+((($3/1024)*$1)/100)}' | tr '.,' ' ' | cut -d ' ' -f1)MiB" 
#   swp="-$(free | grep Mem | awk '{print ($2/1024)+(($2/1024)*4)/100}' | tr '.,' ' ' | cut -d ' ' -f1)MiB"
fi

if  [[ "$swp" == "0" ]]; then
    swp=0 # "-$(free | grep Mem | awk '{print ($2/1024)+(($2/1024)*2)/100}' | tr '.,' ' ' | cut -d ' ' -f1)MiB"
fi
echo "Quantidade de Swap escolhido $swp"

exit
