\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{longtable}
\RequirePackage{array}
\RequirePackage{colortbl}
\RequirePackage{rotating}
\RequirePackage[minimal]{leadsheets}
\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]

% --- Colors ---
\definecolor{tocrowgray}{gray}{0.92}
\definecolor{tocheadgray}{gray}{0.75}

% --- Page style ---
\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,
  disable-measuring,
}

\setchords{
  format = \small,
}

% ==========================================================================
% Song TOC matrix
% ==========================================================================

\newcounter{songnumber}
\newcounter{tocrowcount}

\ExplSyntaxOn
\iow_new:N \g__sb_toc_iow
\tl_new:N \l__sb_title_tl
\tl_new:N \l__sb_mo_tl
\tl_new:N \l__sb_pflb_tl
\tl_new:N \l__sb_num_tl
\bool_new:N \g__sb_toc_opened_bool

% Lazy-open: only truncate the file when first song writes to it
% This ensures the TOC reads the PREVIOUS run's data before truncation
\cs_new_protected:Npn \__sb_ensure_toc_open:
{
  \bool_if:NF \g__sb_toc_opened_bool
  {
    \iow_open:Nn \g__sb_toc_iow { \c_sys_jobname_str .songtoc }
    \bool_gset_true:N \g__sb_toc_opened_bool
  }
}

\AtEndDocument{
  \bool_if:NT \g__sb_toc_opened_bool
  { \iow_close:N \g__sb_toc_iow }
}

\cs_new_protected:Npn \writesongtoc
{
  \__sb_ensure_toc_open:
  \stepcounter{songnumber}
  \tl_set:Nx \l__sb_num_tl { \int_use:N \c@songnumber }
  \label{song:\tl_use:N \l__sb_num_tl}
  \tl_set:Nx \l__sb_title_tl { \songproperty{title} }
  \tl_set:Nx \l__sb_mo_tl { \songproperty{mundorgel} }
  \tl_set:Nx \l__sb_pflb_tl { \songproperty{pfadfinderliederbuch} }
  \iow_now:Nx \g__sb_toc_iow
  {
    \exp_not:N \songtocrow
      { \l__sb_title_tl }
      { \l__sb_mo_tl }
      { \l__sb_pflb_tl }
      { \exp_not:N \pageref { song: \l__sb_num_tl } }
  }
}
\ExplSyntaxOff

% --- Render one TOC row ---
\newcommand{\songtocrow}[4]{%
  #1 & #2 & #3 & \cellcolor{tocheadgray}\textbf{#4} \\
  \hline
}

% --- Rotated column header ---
\newcommand{\rotheader}[1]{%
  \begin{turn}{70}\footnotesize\textbf{#1}\end{turn}%
}

% --- Print the song TOC table ---
\newcommand{\printsongtoc}{%
  \thispagestyle{fancy}%
  {\Large\bfseries Inhaltsverzeichnis\par}%
  \vspace{5mm}%
  \footnotesize
  \rowcolors{2}{tocrowgray}{white}%
  \begin{longtable}{%
    >{\raggedright\arraybackslash}p{0.52\textwidth}|%
    >{\centering\arraybackslash}p{0.10\textwidth}|%
    >{\centering\arraybackslash}p{0.10\textwidth}|%
    >{\centering\arraybackslash\columncolor{tocheadgray}}p{0.12\textwidth}%
  }
  & \rotheader{MO} & \rotheader{PfLB}
  & \rotheader{\normalsize Lieder-\newline\normalsize buch} \\
  \hline
  \endfirsthead
  & \rotheader{MO} & \rotheader{PfLB}
  & \rotheader{\normalsize Lieder-\newline\normalsize buch} \\
  \hline
  \endhead
  \InputIfFileExists{\jobname.songtoc}{}{}%
  \end{longtable}%
}

% ==========================================================================
% Song end section
% ==========================================================================

\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
  \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
% ==========================================================================

\definesongtitletemplate{songbook}{%
  {\LARGE\frakfont\songproperty{title}\par}%
  \writesongtoc
  \vspace{4mm}%
}
