%% hgbthesis.cls
%% LaTeX template for creating diploma theses, masters's theses, bachelor's theses and other term papers.
%% This file is part of the Hagenberg Thesis package for creating bachelors's and masters's theses
%% Author: Wilhelm Burger (wilbur@ieee.org)
%% GitHub: https://github.com/Digital-Media/HagenbergThesis
%% File encoding: UTF-8

% Preferences for the naming macros (commands/variables):
% * internal, undocumented macros (used in release *.cls and *.sty files):   \@macroname or \@MacroName
% * documented internal macros for customizing (eg in theme *.sty files):    \hgb@MacroName or \hgb@macroname
% * documented public macros (available in user documents):                  \hgbMacroName (preferred), \MacroName or \macroname


%% Package Information --------------------------------------------------------

\NeedsTeXFormat{LaTeX2e}[2022/06/01]  % recent LaTeX kernel version, must support \DeclareKeys (ltkeys)
\ProvidesClass{hgbthesis}[2026/10/22]%%AUTO

\RequirePackage{xifthen}

\RequirePackage{ifpdf}	% for checking PDF mode 
\ifpdf{}% \typeout{hgbthesis: *** LaTeX in PDF mode ***}
\else{\errmessage{hgbthesis: *** LaTeX NOT running in PDF mode ***}}
\fi

% Font encoding and fonts, independent of 'hgbpdfa' (pdfTeX only; LuaTeX and XeTeX use Unicode fonts).
% fontenc is not loaded again if T1 is already the default encoding (e.g., set by 'hgbpdfa'), since reloading it
% changes the layout slightly (\@ifpackageloaded cannot be used, because fontenc allows to be loaded multiple times).
\RequirePackage{iftex}
\ifPDFTeX
	\ifthenelse{\equal{\encodingdefault}{T1}}{}{\RequirePackage[T1]{fontenc}}
	\RequirePackage{lmodern}
\fi

\RequirePackage{hgbvars}	% declares shared variables, booleans, and class option helpers

%% Commands for Title Pages (used in class options) ---------------------------


\newboolean{hgb@IsProposal} % indicates if this document is a proposal (true) or final thesis (false).
\setboolean{hgb@IsProposal}{false}
\newcommand{\hgb@ProposalName}{UNDEFINED}

\newcommand{\hgb@@pagelayout}{oneside}% default to 'oneside' (used locally only in this file!)
\newcommand{\hgb@DegreeKey}{}% class option 'degree' (key of the academic degree, see hgbdict.sty)

%% Specifiying/loading thesis theme styles ---------------------------------------------------

\newcommand{\@ThemeID}{default}%			use theme 'hgbtheme-default' if no theme is specified
\newcommand{\@ThemePrefix}{hgbtheme}%	fixed, all theme files are named 'hgbtheme-<themeID>.sty'

