\ProvidesClass{LFD}
\LoadClass{book}

\newcommand{\inmargin}{0.75in}

\ifdefined\JAPANESE
   \renewcommand{\inmargin}{0.5in}
\fi

\newcommand{\dimensionsclass}{
   \usepackage[paperwidth=8.5in, paperheight=11.0in,
      tmargin=0.75in, bmargin=0.625in,
      outer=0.5in, inner=\inmargin] {geometry}
}
\newcommand{\dimensionsslides}{
   \usepackage[paperwidth=8.5in, paperheight=6.375in,
      tmargin=0.50in, bmargin=0.500in,
      outer=0.5in, inner=\inmargin] {geometry}
   %% keep outer inner the same as book to keep linebreaks consistent
   %%  outer=0.5in, inner=0.500in] {geometry}
}

%%% This will let lines look sloppier in spacing to avoid excessive
%%% manual insertion of \linebreak (2020-06-18)
\tolerance=2000


%%This magic is supposed to center all floats (figures, tables, etc.)
\makeatletter
\g@addto@macro\@floatboxreset\centering
\makeatother

\newcommand{\lflogo}{
   \begin{flushright}
      \includegraphics[scale=0.8]{linuxfoundation-cropped.png}
   \end{flushright}
}
%%%%%%%%%% Change the date format to 08/12/2014
%%% seems to be needed for the clearpage macros for a mysterious reason :)
\usepackage{datetime}
%%\renewcommand{\dateseparator}{\_}
\mmddyyyydate

