#!/usr/bin/perl -w
#------------------------------------------------------------------------------
# Administrator settings top CGI
#
# Copyright (c) BAYBITS LLC. All rights reserved.
#------------------------------------------------------------------------------

use strict;
use utf8;
use HTML::Template;
use TF::CommonUtils;

binmode(STDOUT, ":utf8");

my $LOCATION      = $ENV{'TF_LOCATION'} || '';
my $BRAND         = $ENV{'TF_BRANDNAME'} || '';
my $TF_VERSION    = $ENV{'TF_VERSION'} || '';
my $USERTYPE      = $ENV{'TF_USERTYPE'} || 'normal';
my $LANGPARAM     = TF::CommonUtils::getLangPriority();
my $TEMPLATE_NAME = 'adminsettings.tmpl.' . $LANGPARAM;
my $TF_LICENSEUPLOADER = $ENV{'TF_LICENSEUPLOADER'} || 'off';

if (!$LOCATION || !$USERTYPE || $USERTYPE ne 'admin') {
	TF::CommonUtils::showAccessDeny();
	die '';
}

main();

sub main {
	my $template_file = findTemplateFile($TEMPLATE_NAME);
	if (open(my $tmplfh, "<", $template_file)) {
		binmode($tmplfh, ":utf8");
		my $htmltmpl = HTML::Template->new(
			filehandle => $tmplfh,
			case_sensitive => 1,
			die_on_bad_params => 0,
		);
		$htmltmpl->param(
			brandname => $BRAND,
			tfversion => $TF_VERSION,
			location => $LOCATION,
			licenseUploaderEnabled => ($TF_LICENSEUPLOADER eq 'on') ? 1 : 0,
		);
		print "Content-type: text/html; charset=UTF-8\n\n";
		print $htmltmpl->output;
		close($tmplfh);
	}
	else {
		TF::CommonUtils::showTemplateOpenErr($LANGPARAM);
	}
}

sub findTemplateFile {
	my $name = shift || '';
	my @candidates = ("./tmpl/$name", "../tmpl/$name");

	foreach my $path (@candidates) {
		return $path if -f $path;
	}
	return $candidates[0];
}