% The .sty file for the specified theme (#2) is loaded if it exists, otherwise throws an error:
\newcommand{\hgb@UseTheme}[2][]{%		#1=options passed to theme style file, #2=themeID
	\def\@ThemeName{\@ThemePrefix-#2}
	\def\@ThemeFile{\@ThemeName.sty}
	%\typeout{ThemeFile = \@ThemeFile}
	\IfFileExists{\@ThemeFile}%		true if LaTeX can find this file (need not be local!)
		{\RequirePackage[#1]{\@ThemeName}}%  must load immediately, was {\AtEndOfPackage{\usepackage[#1]{\@ThemeName}}}%
		{\ClassError{hgbthesis}{style file '\@ThemeFile' for theme '#2' could not be found}\relax}%
}

% Class option handling --------------------------------------------------

% Specifiy permissible key/value options:
\@RegisterClassOption{type}{bachelor}
\@RegisterClassOption{type}{master}
\@RegisterClassOption{type}{diploma}
\@RegisterClassOption{type}{phd}
\@RegisterClassOption{type}{internship}

\@RegisterClassOption{language}{english}
\@RegisterClassOption{language}{german}

\@RegisterClassOption{titlelanguage}{english}
\@RegisterClassOption{titlelanguage}{german}

% We are using the \DeclareKeys / \ProcessKeyOptions system (LaTeX2e kernel, 2022+).
% See http://mirrors.ctan.org/macros/latex/base/source2e.pdf (p. 956),
% https://tex.stackexchange.com/questions/417704/key-value-syntax-in-package-options
\DeclareKeys{
	% legacy options (deprecated):
	bachelor.code 	= {\renewcommand{\hgb@ThesisType}{bachelor}\@WarnDeprecatedOption{bachelor}{type=bachelor}},
	master.code 		= {\renewcommand{\hgb@ThesisType}{master}\@WarnDeprecatedOption{master}{type=master}},
	diploma.code 		= {\renewcommand{\hgb@ThesisType}{diploma}\@WarnDeprecatedOption{diploma}{type=diploma}},
	internship.code = {\renewcommand{\hgb@ThesisType}{internship}\@WarnDeprecatedOption{internship}{type=internship}},
	% catch old-style language options (for some reason they are still passed to hgb.sty!)
	german.code     = {\renewcommand{\hgb@MainLanguage}{german}\@WarnDeprecatedOption{german}{language=german}},
	ngerman.code    = {\renewcommand{\hgb@MainLanguage}{german}\@WarnDeprecatedOption{ngerman}{language=german}},
	english.code    = {\renewcommand{\hgb@MainLanguage}{english}\@WarnDeprecatedOption{english}{language=english}},
	% new options
	type.code 				= {\@ValidateClassOption{type}{#1}\renewcommand{\hgb@ThesisType}{#1}},
	theme.code 				= \renewcommand{\@ThemeID}{#1},
	degree.code				= \renewcommand{\hgb@DegreeKey}{#1},	% validated at the end of this file
	language.code			= {\@ValidateClassOption{language}{#1}\renewcommand{\hgb@MainLanguage}{#1}},
	titlelanguage.code = {\@ValidateClassOption{titlelanguage}{#1}\renewcommand{\hgb@TitleLanguage}{#1}},
	proposal.code			= \setboolean{hgb@IsProposal}{#1},
	proposal.default:n	= true,	% https://tex.stackexchange.com/a/682260/175415
	apa.code					= {\ifthenelse{\equal{#1}{true}}{\PassOptionsToPackage{apa}{hgbbib}}{}},
	apa.default:n			= true,
	codestyle.code			= \PassOptionsToPackage{codestyle=#1}{hgblistings},	% color|mono|classic, validated there
	aiusage.code			= \PassOptionsToPackage{aiusage=#1}{hgbaiusage},	% detailed|short, validated there
	%	twoside=true|false: default is 'oneside' for backward compatibility ('twoside' is book's default).
	%	\hgb@@pagelayout is injected into \LoadClass[...\hgb@@pagelayout]{book} below.
	twoside.code			= {\ifthenelse{\equal{#1}{true}}%
								{\renewcommand{\hgb@@pagelayout}{twoside}}%
								{\renewcommand{\hgb@@pagelayout}{oneside}}},
	twoside.default:n		= true,
	oneside.code			= {\renewcommand{\hgb@@pagelayout}{oneside}%
								\@WarnDeprecatedOption{oneside}{twoside=false}},
	% hgb@SmartQuotesOn / hgb@UpdateCheck are declared in hgbvars.sty and read by hgb.sty later:
	smartquotes.code		= \setboolean{hgb@SmartQuotesOn}{#1},
	smartquotes.default:n	= true,
	updatecheck.code		= \setboolean{hgb@UpdateCheck}{#1},
	updatecheck.default:n	= true,
	noupdatecheck.code		= {\setboolean{hgb@UpdateCheck}{false}%
								\@WarnDeprecatedOption{noupdatecheck}{updatecheck=false}},
}

\DeclareUnknownKeyHandler[hgbthesis]{
    %\typeout{hgbthesis.cls: unknown option \CurrentOption}%
    \PassOptionsToPackage{\CurrentOption}{hgb}
}

\ProcessKeyOptions[hgbthesis]

%% Paper Options and Style File -----------------------------------------------

%\LoadClass[a4paper,11pt,oneside]{book}
\LoadClass[a4paper,11pt,\hgb@@pagelayout]{book}
\RequirePackage[top=36mm,bottom=40mm,left=32mm,right=32mm]{geometry} %showframe
\RequirePackage{hgb}
\renewcommand*{\@pnumwidth}{2em}	% width of page numbers in the TOC, enough for roman numbers like 'viii' (default 1.55em)
%\newcommand{\hgb@TitlePageFont}{\sffamily}
\renewcommand{\hgb@TitlePageFont}{\sffamily}

%% Setup for Pages in the Front Matter:

% General Thesis Parameters ----------------------------------------

\newcommand{\@warnmultipleauthors}%
	{\ClassWarning{hgbthesis}{Multiple authors are not supported ('\string\and' is ignored)!}}%

\newcommand{\subtitle}[1]{\renewcommand{\hgb@SubTitle}{#1}}

\newcommand{\institution}[1]{%
	\ifthenelse{\isempty{#1}}{}{\renewcommand{\hgb@Institution}{#1}}
}

\newcommand{\programtype}[1]{\renewcommand{\hgb@ProgramType}{#1}}

\newcommand{\programname}[1]{\renewcommand{\hgb@ProgramName}{#1}}

% Free text for the academic degree, replaces the degree selected by the class option 'degree':
\newcommand{\academicdegree}[1]{%
	\ifthenelse{\equal{\hgb@DegreeKey}{}}{}%
		{\ClassWarning{hgbthesis}{\string\academicdegree\space overrides the class option 'degree=\hgb@DegreeKey'}}%
	\renewcommand{\hgb@AcademicDegree}{#1}%
}

\newcommand{\placeofstudy}[1]{\renewcommand{\hgb@PlaceOfStudy}{#1}}

% Advisor(s) -------------------------------------------------------------------

\newcounter{@advisorctr}
\setcounter{@advisorctr}{0}

% User command to specify the role(optional) and name of one advisor: \advisor[<role>]{<name>}
% Repeat to specify multiple advisors.
\newcommand{\advisor}[2][\hgbDictionaryGet{advisorheader}{\hgb@TitleLanguage}]{% e.g., \advisor[Supervisor]{Prof.~Marie Curie, PhD}
	\stepcounter{@advisorctr}%
	\expandafter\def\csname advisorrole-\the@advisorctr\endcsname{#1}
	\expandafter\def\csname advisorname-\the@advisorctr\endcsname{#2}
}

% Returns the number of advisors specified 
\newcommand{\hgb@AdvisorCount}{%
	\the@advisorctr
}

% Returns the 'role' of advisor with the given number (#1 = 1,...,\hgb@AdvisorCount)
\newcommand{\hgb@getAdvisorRole}[1]{%
	\csname advisorrole-#1\endcsname
}

% Returns the 'name' of advisor with the given number (#1 = 1,...,\hgb@AdvisorCount)
\newcommand{\hgb@getAdvisorName}[1]{%
	\csname advisorname-#1\endcsname
}

% Legacy cmommand, returns the first advisor's name
\newcommand{\hgb@Advisor}{\hgb@getAdvisorName{1}}

% Submission Date ---------------------------------------------------------------

\DTMsavenow{hgb@SubmissionDate}  % default submission date is today

\newcommand{\dateofsubmission}[3]{%	usage: \dateofsubmission{yyyy}{mm}{dd}	- used anywhere??
	\DTMsavenoparsedate{hgb@SubmissionDate}{#1}{#2}{#3}{-1}
}

\newcommand{\hgb@SubmissionYear}{\DTMfetchyear{hgb@SubmissionDate}}
\newcommand{\hgb@SubmissionMonth}{\DTMfetchmonth{hgb@SubmissionDate}}
\newcommand{\hgb@SubmissionDay}{\DTMfetchday{hgb@SubmissionDate}}

\newcommand{\hgb@GetMonthName}[2]{% #1=language #2=monthNumber,
	\csname DTM#1monthname\endcsname{#2}% brittle?
}

% Copyright/License Settings --------------------------------------------------

\newcommand{\license}[1]{%
	\renewcommand{\hgb@License}{#1}
	\ifthenelse{\equal{#1}{cc}}{\renewcommand{\hgb@License}{cclicense}}{}%
	\ifthenelse{\equal{#1}{strict}}{\renewcommand{\hgb@License}{strictlicense}}{}%
}

\newcommand{\cclicense}{% for backward compatibility, use \license{cc} instead
	\ClassWarning{hgbthesis}{\string\cclicense\space command is deprecated, use \string\license{cc} instead!}%
	\license{cc}%
}

\newcommand{\strictlicense}{% for backward compatibility, use \license{strict} instead
	\ClassWarning{hgbthesis}{\string\strictlicense\space command is deprecated, use \string\license{strict} instead!}%
	\license{strict}%
}

% Legacy commands, keep for internship reports:
\newcommand{\companyName}[1]{\renewcommand{\hgb@CompanyName}{#1}}
\newcommand{\companyUrl}[1]{\renewcommand{\hgb@CompanyUrl}{#1}}


%% Logo -------------------------------------------------------------------------

% Specify the institution's logo - DEPRECATED - logo is now handled in theme files only!
\newcommand{\logofile}[1]{%
	\ClassWarning{hgbthesis}{Command \string\logofile\space is deprecated, logo should be specified in theme file!}%
	%\renewcommand{\hgb@LogoFile}{#1}
}

%% Create title pages -------------------------------------

% Generate the front pages (to be redefined by theme .sty files)
\newcommand{\hgb@MakeFrontPages}{%
	\ClassError{hgbthesis}{Command \string\hgb@MakeFrontPages\space is not defined!}\relax%
}

% Redefine LaTeX's \maketitle
\renewcommand{\maketitle}{% redefine standard \maketitle command
	\SetLanguage{\hgb@TitleLanguage}
	\hgb@MakeFrontPages%						generate front pages (using the specified theme)
	\SetLanguage{\hgb@MainLanguage}
	% proposals have no affidavit, so the short form of the documentation of AI tools usage gets a separate
	% chapter (hgbaiusage):
	\ifthenelse{\boolean{hgb@IsProposal}}{\hgb@MakeAIUsageChapter}{}%
	\hypersetup{pageanchor=true}%		re-enable hyperref page anchors in the main document (only)
}

% Define new hook to be used in theme style files for various initializations: 
% Usage: \AddToHook{hgb@InitThemeHook}{...}
% Invoked before \maketitle (by hook code below).
\NewHook{hgb@InitThemeHook}

%% Do before running \maketitle:
\AddToHook{cmd/maketitle/before}{%
	% initialize remaining parameters:
	\renewcommand{\hgb@Author}{\@author}%
	\renewcommand{\hgb@Title}{\@title}%
	\ifthenelse{\equal{\hgb@SubTitle}{}}%
		{\renewcommand{\hgb@FullTitle}{\hgb@Title}}%
		{\renewcommand{\hgb@FullTitle}{\hgb@Title: \hgb@SubTitle}}%
	%
	% now trigger the generation of title pages (using the specified theme):
	\UseHook{hgb@InitThemeHook}%	execute collected code for hgb@InitThemeHook (if any)
	\hgb@SetDocumentMetadata%
}

% Special sectioning setup for thesis proposals: ----------------------------
% wilbur: should be simplified and moved elsewhere (TODO)
%
% Provides a special chapter/section configuration for thesis proposals:
% The idea is to use the regular 'hgbthesis' class but to have only a single 
% (unnumbered) chapter with a couple of sections. All float elements and equations
% are numbered without the chapter count:
%
\newcommand{\hgb@ProposalSetup}[0]{%
	% remove "Kapitel X"/"Chapter X" from chapter titles:
	\titleformat{\chapter}[display]{\sffamily}{}{0pt}{\Huge}% from 'titlesec' package loaded by 'hgbheadings'
	% remove chapter number from header:
	\renewcommand{\chaptermark}[1]{\markboth{##1}{}}% wilbur: from 'fancyhdr' package loaded by 'hgbheadings'
	% define an empty entry format for the TOC chapter numbering (tocbasic)
	\newcommand\noentrynumber[1]{}
	% set empty number and remove indentation for chapter numbering in TOC (tocbasic)
	\DeclareTOCStyleEntry[entrynumberformat=\noentrynumber,numwidth=0pt]{tocline}{chapter}
	 % remove chapter number from sections and figures (basic LaTeX):
	\counterwithout{section}{chapter}
	\counterwithout{figure}{chapter}
	\counterwithout{table}{chapter}
	\counterwithout{equation}{chapter}
	\counterwithout{program}{chapter}
	\counterwithout{algorithm}{chapter}
}

\AtBeginDocument{%
	\hypersetup{pageanchor=false}	% to avoid duplicate page identifiers in front matter
	\ifthenelse{\boolean{hgb@IsProposal}}{\hgb@ProposalSetup}{}%
}

%% Set various terms to be redefined by title page themes

\ifthenelse{\equal{\hgb@TitleLanguage}{}}%	duplicate in 'hgb.sty'!
		{\renewcommand{\hgb@TitleLanguage}{\hgb@MainLanguage}}{}%

\renewcommand{\hgb@ThesisName}{\hgbDictionaryGet{\hgb@ThesisType-docname}{\hgb@TitleLanguage}}%
\renewcommand{\hgb@ProposalName}{\hgbDictionaryGet{proposal-name}{\hgb@TitleLanguage}}%
\renewcommand{\hgb@ProgramType}{\hgbDictionaryGet{\hgb@ThesisType-programtype}{\hgb@TitleLanguage}}% default, see \programtype


%% Additional Hagenberg packages ----------------------------------------------

\RequirePackage{hgbmath}
\RequirePackage{hgbalgo}
\RequirePackage{hgbheadings}
\RequirePackage{hgbabbrev}
\RequirePackage{hgblistings}
\RequirePackage{hgbbib}
\RequirePackage{hgbdict}
\RequirePackage{hgbaiusage}	% documentation of AI tool usage (environments 'aiusage', 'aiusagedetails', option 'aiusage')

\hgb@UseTheme{\@ThemeID}%		load theme style file

%% Academic degree from class option 'degree' ---------------------------------
%% Resolved after loading the theme, which may define additional degrees (\hgbDefineDegree).
%% The level (bachelor/master) is taken from the class option 'type'; internship reports show no degree.

\ifthenelse{\equal{\hgb@DegreeKey}{} \OR \equal{\hgb@ThesisType}{internship}}%
	{}%
	{%
		\ifcsname hgb@dict-degree-\hgb@ThesisType-\hgb@DegreeKey-english\endcsname
			\renewcommand{\hgb@AcademicDegree}{\hgbDictionaryGet{degree-\hgb@ThesisType-\hgb@DegreeKey}{\hgb@TitleLanguage}}%
		\else
			\ifcsname hgb@dict-degree-bachelor-\hgb@DegreeKey-english\endcsname
				\ClassError{hgbthesis}{academic degree 'degree=\hgb@DegreeKey' is not available for 'type=\hgb@ThesisType'}%
					{Use a degree that exists for this type or specify it as free text with \string\academicdegree{...}.}%
			\else\ifcsname hgb@dict-degree-master-\hgb@DegreeKey-english\endcsname
				\ClassError{hgbthesis}{academic degree 'degree=\hgb@DegreeKey' is not available for 'type=\hgb@ThesisType'}%
					{Use a degree that exists for this type or specify it as free text with \string\academicdegree{...}.}%
			\else
				\ClassError{hgbthesis}{unknown academic degree 'degree=\hgb@DegreeKey'}%
					{See the package manual for the supported values or specify the degree as free text with \string\academicdegree{...}.}%
			\fi\fi
		\fi
	}%

\endinput