\newcommand{\checkcourse}[2]
{
   \ifthenelse{\equal{#1}{\course}}{#2}{}
}
\newcommand{\ifcheckcourse}[3]
{
   \ifthenelse{\equal{#1}{\course}}{#2}{#3}
}

\newcommand{\checkclearpage}[1]
{
   \ifthenelse{\equal{#1}{\course}}{\clearpage}{}
}

\newcommand{\clearthepage}[1]{\checkclearpage{#1}}
\newcommand{\SOURCEFILE}[1]{}

%% slides or book dimensions

\ifdefined\slideoutput
%%slides
\dimensionsslides
\renewcommand{\checkclearpage}[1]
{\ifthenelse{\equal{#1}{FUCKSHIT}}{\clearpage}{}}
\newcommand{\clearpageslides}{\clearpage}
\let\oldfigbegin\figure
\let\oldfigend\endfigure
\renewenvironment{figure}{\oldfigbegin}{\oldfigend\clearpage}
\newcommand{\coverfigure}{\course-COVER-SLIDE.pdf}
\else
%%book
\dimensionsclass
\newcommand{\clearpageslides}{}
\newcommand{\coverfigure}{\course-COVER-FRONT.pdf}
\fi

\RequirePackage[user,abspage]{zref}

\usepackage[utf8x]{inputenc}
\usepackage{layout}
\usepackage{graphics}
\usepackage[\ENGINE]{color,graphicx}
%%% For use with xetex in Japanese:
\ifthenelse{\equal{\ENGINE}{xetex}}{
   \usepackage{chappg}
   \usepackage{xeCJK}
   \setCJKmainfont{IPAPMincho}
   \setCJKsansfont{IPAPGothic}
   \setCJKmonofont{IPAGothic}
}{}

\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{verbatim}
\usepackage[textfont=bf,tableposition=top,figureposition=top]{caption}
\usepackage{wrapfig}
\usepackage{xcolor}
\usepackage{fitbox}
\usepackage{varwidth}
\RequirePackage{currfile}
\usepackage{morewrites}
\usepackage[breakable,external,fitting,hooks,magazine,poster,raster,skins,theorems,vignette,xparse,listingsutf8,minted]{tcolorbox}
\usepackage{longtable}
\usepackage{makeidx}
\usepackage{tabularx}
\usepackage{colortbl}
\usepackage{fancyvrb}
\usepackage{setspace}
\usepackage{supertabular}
\usepackage{import}
\usepackage{alltt}
\usepackage{multicol}
\usepackage{cprotect}
\usepackage{smartdiagram}
%%\usepackage[T1]{fontenc}
\usepackage{lmodern} % Fixes encoding of $ in minted listings
\usepackage{placeins} % for FloatBarrier

%%to produce shaded background boxes
\usepackage{framed,color}
%% lets nested lists use a b c d e 
\usepackage{enumitem}
%% better float control in subsections, prbably not needed
\usepackage{placeins}
%% 0.95 for rather light gray
\definecolor{lgray}{gray}{0.95}
%% 0.97 for a medium gray
\definecolor{mgray}{gray}{0.85}

\newcommand{\allttcolor}{black}
\newcommand{\RawColor}{black}
\definecolor{LFGreen}{RGB}{0, 100, 0}
\definecolor{LFPurple}{RGB}{100, 0, 50}

% get rid of alltt bottom space
\preto{\alltt}{\topsep=0pt \partopsep=0pt}

%% Change alltt to be smaller font and change text color
\let\oldallttbegin\alltt
\let\oldallttend\endalltt

\renewenvironment{alltt}{\oldallttbegin\small\color{\allttcolor}}{\oldallttend}

%%%%%%% avoid subsection numbers of 10 fucking things up
\usepackage[titles]{tocloft}
\setlength{\cftsecnumwidth}{3em}
\setlength{\cftfignumwidth}{3em}
\setlength{\cfttabnumwidth}{3em}

%%% get chapters to print out as two digits when desired:
%%% \thesessionref will do this!

\newcounter{sessionref}
\makeatletter
\renewcommand\thesessionref{\two@digits{\value{chapter}}}
\makeatother

\usepackage{minitoc}
\nomtcrule

\usepackage{appendix}

\parindent 0pt
\parskip 7.2pt
\raggedbottom

\captionsetup[table]{position=top}
\captionsetup[figure]{position=top}

%% Various raw environments of different size

\DefineVerbatimEnvironment%
{rawX}{Verbatim}
{fontsize=\small\color{blue}
}

%% For custom kernel quoting boxes
\DefineVerbatimEnvironment%
{kraw}{Verbatim}
{
   fontsize=\small\color{Sepia},
   frame=single,framerule=1pt,rulecolor=\color{Cyan},
   %%    frame=single,framerule=1pt,rulecolor=\color{OrangeRed}
}

\DefineVerbatimEnvironment%
{kcodesmall}{Verbatim}
{
   fontsize=\small\color{Sepia}
}


\DefineVerbatimEnvironment%
{kcode_OLD}{Verbatim}
{fontsize=\small\color{Sepia}}

\DefineVerbatimEnvironment%
{raw}{Verbatim}
{fontsize=\small\color{\RawColor}}

\DefineVerbatimEnvironment%
{rawsmall}{Verbatim}
{fontsize=\small\color{\RawColor}}

\DefineVerbatimEnvironment%
{rawhuge}{Verbatim}
{fontsize=\huge\color{\RawColor}}

\DefineVerbatimEnvironment%
{rawfootnotesize}{Verbatim}
{fontsize=\footnotesize\color{\RawColor}}

\DefineVerbatimEnvironment%
{rawscriptsize}{Verbatim}
{fontsize=\scriptsize\color{\RawColor}}

\DefineVerbatimEnvironment%
{rawtiny}{Verbatim}
{fontsize=\tiny\color{\RawColor}}

\RecustomVerbatimCommand{\VerbatimInput}{VerbatimInput}%
                        {fontsize=\small}

%%% Redefined to put a box around things


\newcommand{\rawfile}[1]
{\begingroup\small\color{\RawColor}{\verbatiminput{#1}}\endgroup}
\newcommand{\rawfilesmall}[1]
{\begingroup\small\color{\RawColor}{\verbatiminput{#1}}\endgroup}
\newcommand{\rawfiletiny}[1]
{\begingroup\tiny\color{\RawColor}{\verbatiminput{#1}}\endgroup}
\newcommand{\rawfilescriptsize}[1]
{\begingroup\scriptsize\color{\RawColor}{\verbatiminput{#1}}\endgroup}
\newcommand{\rawfilefootnotesize}[1]
           {\begingroup\footnotesize\color{\RawColor}{\verbatiminput{#1}}\endgroup}

\newcommand{\rawcmdfile}[1]
{\begingroup\small\color{blue} {\verbatiminput{#1}}\endgroup}

%% redefined to take a border
\newcommand{\rawoutfile}[1]{\small
  \begin{tcolorbox}
    \color{LFPurple}{
      \verbatiminput{#1}
    }
  \end{tcolorbox}
}

%%%% These are redefined temporarily in the frame environment for LFS

\newcommand{\respfileKEEP}[1]{\begingroup\small\color{LFPurple} {\verbatiminput{#1}}\endgroup}

\newcommand{\respfile}[1]{\begingroup\small
  \begin{tcolorbox}
    \color{LFPurple} {\verbatiminput{#1}}
  \end{tcolorbox}
  \endgroup}


\newcommand{\respfilevar}[2]{\begingroup
  \begin{tcolorbox}
    \color{LFPurple}
  {#2
      \verbatiminput{#1}}
    \end{tcolorbox}
  \endgroup

}

\newcommand{\respfilevarKEEP}[2]{\begingroup\color{LFPurple}
   {#2 \verbatiminput{#1}}\endgroup}


\newcommand{\OLDrawoutfile}[1]
{\begingroup\small\color{LFPurple} {\verbatiminput{#1}}\endgroup}

%% increase the box border width, affects all boxes
\setlength{\fboxrule}{1.5pt}

%%to produce shaded background boxes

\newenvironment{OLDlfbox}[1][black]
{\def\FrameCommand{\fboxsep=\FrameSep\fcolorbox{#1}{lgray}}%
   \MakeFramed {\advance\hsize-2.5\leftmargin \FrameRestore}}
{\endMakeFramed}

\newenvironment{lfbox}[1][black]
{\def\FrameCommand{\fboxsep=\FrameSep\fcolorbox{#1}{lgray}}%
   \MakeFramed {\advance\hsize-2.5\leftmargin \FrameRestore}}
{\endMakeFramed}

%%% 20150319 add lfslide environment to add slides at the start 
%%%          of a chapter

\ifdefined\slideoutput
   %%slides
   \newsavebox{\lfslidebox}
   \newenvironment{lfslide}[1]
   {\begin{lrbox}{\lfslidebox}%
         \begin{minipage}[c][4.6in][t]{7.2in}%
            \begin{Large}
               \colorbox{gray}{\parbox[c][8ex][c]{\dimexpr\linewidth-2\fboxsep}{\textcolor{white}{\hspace{10pt}\textbf{#1}}}}\parindent0pt
               }%
               {\end{Large}\end{minipage}\end{lrbox}%
      \fboxsep0mm\fcolorbox{black}{lgray}{\usebox{\lfslidebox}}%
   }

\else
   %%book
   \newsavebox{\lfslidebox}
   \newenvironment{lfslide}[1]
   {\begin{lrbox}{\lfslidebox}%
         \begin{minipage}[c][4.5in][t]{7.2in}%
            \begin{Large}
               \colorbox{mgray}{\parbox[c][8ex][c]{\dimexpr\linewidth-2\fboxsep}{\textcolor{black}{\hspace{10pt}\textbf{#1}}}}\parindent0pt
               }%
               {\end{Large}\end{minipage}\end{lrbox}%
      \fboxsep0mm\fcolorbox{black}{white}{\usebox{\lfslidebox}}%
   }
\fi

%% kernel version boxes  TODO may have to adjust width as in lfbox

\newenvironment{kvnote}[1][black]
{\def\FrameCommand{\fboxsep=\FrameSep\fcolorbox{#1}{lgray}}%
   \MakeFramed {\advance\hsize-\width \FrameRestore}
   \centering{\includegraphics[width=0.55\textwidth]{IMAGES/pen.png}}
}
{\endMakeFramed}

\input{lf-boxes}
%% Watermarking

%%\usepackage[nostamp]{draftwatermark} for no watermark
%%\usepackage{draftwatermark} use watermark
\ifdefined\watermark
%%\usepackage[hpos=0.5\textwidth]{draftwatermark}
\usepackage{draftwatermark}
   \SetWatermarkFontSize{12pt}
   \SetWatermarkText{
     \scriptsize{\color{blue}{\copyright\ The Linux Foundation 2021:}}
     DO NOT COPY OR DISTRIBUTE}
   \SetWatermarkScale{1.5}
   \SetWatermarkAngle{0}
   \SetWatermarkLightness{0.0}
   \SetWatermarkVerCenter{0.2in}
   \SetWatermarkColor{red}
\fi

\ifdefined\wmappendix
   \usepackage{draftwatermark}
   \SetWatermarkText{\includegraphics{common/IMAGES/wmbackground.jpg}}
   \SetWatermarkAngle{0}
\fi

%%%%%%%%%%%%%%%% LANGUAGE SPECIFIC DEFS START
\newcommand{\lfcopyright}{
   \textcopyright\  Copyright the Linux Foundation 2021.  All rights reserved.}
\newcommand{\inputcopyright}{\input{longcopyright}}
\newcommand{\titver}{ Version \version}
\newcommand{\solutionname}{Solution}
\newcommand{\exercisename}{Exercise}

\newcommand{\optionalfootnotetext}{
   \begin{info}
      \textbf{**} These sections may be considered in part or
      in whole as optional.  They contain either background reference
      material, specialized topics, or advanced subjects. The
      instructor may choose to cover or not cover them depending on
      classroom experience and time constraints.
   \end{info}
}


\ifdefined\SPANISH
   \renewcommand{\lfcopyright}{
      \textcopyright\  Copyright Linux Foundation 2021.  Todos los derechos reservados.}
   \renewcommand{\inputcopyright}{\input{longcopyrightspanish}}
   \renewcommand{\contentsname}{Contenidos}
   \renewcommand{\chaptername}{Cap\'itulo}
   \renewcommand{\figurename}{Figura}
   \renewcommand{\titver}{ Versión \version}
   \renewcommand{\exercisename}{Ejercicio}
   \renewcommand{\solutionname}{Solución}
   \renewcommand{\optionalfootnotetext}{
      \begin{info}
         \textbf{**} Estas secciones pueden considerarse en parte o totalmente opcionales. Contienen ya sea referencias del material, tópicos especializados o avanzados. El instructor puede optar por cubrirlas o no dependiendo de la experiencia de la clase y las limitantes de tiempo.
      \end{info}
   }
\fi


\ifdefined\JAPANESE
   \renewcommand{\lfcopyright}{
      \textcopyright\  Copyright The Linux Foundation 2021.  All rights reserved.(無断転載・無断複製禁止)}
   \renewcommand{\exercisename}{課題}
   \renewcommand{\inputcopyright}{\input{longcopyrightjapanese}}
   \renewcommand{\contentsname}{目次}
   \renewcommand{\chaptername}{章}
   \renewcommand{\figurename}{図}
   %%\renewcommand{\titver}{ Versión \version}
   \renewcommand{\solutionname}{解}
   %%\renewcommand{\optionalfootnotetext}{
   %%\begin{info}
   %%  \textbf{**} Estas secciones pueden considerarse en parte o totalmente opcionaales. Contienen ya sea referencias del material, tópicos especializados o avaados. El instructor puede optar por cubrirlas o no dependiendo de la experiencia de la clase y las limitantes de tiempo.
   %%\end{info}
   %%}
\fi


%%%%%%%%%%%%%%%% LANGUAGE SPECIFIC DEFS END

%%%%%%%%%%% header and footer crap
%%%%%%   This must come after the pagesize dimensions are set

\usepackage{fancyhdr}
\pagestyle{fancy}
\renewcommand{\footrulewidth}{0.6pt}
\fancyhf{}
\fancyhead[RE]{\textit{\leftmark}}
\fancyhead[LO]{\textit{\rightmark}}
\fancyhead[LE]{\thepage}
\fancyhead[RO]{\thepage}
\ifdefined\JAPANESE
   \fancyfoot[C]{\footnotesize \textbf{\lfcopyright}}
\else
   \fancyfoot[C]{\textbf{\lfcopyright}}
\fi
\fancyfoot[R]{\includegraphics[scale=0.25]{linuxfoundation-cropped}
}
\fancyfoot[L]{\textbf{ V\_\version}}

% this disables the boxes around links
\usepackage[breaklinks,\ENGINE, pdfborderstyle={/S/U/W 0} ]{hyperref}
%%\usepackage[breaklinks, pdftex, pdfborderstyle={/S/U/W 0} ]{hyperref}
%% set link colors, url=http links, file=localfiles, link=toc etc.
\hypersetup{colorlinks=true,urlcolor=RawSienna,filecolor=PineGreen,
   linkcolor=Emerald}
%%linkcolor=Red}

%% Do not use monspace file for urls, just a normal font!
\urlstyle{same}

%%\newcommand{\filelink}[1]{\href{run:///#1}{\texttt{#1}}}
\newcommand{\filelink}[1]{\href{run:///#1}{\texttt{\nolinkurl{#1}}}}
%%\newcommand{\kernlink}[1]{\href{run:////usr/src/linux/#1}{\texttt{#1}}}
\newcommand{\kernlink}[1]{\href{run:////usr/src/linux/#1}{\texttt{\nolinkurl{#1}}}}
\newcommand{\lablink}[1]{\href{run:./SOLUTIONS/s_\thesessionref/#1}{\texttt{#1}}}
\newcommand{\labref}[1]{
   \begin{alltt}\href{run:./SOLUTIONS/s_\thesessionref/#1}{\texttt{#1}}\end{alltt}
}
\newcommand{\examplink}[1]{\href{run:./SOLUTIONS/s_\thesessionref/EXAMPLES/#1}{\texttt{#1}}}
%% refer to RESOURCES
\newcommand{\reslink}[1]{\href{run:./RESOURCES/s_\thesessionref/#1}{\texttt{#1}}}
\newcommand{\resref}[1]{
   \begin{alltt}\href{run:./RESOURCES/s_\thesessionref/#1}{\texttt{#1}}\end{alltt}
}
%% just to refer to a file that you won't click on
\newcommand{\file}[1]{{\color{Violet}\texttt{#1}}}

\newcommand{\topshit}{
   \frontmatter
   \begin{titlepage}
      \begin{center}
         \includegraphics[scale=0.76]{\coverfigure}
      \end{center}
      \textbf{
         \titver
      }
      \par
      \textbf{\lfcopyright}
      \par

      \pagebreak
      \inputcopyright

   \end{titlepage}
   \tablestuff
   \mainmatter
}

\newcommand{\includefootnote}{
   \vspace{24pt}
   \optionalfootnotetext
}

%%%% defaults with all on
\newcommand{\includelistoffigures}{
   \listoffigures
}
\newcommand{\includelistoftables}{
   \listoftables
}

\newcommand{\tablestuff}
{
   \setcounter{tocdepth}{1}
   \dominitoc[n]
%%   \begin{spacing}{1.4}
      \tableofcontents
      \includefootnote   %%% defined by us
      \includelistoffigures   %%% defined by us
      \includelistoftables   %%% defined by us
%%   \end{spacing}
}

\makeindex


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% For conversion of LFS documents to LFD format
\usepackage[scaled]{helvet}
\usepackage{float}
\renewcommand{\familydefault}{\sfdefault}


\newcounter{labno}
%%\newcommand{\fulllabno}{\large\thechapter.\arabic{labno}}
\newcommand{\fulllabno}{\Large\thechapter.\thelabno}

\newenvironment{Lab}{
   \setcounter{labno}{0}
}{}

\newtoggle{split}
\togglefalse{split}
\DeclareOption{splitlabs}{\toggletrue{split}}

\ProcessOptions
\ExecuteOptions{splitlabs}

%% separate lab page number stuff
\iftoggle{split}{
   \newwrite\myfile
   \immediate\openout\myfile=labcontents.data
}{}

\iftoggle{split}
{
   \newenvironment{exe}[1]{
     \clearpage
      \setcounter{page}{1}
      \stepcounter{labno}
      {
         \lflogo
         \Large{\textbf{\exercisename\ \fulllabno :  #1}}}
      %% Cannot use \large in immeidat\write so use full macro def
      %    \immediate\write\myfile{\fulllabno\space\theabspage}
      \immediate\write\myfile{\thechapter.\thelabno\space\theabspage}
   }{
      \immediate\write\myfile{\theabspage}
   }
}{
   \newenvironment{exe}[1]{
      \stepcounter{labno}
      \includegraphics[width=4mm]{common/IMAGES/icon-practice}
      {\Large{\textbf{\exercisename\ \fulllabno :  #1}}}
   } {}
}
\newenvironment{sol}{
\iftoggle{split} {  
  \pagebreak
} {}
\includegraphics[width=4mm]{common/IMAGES/icon-checkmark}
   {\Large{\textbf{\solutionname\ \fulllabno} } } }
{}

\ifdefined\slideoutput
   \newcommand{\note}[1]
   {\pagebreak}
\else
   \newcommand{\note}[1]{
      \begin{minipage}[t]{\textwidth}
         {\parskip4pt
            {#1}
         }
      \end{minipage}
      \vfill

   }
   \fi

\newcommand{\verbsize}{\small}
\newcommand{\negresponsespace}{-10pt}

\newenvironment{kcode}    {\color{Sepia}   \verbsize \VerbatimEnvironment
   \begin{Verbatim}}  {\end{Verbatim}}

\newenvironment{cmd} {\verbsize \VerbatimEnvironment
    \begin{Verbatim}[formatcom=\color{blue}]}
                   {\end{Verbatim}}

%%% This puts a box around {cmd}, not used for now, too much space wasted
\newenvironment{cmdline} {\verbsize \VerbatimEnvironment
  \begin{Verbatim}[formatcom=\color{blue},
      frame=single,
      framerule=0.4pt]}
               {\end{Verbatim}}
               
\newenvironment{responsenoline} {\color{LFPurple} \verbsize \VerbatimEnvironment
  \vspace{\negresponsespace}
  \begin{Verbatim}}
               {\end{Verbatim}}


\newenvironment{response} {\verbsize \VerbatimEnvironment
  \vspace{\negresponsespace}
  \begin{Verbatim}[formatcom=\color{LFPurple},
      frame=single,
      framerule=0.4pt,
      numbers=left,
      numbersep=4pt]}
               {\end{Verbatim}}

\newenvironment{responsevar}[1] {\color{LFPurple} \verbsize \VerbatimEnvironment
   \vspace{\negresponsespace} #1
  \begin{Verbatim}[formatcom=\color{LFPurple},
      frame=single,
      framerule=0.4pt,
      numbers=left,
      numbersep=4pt]}
               {\end{Verbatim}}

 \newenvironment{responsevarnoline}[1] {\color{LFPurple} \verbsize \VerbatimEnvironment
   \vspace{\negresponsespace} #1
  \begin{Verbatim}}
               {\end{Verbatim}}              

\newsavebox{\coopslidebox}

\usepackage{afterpage}
\renewenvironment{frame}[1]{

  \ifdefined\slideoutput
  \else
      \afterpage{\clearpage}
  \fi

%%  \renewenvironment{cmd} {\verbsize \VerbatimEnvironment
%%    \begin{Verbatim}[formatcom=\color{blue}]}
%%                   {\end{Verbatim}}

  \renewenvironment{response} {\verbsize \VerbatimEnvironment
    \vspace{\negresponsespace}
    \begin{Verbatim}[formatcom=\color{LFPurple}]}
      {\end{Verbatim}}                 
  
   \begin{lrbox}{\coopslidebox}
   \ifdefined\slideoutput
   \begin{minipage}[c][4.6in][t]{6.9in}
   \else
   \begin{minipage}[c][4.7in][t]{6.9in}
      \fi
      \begin{Large}
         \colorbox{NavyBlue}{
            \parbox[c][8ex][c]{\dimexpr\linewidth-3\fboxsep}{
               \textcolor{white}{
                  \hspace{10pt}
                  \begin{center}
                     \begin{Huge}
                        %% \includegraphics[scale=0.25]{linuxfoundation-cropped}
                        \textbf{#1}
                     \end{Huge}
                  \end{center}
               }
            }
         }
         \parindent0pt
         \vfill
         \RecustomVerbatimEnvironment
         {raw}{Verbatim}{fontsize=\large\color{\RawColor}}
         \renewcommand{\verbsize}{\large}
         \renewcommand{\negresponsespace}{0pt}
         \begin{quote}
            }{
         \end{quote}
         \renewcommand{\verbsize}{\small}
         \renewcommand{\negresponsespace}{-10pt}
      \end{Large}
      \vfill
      \hfill
   \end{minipage}
   \end{lrbox}
   \fboxsep3mm
   \fcolorbox{black}{white}
   {\usebox{\coopslidebox}
   }
}

\newif\ifEMBEDDED
\EMBEDDEDfalse
