#!/bin/bash

##########################################
#  Gepatto 2023                          #
##########################################

### Color  Variables ###
BLACK="\e[30m"
RED="\e[31m"
GREEN="\e[32m"
YELLOW="\e[33m"
BLUE="\e[34m"
PURPLE="\e[35m"
TEAL="\e[36m"
WHITE="\e[37m"
WARN="\e[38m"
CLEAR='\e[0m'

# >>>>>> HELPER FUNCTIONS
Section() {
    local color="${2:-$YELLOW}";echo -en "$color\n--------------------------------------------------------------------------------\n* \n* "$1"\n* \n--------------------------------------------------------------------------------$WHITE\n"          
} 
Line() {
    local color="${2:-$YELLOW}";echo -en "$color\n*  "$1"$WHITE\n"
}
Done() {
    echo -en "$GREEN\n--------------------------------------------------------------------------------\n* \n* "$1"\n* \n--------------------------------------------------------------------------------$WHITE\n"          
}
Warn() {
    echo -en "$WARN\n--------------------------------------------------------------------------------\n* \n* "$1"\n* \n--------------------------------------------------------------------------------$WHITE\n"          
}
Fail() {
    echo -en "$RED\n--------------------------------------------------------------------------------\n* \n* "$1"\n* \n--------------------------------------------------------------------------------$WHITE\n"          
}
Confirm() {
    echo -en "\n$PURPLE--------------------------------------------------------------------------------\n* $1?$WHITE "
    read -p "[Y/n] " -n 1 -r
    echo -en "\n$PURPLE--------------------------------------------------------------------------------\n$WHITE"          
}
ColorGreen(){
    echo -en $GREEN$1$CLEAR
}
ColorBlue(){
    echo -en $BLUE$1$CLEAR
}
ColorPurple(){
    echo -en $PURPLE$1$CLEAR
}
#END HELPER FUNCTIONS <<<<<<<<


THISHOST=$(hostname)
SMBPASSWORD="billabong"

function installSamba {
    Section "Installing Samba from repository"

    sudo apt-get -y install samba samba-common-bin
}

function test(){
    if [ `pdbedit -w -L | awk -F":" '{ print $1 }' | grep 'patrick' | wc -l` -eq 1 ]; then
     Line "samba user $USER exists"
    fi
}

function addSambaUser {    
    Section "adding Samba User"

    if [ `sudo pdbedit -L  | awk -F":" '{ print $1  }' | grep $USER | wc -l` -eq 1 ]; then
        echo "samba user $USER exists"
    else
        echo  "enter as password for samba user $USER"
        read SMBPASSWORD
        echo "creating samba user: $USER with given password"
        (echo $SMBPASSWORD; echo $SMBPASSWORD) | sudo smbpasswd -a $USER
        fi
}

function configureSamba {    
    Section "configuring samba with custom settings"

    sudo mkdir -p /etc/samba/conf.d/

    ## backup original smb.conf
    if [ ! -f /etc/samba/smb.conf.dist ]; then
        
        if [ -f /etc/samba/smb.conf ]; then
			Line "copying smb.conf to smb.conf.dist"
            sudo cp /etc/samba/smb.conf /etc/samba/smb.conf.dist
			
            Line "removing unused lines"
            sudo sh -c "grep -ve ^# -ve '^;' -ve ^$ /etc/samba/smb.conf.dist > /etc/samba/smb.conf"
			
            Line "adding include and conf file"
            sudo sh -c "echo '\n include = /etc/samba/conf.d/pishare.conf' >> /etc/samba/smb.conf"

            #wget "https://rpi.gepatto.nl/smbconf" 
            #sudo mv smbconf smb.conf

            ## create a default share
            sudo sh -c "echo '[$THISHOST]
        comment= $USER Home
        path=/home/$USER
        browseable=Yes
        writeable=Yes
        only guest=no
        create mask=0775
        directory mask=0775
        public=no' >> /etc/samba/conf.d/pishare.conf"

        sudo service smbd restart
        fi
    else
        echo "smb.conf dis was already created: skipping samba setup"
    fi
}

function all {    
    Section "Complete Install"

    installSamba
    addSambaUser
    configureSamba
}

function menu(){
    Section "Menu for installing Samba with custom conf"
    echo -ne \
"$(ColorGreen '1)') Install Samba from repo
$(ColorGreen '2)') add Samba User
$(ColorGreen '3)') modify samba.conf
$(ColorGreen '4)') All of the above
$(ColorGreen '5)') test
$(ColorGreen '0)') Exit
$(ColorPurple '>>  Choose an option:') "
        read a
        case $a in
            1) clear -x;installSamba ; menu ;;
            2) clear -x;addSambaUser ; menu ;;
            3) clear -x;configureSamba ; menu ;;
            4) clear -x;all ; menu ;;
            5) clear -x;test ; menu ;;
       		0) exit 0 ;;
        	*) clear -x;echo -e $RED" Wrong option: "$a$CLEAR; menu;;
        esac
}

# LET'S START THE MENU
clear -x;
menu
