#!/bin/bash

#####################################################################################################
# Configurações Iniciais: 
#####################################################################################################
#1) Gravar cartão SD, usar o programa Rufus com imagem: 2019-04-08-raspbian-stretch-lite.zip
#1) Link Rufus: [link]
#1) Link Raspbian: [link] 
#2) Ativar o SSH: após gravar imagem no cartão, criar um arquivo chamado ssh.txt no cartão
    Obs: cuidar no nome: ssh.txt - no windows pode acontecer: ssh.txt.txt
#2) o arquivo pode conter qualquer texto ou estar até mesmo vazio ...
#2) Para logar na SBC Raspberry Pi utilizar o Putty.exe no windows [link] ou ssh no linux
#3) Ativar WiFi: após gravar imagem no cartão, criar um arquivo chamado wpa_supplicant.conf contendo:
------------------------------------------------------------------------------------------------------
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=BR

network={
    ssid="NomeDaSuaRedeAqui"
    psk="SenhaDaSuaRedeAqui"
}
------------------------------------------------------------------------------------------------------
# Login
# usr: pi
# psw: raspberry

# Alterar para o Super User (root):
sudo su
# Desligar o Linux:
shutdown -h now
# Reiniciar o Linux:
shutdown -r now

# Instalar o mcedit: 
/usr/bin/sudo su
/usr/bin/apt-get update -y
/usr/bin/apt-get upgrade -y 
/usr/bin/apt-get install mc -y

# Instalar o json, basic calculator, converter txt dos para windows, expect, git:
/usr/bin/apt-get install jq bc dos2unix expect zip unzip i2c-tools git -y

# Trocar a senha root (método manual):
/usr/bin/sudo su
/usr/bin/passwd

# Trocar a senha root (método automático para scripts):
/usr/bin/expect << EOF
spawn "/usr/bin/passwd"
expect "Enter new UNIX password:"
send "12345\r"
expect "Retype new UNIX password:"
send "12345\r"
expect eof
EOF

# Trocar a senha pi (método automático para scripts):
/bin/su pi
/usr/bin/expect << EOF
spawn "/usr/bin/passwd"
expect "(current) UNIX password:"
send "raspberry\r"
expect "Enter new UNIX password:"
send "12345678\r"
expect "Retype new UNIX password:"
send "12345678\r"
expect eof
EOF

# Configurar editor de texto padrão / configuração do history:
# cd /root				
# mcedit .bashrc		
# No final do arquivo insira: 
# alias ll='ls -l --color=auto'
# alias la='ls -la --color=auto'
# export EDITOR=mcedit
# export HISTSIZE=1000
# export HISTFILESIZE=1000
echo "" >> /root/.bashrc
echo "#LT38C - Configuração Básica:" >> /root/.bashrc
echo "alias ll='ls -l --color=auto'" >> /root/.bashrc
echo "alias la='ls -la --color=auto'" >> /root/.bashrc
echo "export EDITOR=mcedit" >> /root/.bashrc
echo "export HISTSIZE=1000" >> /root/.bashrc
echo "export HISTFILESIZE=1000" >> /root/.bashrc

# raspi-config: Alterar o timezone (relógio) ...
/usr/bin/scp /etc/timezone /etc/timezone.bkp
/bin/echo "America/Sao_Paulo" > /etc/timezone
/bin/rm /etc/localtime
/usr/sbin/dpkg-reconfigure -f noninteractive tzdata

# raspi-config: Alterar layout de teclado para ABNT (para quem usar o teclado na porta USB) ...
# 1) localisation options -> change keybord layout: generickey 104
#     -> others: portugues (Brasil) - tab ok | tab ok (Obs: Somente se tiver teclado conectado)
# 2) localisation options -> change locale: pt-br iso 8859-1 (space to select)
#     -> pt-br (enter)
# 3) localisation options -> timezone: America -> São Paulo

# Habilitar o servidor SSH:
# Método 01: Criar o arquivo: /boot/ssh.txt - echo "DL" > /boot/ssh.txt (pode ser no windows / mac)
# Método 02: raspi-config -> 5 Interfacing Options -> P2 SSH
# Método 03: Executar as linhas abaixo (script):

#####################################################################################################
# Ativar o servidor SSH:
#####################################################################################################
# Habilitar o serviço SSH:
update-rc.d ssh enable && invoke-rc.d ssh start && STATUS=enabled
# liberar SSH para usuário root:
/bin/echo "PermitRootLogin yes" >> /etc/ssh/sshd_config

#End