#!/bin/bash

. ${TOOLKIT}-functions.sh


add_share()
{
	local share=$1
	local desc=${2:-""}
	shift 2
	local args="$@"
	local name=
	local path=
	local opts=

	if echo "$share" | grep -q ","
	then
		path=`echo "$share" | cut -d',' -f1`
		opts=`echo "$share" | sed -e "s}^$path,}}"`
	else
		path="$share"
	fi

	if echo "$path" | grep -q "="
	then
		name=`echo "$path" | cut -d'=' -f1`
		path=`echo "$path" | sed -e "s}^$name=}}"`
	else
		name=`basename "$path"`
	fi

	grep -q "^$name$" $done && {
		echo "  WARN: ignoring duplicate share name '$name'"
		return 1
	}

	printf "  share: %-12s on %-20s " "$name" "$path"

	echo "[$name]"			>>$outfile
	echo "  path = $path"		>>$outfile
	echo "  comment = $desc"	>>$outfile
	[ "X$args" != "X" ] && {
	    echo -e "$args" 		>>$outfile
	}

	local opt=
	local arg=

	for opt in `echo "$opts" | tr ',' ' '`
	do
	    case $opt in
	      GROUP:*)	arg=`echo "$opt" | cut -d':' -f2`
	      		echo "  force group = +$arg" >>$outfile
			;;
	      USER:*)	arg=`echo "$opt" | cut -d':' -f2`
	      		echo "  force user = +$arg" >>$outfile
			;;
	      *)	echo "  $opt" >>$outfile
	      		;;
	    esac
	done

	if [ -d $path ]
	then
		echo "(ok)"
	else
		echo "  WARN! path not existent!"
	fi
	
	echo "$name" >>$done
	return 0
}




myself=`uname -n`
myself_short=`uname -n | sed -e 's/\..*//'`



# SANITY CHECKS

needs_guest=false
jtconf samba.pub_shares 2>/dev/null >/dev/null && needs_guest=true
jtconf samba.open_shares 2>/dev/null >/dev/null && needs_guest=true

$needs_guest && {
	guest=`jtconf samba.guest_user`	|| exit_missing_define samba.guest_user

	[ "$guest" = "Bad User" ] && {
		putwarning "samba guest user" \
			"you have some shares declared as Guest, but your" \
			"guest user is now set to '$guest', please use a real" \
			"one in 'samba.guest_users' variable"
		exit 1
	}
}


# BUILD SAMBA CONFIG FILE

outfile="smb.conf.tmp"
done="done.tmp"
cp /dev/null $done


# --- COMMMON (GLOBALS) -------------------------------------------------------

# base config file
file=`filepath smb.conf-base` || exit_err 1 "can't find smb.conf-base"
cat $file >>$outfile


# optional global parms
#
echo -e "\t# optional parms\n" >>$outfile
for var in	wins_server \
		directory_mask create_mask force_directory_mode force_create_mode \
		map_archive map_hidden
do
	value=$(jtconf samba.$var 2>/dev/null) || continue
	[ "$value" == "" ] && continue
	key=$(echo "$var" | tr '_' ' ')
	echo "	$key = $value" >>$outfile
done

##value=`jtconf samba.wins_server 2>/dev/null` && {
	##[ "$value" != "" ] && 	echo "	wins server = $value" >>$outfile
##} || :
##value=`jtconf samba.directory_mask 2>/dev/null` && {
	##[ "$value" != "" ] && 	echo "	directory mask = $value" >>$outfile
##} || :
##value=`jtconf samba.create_mask 2>/dev/null` && {
	##[ "$value" != "" ] && 	echo "	create mask = $value" >>$outfile
##} || :
##value=`jtconf samba.force_directory_mode 2>/dev/null` && {
	##[ "$value" != "" ] && 	echo "	force directory mode = $value" >>$outfile
##} || :
##value=`jtconf samba.force_create_mode 2>/dev/null` && {
	##[ "$value" != "" ] && 	echo "	force create mode = $value" >>$outfile
