#!/usr/local/bin/cbsd
#v11.1.7
MYARG="mode"
MYOPTARG="jname header display p9device p9path"
MYDESC="Manage bhyve p9 shared folders"
CBSDMODULE="bhyve"
ADDHELP="mode=list - show shares\n\
mode=attach - attach shared folder to jname, p9device= and p9path= required\n\
mode=detach - detach shared folder from jname, p9device= required\n\
p9device - share name, one-word\n\
p9path - shared folders path\n
header=0 don't print header\n\
display= list by comma for column. Default: jname,p9path,p9device\n"

. ${subrdir}/nc.subr

. ${subrdir}/virtual.subr

. ${cbsdinit}
. ${system}

[ -z "${display}" ] && display="jname,p9path,p9device"

#remove commas for loop action on header
mydisplay=$( echo ${display} | ${TR_CMD} ',' '  ')

# upper for header
myheader=$( echo ${mydisplay} | ${TR_CMD} '[:lower:]' '[:upper:]' )

show_header()
{
	local _header="${H1_COLOR}${BOLD}${myheader}${N0_COLOR}"
	[ ${header} -ne 0 ] && ${ECHO} ${_header}
}

# if $1 = "Unregister" then overwrite status to "Unregister"
populate_output_data()
{
	local _i _val

	_status=

	#populate values for in output string
	for _i in ${mydisplay}; do
		_val=""
		eval _val=\$$_i
		[ -z "${_val}" ] && return 0

		if [ -z "${_status}" ]; then
			_status="${N0_COLOR}${_val}"
		else
			_status="${_status} ${_val}"
		fi
	done
}


# $1 - which file from. Eg: local
show_jaildata_from_sql()
{
	local _i

	#   set sqlfile for ". rcconf" including
	if [ -n "${1}" ]; then
		sqlfile="$1"
	else
		sqlfile="local"
	fi

	[ -n "${2}" ] && local jname="${2}"

	_status=
	_sql="SELECT p9path,p9device FROM p9shares"
	cbsdsqlro ${sqlfile} ${_sql}| while read p9path p9device; do
		populate_output_data
		printf "${N2_COLOR}"
		printf "${_status}"
		printf "${N0_COLOR}\n"
	done
}


# return 1 if p9device record exist
#  $p9device, $mydb must be set
share_exist()
{
	local _tmp

	_tmp=$( cbsdsqlro ${mydb} SELECT p9path FROM p9shares WHERE p9device=\"${p9device}\" 2>/dev/null )

	if [ -n "${_tmp}" ]; then
		return 0
	else
		return 1
	fi
}

show_shares()
{
	show_header

	for i in ${jname}; do
		mydb="${jailsysdir}/${i}/local.sqlite"
		show_jaildata_from_sql ${mydb} ${i}
	done

	return 0
}

[ -z "${header}" ] && header=1
sqldelimer=" "

case "${mode}" in
	list)
		[ -z "${jname}" ] && jname=$( ${miscdir}/sqlcli ${dbdir}/local.sqlite "SELECT jname FROM jails WHERE emulator = \"bhyve\"" )

		show_shares | /usr/bin/column -t

		err 0 ""
		;;
	attach)
		[ -z "${jname}" ] && err 1 "${N1_COLOR}Please set ${N2_COLOR}jname=${N0_COLOR}"
		[ -z "${p9path}" -o -z "${p9device}" ] && err 1 "${N2_COLOR}p9path= ${N1_COLOR}and ${N2_COLOR}p9device=${N1_COLOR} must be set${N0_COLOR}"
		mydb="${jailsysdir}/${jname}/local.sqlite"

		if share_exist; then
			err 1 "${N1_COLOR}Share with p9device=${N2_COLOR}${p9device}${N1_COLOR} already exist${N0_COLOR}"
		fi

		[ ! -d ${p9path} ] && err 1 "${N1_COLOR}Path not exist: ${N2_COLOR}${p9path}${N0_COLOR}"

		cbsdsqlrw ${mydb} "INSERT INTO p9shares ( p9path, p9device ) VALUES ( \"${p9path}\", \"${p9device}\" )"
		err 0 "${N1_COLOR}Attached${N0_COLOR}"
		;;
	detach)
		[ -z "${jname}" ] && err 1 "${N1_COLOR}Please set ${N2_COLOR}jname=${N0_COLOR}"
		[ -z "${p9device}" ] && err 1 "${N2_COLOR}${N2_COLOR}p9device=${N1_COLOR} must be set${N0_COLOR}"
		mydb="${jailsysdir}/${jname}/local.sqlite"

		if share_exist; then
			cbsdsqlrw ${mydb} "DELETE FROM p9shares WHERE p9device=\"${p9device}\""
		else
			err 1 "${N1_COLOR}Share with p9device=${N2_COLOR}${p9device}${N1_COLOR} not exist${N0_COLOR}"
		fi
		cbsdsqlrw ${mydb} "DELETE FROM p9shares WHERE p9device=\"${p9device}\""
		err 0 "${N1_COLOR}Dettached${N0_COLOR}"
		;;
	*)
		err 1 "${N1_COLOR}Unknown mode: ${N2_COLOR}${mode}${N0_COLOR}"
esac
