#!/usr/share/ucs-test/runner bash
# shellcheck shell=bash
## desc: "Upload printer driver via rpcclient"
## exposure: dangerous
## packages:
##  - univention-samba | univention-samba4
## bugs: [55048, 55940]
## roles:
## - domaincontroller_master
## - domaincontroller_backup
## - domaincontroller_slave
## - memberserver

# shellcheck disable=SC1091
. "$TESTLIBPATH/base.sh" || exit 137
# shellcheck disable=SC2154
admin_account="$(echo "$tests_domainadmin_account" | sed -e 's|uid=||;s|,.*||')"
# shellcheck disable=SC2154
admin_passwd="$tests_domainadmin_pwd"

upload_driver_file () {
	local tmp_file
	tmp_file="$(su "$admin_account" -c "mktemp")"
	cd /tmp || fail_fast 1 "cd /tmp failed"
	tmp_file="$(basename "$tmp_file")"
	smbclient "//$(hostname)/print\$" -U "$admin_account%$admin_passwd" -c "prompt; put $tmp_file x64/$tmp_file" || fail_fast 1 "smbclient failed"
	echo "$tmp_file"
}

driver_name="test-driver-$(makepasswd)"

driver_file="$(upload_driver_file)"
driver_file="$(upload_driver_file)"
data_file="$(upload_driver_file)"
config_file="$(upload_driver_file)"
help_file="$(upload_driver_file)"
language_monitor_file="$(upload_driver_file)"

driver_settings="$driver_name"
driver_settings+=":$driver_file"
driver_settings+=":$data_file"
driver_settings+=":$config_file"
driver_settings+=":$help_file"
driver_settings+=":$language_monitor_file"

driver_settings+=":RAW:"

# upload driver
rpcclient -U "$admin_account%$admin_passwd" localhost -c "adddriver 'Windows x64' '$driver_settings'" || fail_fast 1 "adddriver failed"

# check uploaded driver
rpcclient -U "$admin_account%$admin_passwd" localhost -c "enumdrivers 3"
rpcclient -U "$admin_account%$admin_passwd" localhost -c "enumdrivers 3" |
	grep "Driver Name: \[$driver_name\]" ||
	fail_fast 1 "could not find new driver in enumdrivers"

exit "$RETVAL"