##} || :

# check for obsoleted values
value=`jtconf samba.directory_mode 2>/dev/null` && {
	[ "$value" != "" ] && {
		echo "  samba.directory_mode obsoleted, use samba.force_directory_mode instead"
		exit 1
	}
} || :
value=`jtconf samba.create_mode 2>/dev/null` && {
	[ "$value" != "" ] && {
		echo "  samba.create_mode obsoleted, use samba.force_create_mode instead"
		exit 1
	}
} || :



# domain master yes/no?
#
file="smb.conf-no-master-domain"
getconfirm samba.domain_master && {
	echo "  enabling Domain Master"
	file="smb.conf-master-domain"
}
path=`filepath $file` || exit_err 1 "can't find $file"
cat $path >>$outfile


# ldap support yes/no?
#
file="smb.conf-no-ldap"

case $DISTRIB_ID in
  Devuan)
	getconfirm samba.ldap && {
  		echo "  WARNING: LDAP backend not available on samba4"
	}
	;;
  *)
	getconfirm samba.ldap && {
		echo "  enabling LDAP backend"
		if service_requested ldap
		then
			file="smb.conf-ldap"
		else
			putwarning "SAMBA / LDAP" \
				"you have requested LDAP backend in Samba, but you don't have" \
				"ldap service enabled in kusa (services)"
		fi
	}
	;;
esac
path=`filepath $file` || exit_err 1 "can't find $file"
cat $path >>$outfile




# --- SHARES -------------------------------------------------------------------------------

# netlogons?
#
getconfirm samba.domain_master && {
	echo "  share: Netlogon & Profiles"
	file=`filepath smb.conf-netlogon` || exit_err 1 "can't find smb.conf-netlogon"
	cat $file >>$outfile
}


# homedirs?
#
getconfirm samba.share_homes && {
	echo "  share: Homes"
	file=`filepath smb.conf-homes` || exit_err 1 "can't find smb.conf-homes"
	cat $file >>$outfile
}


# printers
#
getconfirm samba.share_print && {
	echo "  share: Print"
	file=`filepath smb.conf-printers` || exit_err 1 "can't find smb.conf-printers"
	cat $file >>$outfile
}


# automatic shares

# rapid (standard)
#
for share in `jtconf samba.rapid_shares 2>/dev/null`
do
	add_share $share ""
done

# pub (guest ok & read-only)
#
for share in `jtconf samba.pub_shares 2>/dev/null`
do
	add_share $share "(pub)" "  guest ok = yes\n  read only = yes"
done

# open (guest ok & READ/WRITE (!)
#
for share in `jtconf samba.open_shares 2>/dev/null`
do
	add_share $share "(open)" "  guest ok = yes\n"
done

# readonly
#
for share in `jtconf samba.ro_shares 2>/dev/null`
do
	add_share $share "" "  read only = yes"
done


# scansione entries samba.shares.* e seleziona quelle per questo host
#
for share in `jtconf --list samba.share.`
do
	# se definita lista server, controlla se sono in una di queste
	#
	if servers=`jtconf $share.servers 2>/dev/null`
	then
		add_share=false
		for srv in `echo $servers | tr ',' ' '`
		do
			[ $srv = $myself -o $srv = $myself_short ] && {
				add_share=true
				break
			}
		done
	else
		add_share=true
	fi
	$add_share || continue	# not for me

	name=`jtconf $share.name 2>/dev/null`
	path=`jtconf $share.path` || exit $?
	desc=`jtconf $share.desc 2>/dev/null`

	[ "$name" ] || {
		name=`echo $share | sed -e 's/^samba\.share\.//'`
	}

	add_share "${name}=$path" "$desc"
done

# machine overrides
#
file=`filepath smb.conf-machine-overrides` || exit_err 1 "can't find smb.machine-overrides"
cat $file >>$outfile


# latest, add local customizations (id any)
#
file=`filepath smb.conf.local` && {
	echo -e "  adding custom settings $file"
	cat $file >>$outfile
}

exit 0
