\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{songbook-style}[2026/04/01 Pfadfinder Liederbuch Style]

% --- Core packages ---
\RequirePackage{fontspec}
\RequirePackage[ngerman]{babel}
\RequirePackage[
  a5paper,
  top=15mm,
  bottom=20mm,
  inner=20mm,
  outer=12mm
]{geometry}
\RequirePackage[hidelinks]{hyperref}
\RequirePackage{fancyhdr}
\RequirePackage{xcolor}
\RequirePackage[minimal]{leadsheets}
% Pre-mark musicsymbols as loaded to avoid missing musix11 font error
\ExplSyntaxOn
\cs_new:cpn {leadsheets-library-musicsymbols-loaded} {}
\ExplSyntaxOff
\useleadsheetslibraries{chordnames,chords,shorthands,properties,templates,translations,songs}

% --- Font setup ---
\setmainfont{TeX Gyre Heros}
\newfontfamily\frakfont{UnifrakturMaguntia-Book}[Path=fonts/,Extension=.ttf]

% --- Page style: page numbers at bottom-outer, large ---
\pagestyle{fancy}
\fancyhf{}
\fancyfoot[LE]{\large\bfseries\thepage}
\fancyfoot[RO]{\large\bfseries\thepage}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}

% --- Custom song properties ---
\definesongproperty{alias}
\definesongproperty{note}
\definesongproperty{mundorgel}
\definesongproperty{pfadfinderliederbuch}

% --- leadsheets settings ---
\setleadsheets{
  title-template = songbook,
  verse/numbered = false,
  verse/named = false,
  chorus/named = false,
  chorus/numbered = false,
  after-song = \songendsection,
}

% --- Chord appearance: regular weight, black (matching reference style) ---
\setchords{
  format = \small,
}

% --- Command to render metadata at bottom of song ---
\newcommand{\songendsection}{%
  \vfill
  \ifsongproperty{note}{%
    {\footnotesize\songproperty{note}\par\smallskip}%
  }{}%
  \begingroup\footnotesize
    \ifsongproperty{lyrics}{%
      \ifsongproperty{composer}{%
        Worte: \songproperty{lyrics}\par
        Weise: \songproperty{composer}\par
      }{%
        Worte und Weise: \songproperty{lyrics}\par
      }%
    }{%
      \ifsongproperty{composer}{%
        Weise: \songproperty{composer}\par
      }{}%
    }%
  \endgroup
  % Reference book footer
  \vspace{3mm}%
  \begingroup\footnotesize
    \noindent
    \makebox[0.18\textwidth][c]{MO}%
    \makebox[0.18\textwidth][c]{PfLB}%
    \hfill\par\noindent
    \makebox[0.18\textwidth][c]{%
      \ifsongproperty{mundorgel}{\songproperty{mundorgel}}{}%
    }%
    \makebox[0.18\textwidth][c]{%
      \ifsongproperty{pfadfinderliederbuch}{\songproperty{pfadfinderliederbuch}}{}%
    }%
    \hfill
  \endgroup
  \newpage
}

% --- Song title template: Fraktur title, metadata at bottom ---
\definesongtitletemplate{songbook}{%
  {\LARGE\frakfont\songproperty{title}\par}%
  \addcontentsline{toc}{section}{\songproperty{title}}%
  \vspace{4mm}%
}
