#!/bin/bash
do {
	#////////////////////////////////////
	# DietPi Function:
	# - Setup and apply DietPi smbclient details
	#
	#////////////////////////////////////
	# Created by Daniel Knight / daniel.knight@dietpi.com / dietpi.com
	#
	#////////////////////////////////////
	#
	# Info:
	# - Menu system that allows users to change Samba Client details stored in dietpi.txt and automatically mount.
	# - Applies details to /etc/fstab
	# - Mounts to /mnt/samba if successful
	#
	# Usage:
	# - /DietPi/dietpi/func/dietpi-set_smbclient	= Menu
	# - /DietPi/dietpi/func/dietpi-set_smbclient 1	= Apply and mount using settings in dietpi.txt
	#////////////////////////////////////

	#Grab Input
	setvar INPUT = '0'
	if [[ $1 =~ ^-?[0-9]+$ ]] {
		setvar INPUT = "$1"
	}

	#Import DietPi-Globals ---------------------------------------------------------------
	source /DietPi/dietpi/func/dietpi-globals
	G_CHECK_ROOT_USER
	export G_PROGRAM_NAME='DietPi-Set_smbclient'
	#Import DietPi-Globals ---------------------------------------------------------------

	#/////////////////////////////////////////////////////////////////////////////////////
	#smbclient data
	#/////////////////////////////////////////////////////////////////////////////////////
	setvar OPTION = '0'
	setvar CHOICE = '0'
	setvar samba_clientname = $(cat /DietPi/dietpi.txt | grep -m1 '^CONFIG_SMBCLIENT_COMPUTERNAME=' | sed 's/.*=//')
	setvar samba_clientshare = $(cat /DietPi/dietpi.txt | grep -m1 '^CONFIG_SMBCLIENT_SHARENAME=' | sed 's/.*=//')
	setvar samba_clientusename = $(cat /DietPi/dietpi.txt | grep -m1 '^CONFIG_SMBCLIENT_USERNAME=' | sed 's/.*=//')
	setvar samba_clientpassword = $(cat /DietPi/dietpi.txt | grep -m1 '^CONFIG_SMBCLIENT_PASSWORD=' | sed 's/.*=//')

	proc Apply_And_Mount {

		#NB: Convert spaces into '\040': https://github.com/Fourdee/DietPi/issues/1201#issuecomment-339720271
		local space_to_040=$(echo -e "$samba_clientshare" | sed 's/ /\\\\040/g')

		#Apply to fstab
		sed -i "/\/mnt\/samba/c\\/\/$samba_clientname\/$space_to_040 \/mnt\/samba cifs username=$samba_clientusename,password=$samba_clientpassword,iocharset=utf8,sec=ntlm,nofail  0  0" /etc/fstab

		#Mount up
		mount -a

	}

	#/////////////////////////////////////////////////////////////////////////////////////
	# Main Loop
	#/////////////////////////////////////////////////////////////////////////////////////
	#-----------------------------------------------------------------------------------
	#Menu
	if (( $INPUT == 0 )) {
		setvar OPTION = $(whiptail --inputbox "Please enter the fileservers IP address\n - eg: 192.168.0.2" 9 65 "$samba_clientname" --title "Samba Client Setup" 3>&1 1>&2 2>&3)
		setvar CHOICE = ""$?
		if (( $CHOICE == 0 )) {
			setvar samba_clientname = "$OPTION"

			setvar OPTION = $(whiptail --inputbox "Please enter the fileservers shared folder name\n - eg: MySharedFolder" 9 65 "$samba_clientshare" --title "Samba Client Setup" 3>&1 1>&2 2>&3)
			setvar CHOICE = ""$?
			if (( $CHOICE == 0 )) {
				setvar samba_clientshare = "$OPTION"

				#Username
				setvar OPTION = $(whiptail --inputbox "Please enter the fileservers username\n - eg: JoeBloggs" 9 65 "$samba_clientusename" --title "Samba Client Setup" 3>&1 1>&2 2>&3)
				setvar CHOICE = ""$?
				if (( $CHOICE == 0 )) {
					setvar samba_clientusename = "$OPTION"

					#Password
					setvar OPTION = $(whiptail --inputbox "Please enter the fileservers password\n - eg: LetMeIn\n - (NOTICE) This will be stored with no encryption" 10 65 "$samba_clientpassword" --title "Samba Client Setup" 3>&1 1>&2 2>&3)
					setvar CHOICE = ""$?
					if (( $CHOICE == 0 )) {
						setvar samba_clientpassword = "$OPTION"

						#Unmount if connected
						clear
						echo -e "\n\n Attempting mount, please wait...."
						umount /mnt/samba &> /dev/null

						#Save to Dietpi.txt
						sed -i "/CONFIG_SMBCLIENT_COMPUTERNAME/c\CONFIG_SMBCLIENT_COMPUTERNAME=$samba_clientname" /DietPi/dietpi.txt
						sed -i "/CONFIG_SMBCLIENT_SHARENAME/c\CONFIG_SMBCLIENT_SHARENAME=$samba_clientshare" /DietPi/dietpi.txt
						sed -i "/CONFIG_SMBCLIENT_USERNAME/c\CONFIG_SMBCLIENT_USERNAME=$samba_clientusename" /DietPi/dietpi.txt
						sed -i "/CONFIG_SMBCLIENT_PASSWORD/c\CONFIG_SMBCLIENT_PASSWORD=$samba_clientpassword" /DietPi/dietpi.txt

						Apply_And_Mount
					}
				}
			}
		}

	#-----------------------------------------------------------------------------------
	#Apply and mount. Using settings from dietpi.txt
	} elif (( $INPUT == 1 )) {
		Apply_And_Mount
	}

	#-----------------------------------------------------------------------------------
	exit
	#-----------------------------------------------------------------------------------
}