\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{simurgh-footnotes}[2013/09/18 v0.01a footnotes]
\RequirePackage{zref-abspage}
\RequirePackage{auxhook}
\RequirePackage{ltxcmds}
\newif\if@RTL@footnote
\newif\if@autofootnoterule
\newif\iffootdir@debug

\def\simurgh@mpfootnoterule{\footnoterule}
\newcounter{footdir@label}
\renewcommand*{\thefootdir@label}{footdir@\the\value{footdir@label}}
\newcommand*{\footdir@write}{%
  \stepcounter{footdir@label}%
  \zref@labelbyprops{\thefootdir@label}{abspage}%
}

\newcommand*{\debugfootnotedirection}{\footdir@debugtrue}
\newtoks\footdir@toks
\newcommand*{\footdir@debug}[1]{%
  \iffootdir@debug
    \typeout{[footdir] #1}%
  \fi
}
\@ifpackagelater{zref-abspage}{2012/10/27}{%
  \newcommand*{\footdir@LeftRightElse}{%
    \ltx@ifundefined{footdir@\the\value{zabspage}}{%
      \footdir@debug{abspage \the\value{zabspage}: else}%
      \ltx@thirdofthree
    }{%
      \if\csname footdir@\the\value{zabspage}\endcsname L%
        \footdir@debug{abspage \the\value{zabspage}: left}%
        \expandafter\ltx@firstofthree
      \else
        \footdir@debug{abspage \the\value{zabspage}: right}%
        \expandafter\ltx@secondofthree
      \fi
    }%
  }%
}{%
  \providecommand*{\c@zabspage}{\c@abspage}%
  \newcommand*{\footdir@LeftRightElse}{%
    \footdir@write
    \zifrefundefined{\thefootdir@label}{%
      \footdir@debug{abspage ??: else}%
      \ltx@thirdofthree
    }{%
      \edef\footdir@abspage{%
        \zref@extractdefault{\thefootdir@label}{abspage}{0}%
      }%
      \ltx@ifundefined{footdir@\footdir@abspage}{%
        \footdir@debug{\footdir@abspage: else}%
        \ltx@thirdofthree
      }{%
        \if\csname footdir@\footdir@abspage\endcsname L%
          \footdir@debug{\footdir@abspage: left}%
          \expandafter\ltx@firstofthree
        \else
          \footdir@debug{\footdir@abspage: right}%
          \expandafter\ltx@secondofthree
        \fi
      }%
    }%
  }%
}
\newcommand*{\footdir@fntext}[1]{%
  \zifrefundefined{\thefootdir@label}{%
  }{%
    \edef\footdir@name{%
      footdir@\zref@extractdefault{\thefootdir@label}{abspage}{0}%
    }%
    \@ifundefined{\footdir@name}{%
      \expandafter\gdef\csname\footdir@name\endcsname{#1}%
    }{}%
  }%
}

\newcommand*{\footdir@temp}[3]{%
  \@ifdefinable{#1}{%
    \let#1#2%
    \renewcommand{#2}[1]{%
      \footdir@write
      \footdir@fntext{#3}%
      \stepcounter{footdir@label}%
      \footdir@fntext{#3}%
      \footdir@toks{##1}%
      \footdir@toks\expandafter{%
        \the\expandafter\footdir@toks
        \expandafter
        \zref@labelbyprops\expandafter{\thefootdir@label}{abspage}%
      }%
      \expandafter#1\expandafter{\the\footdir@toks}%
    }%
  }%
}

\def\right@footnoterule{%
  \hbox dir TRT to \columnwidth
  {\vbox{\kern -3\p@
   \hrule width .4\columnwidth \kern2.6\p@}\hfil}}
\def\left@footnoterule{%
  \hbox dir TLT to \columnwidth
  {\vbox{\kern -3\p@
   \hrule width .4\columnwidth \kern2.6\p@}\hfil}}
\def\textwidth@footnoterule{\kern-3\p@
  \hrule \@width \columnwidth \kern 2.6\p@}
\def\leftfootnoterule{%
\@autofootnoterulefalse%
\def\footnoterule{\left@footnoterule}}
\def\rightfootnoterule{%
\@autofootnoterulefalse%
\def\footnoterule{\right@footnoterule}}
\def\textwidthfootnoterule{%
\@autofootnoterulefalse%
\def\footnoterule{\textwidth@footnoterule}}

\def\auto@footnoterule{%
\footdir@LeftRightElse%
\left@footnoterule%
\right@footnoterule%
\left@footnoterule%
}

\def\autofootnoterule{%
\@autofootnoteruletrue%
\def\footnoterule{\auto@footnoterule}%
}

\newcommand*{\SplitFootnoteRule}{%
  \gdef\split@prev{0}
  \let\pagefootnoterule\footnoterule
  \let\mpfootnoterule\footnoterule
  \def\splitfootnoterule{\kern-3\p@ \hrule \kern2.6\p@}
  \def\footnoterule{\relax
    \ifx \@listdepth\@mplistdepth
      \mpfootnoterule
    \else
      \ifnum\split@prev=\z@
        \pagefootnoterule
      \else
        \splitfootnoterule
      \fi
      \xdef\split@prev{\the\insertpenalties}%
    \fi
  }%
}

\def\RTLfootnote{\@ifnextchar[\@xRTLfootnote{\stepcounter\@mpfn
     \protected@xdef\@thefnmark{\thempfn}%
     \@footnotemark\@RTLfootnotetext}}
\def\@xRTLfootnote[#1]{%
   \begingroup
     \csname c@\@mpfn\endcsname #1\relax
     \unrestored@protected@xdef\@thefnmark{\thempfn}%
   \endgroup
   \@footnotemark\@RTLfootnotetext}
\def\LTRfootnote{\@ifnextchar[\@xLTRfootnote{\stepcounter\@mpfn
     \protected@xdef\@thefnmark{\thempfn}%
     \@footnotemark\@LTRfootnotetext}}
\def\@xLTRfootnote[#1]{%
   \begingroup
     \csname c@\@mpfn\endcsname #1\relax
     \unrestored@protected@xdef\@thefnmark{\thempfn}%
   \endgroup
   \@footnotemark\@LTRfootnotetext}



\long\def\@footnotetext#1{\insert\footins{%
    \if@RTL@footnote%
      \@RTLtrue%
      \pardir TRT%
      \textdir TRT%
      \reset@parsifont%
    \else%
      \@RTLfalse%
      \pardir TLT%
      \textdir TLT%
      \reset@latinfont%
    \fi%
    \footnotesize
    \interlinepenalty\interfootnotelinepenalty
    \splittopskip\footnotesep
    \splitmaxdepth \dp\strutbox \floatingpenalty \@MM
    \hsize\columnwidth \@parboxrestore
    \protected@edef\@currentlabel{%
       \csname p@footnote\endcsname\@thefnmark
    }%
    \color@begingroup
      \@makefntext{%
        \rule\z@\footnotesep\ignorespaces#1\@finalstrut\strutbox}%
    \color@endgroup}}%

\long\def\@RTLfootnotetext#1{\insert\footins{%
   \@RTLtrue%
    \pardir TRT%
    \textdir TRT%
    \reset@parsifont\footnotesize
    \interlinepenalty\interfootnotelinepenalty
    \splittopskip\footnotesep
    \splitmaxdepth \dp\strutbox \floatingpenalty \@MM
    \hsize\columnwidth \@parboxrestore
    \protected@edef\@currentlabel{%
       \csname p@footnote\endcsname\@thefnmark
    }%
    \color@begingroup
      \@makefntext{%
        \rule\z@\footnotesep\ignorespaces#1\@finalstrut\strutbox}%
    \color@endgroup}}%

\long\def\@LTRfootnotetext#1{\insert\footins{%
   \@RTLfalse%
    \pardir TLT%
    \textdir TLT%
    \reset@latinfont\footnotesize
    \interlinepenalty\interfootnotelinepenalty
    \splittopskip\footnotesep
    \splitmaxdepth \dp\strutbox \floatingpenalty \@MM
    \hsize\columnwidth \@parboxrestore
    \protected@edef\@currentlabel{%
       \csname p@footnote\endcsname\@thefnmark
    }%
    \color@begingroup
      \@makefntext{%
        \rule\z@\footnotesep\ignorespaces#1\@finalstrut\strutbox}%
    \color@endgroup}}%

\AtBeginDocument{%
\if@RTL@footnote
  \newcommand*{\simurgh@footdir@footnote}{R}
\else
  \newcommand*{\simurgh@footdir@footnote}{L}
\fi%
}

\footdir@temp\footdir@ORG@simurgh@footnotetext\@footnotetext{\simurgh@footdir@footnote}%
\footdir@temp\footdir@ORG@simurgh@RTLfootnotetext\@RTLfootnotetext{R}%
\footdir@temp\footdir@ORG@simurgh@LTRfootnotetext\@LTRfootnotetext{L}%

\long\def\@mpRTLfootnotetext#1{%
  \if@autofootnoterule\ifnum\c@mpfootnote=\@ne\global\let\simurgh@mpfootnoterule\right@footnoterule\fi\fi%
  \global\setbox\@mpfootins\vbox{%
   \@RTLtrue%
    \pardir TRT%
    \textdir TRT%
    \unvbox\@mpfootins
    \reset@parsifont\footnotesize
    \hsize\columnwidth
    \@parboxrestore
    \protected@edef\@currentlabel
         {\csname p@mpfootnote\endcsname\@thefnmark}%
    \color@begingroup
      \@makefntext{%
        \rule\z@\footnotesep\ignorespaces#1\@finalstrut\strutbox}%
    \color@endgroup}}
\long\def\@mpLTRfootnotetext#1{%
 \if@autofootnoterule\ifnum\c@mpfootnote=\@ne\global\let\simurgh@mpfootnoterule\left@footnoterule\fi\fi%
  \global\setbox\@mpfootins\vbox{%
   \@RTLfalse%
    \pardir TLT%
    \textdir TLT%
    \unvbox\@mpfootins
    \reset@latinfont\footnotesize
    \hsize\columnwidth
    \@parboxrestore
    \protected@edef\@currentlabel
         {\csname p@mpfootnote\endcsname\@thefnmark}%
    \color@begingroup
      \@makefntext{%
        \rule\z@\footnotesep\ignorespaces#1\@finalstrut\strutbox}%
    \color@endgroup}}
\long\def\@mpfootnotetext#1{%
 \if@autofootnoterule\ifnum\c@mpfootnote=\@ne\if@RTL@footnote\global\let\simurgh@mpfootnoterule\right@footnoterule\else\global\let\simurgh@mpfootnoterule\left@footnoterule\fi\fi\fi%
  \global\setbox\@mpfootins\vbox{%
    \if@RTL@footnote%
      \@RTLtrue%
      \pardir TRT%
      \textdir TRT%
      \reset@parsifont%
    \else%
     \@RTLfalse%
      \pardir TLT%
      \textdir TLT%
      \reset@latinfont%
    \fi%
    \unvbox\@mpfootins
    \footnotesize
    \hsize\columnwidth
    \@parboxrestore
    \protected@edef\@currentlabel
         {\csname p@mpfootnote\endcsname\@thefnmark}%
    \color@begingroup
      \@makefntext{%
        \rule\z@\footnotesep\ignorespaces#1\@finalstrut\strutbox}%
    \color@endgroup}}
    
\def\@iiiminipage#1#2[#3]#4{%
  \leavevmode
  \@pboxswfalse
  \setlength\@tempdima{#4}%
  \def\@mpargs{{#1}{#2}[#3]{#4}}%
  \setbox\@tempboxa\vbox\bgroup
    \color@begingroup
      \hsize\@tempdima
      \textwidth\hsize \columnwidth\hsize
      \@parboxrestore
      \def\@mpfn{mpfootnote}\def\thempfn{\thempfootnote}\c@mpfootnote\z@
      \let\@footnotetext\@mpfootnotetext
      \let\@LTRfootnotetext\@mpLTRfootnotetext
      \let\@RTLfootnotetext\@mpRTLfootnotetext
      \let\@listdepth\@mplistdepth \@mplistdepth\z@
      \@minipagerestore
      \@setminipage}    
    
\def\endminipage{%
    \par
    \unskip
    \ifvoid\@mpfootins\else
      \vskip\skip\@mpfootins
      \normalcolor
      \simurgh@mpfootnoterule
      \unvbox\@mpfootins
    \fi
    \@minipagefalse   %% added 24 May 89
  \color@endgroup
  \egroup
  \expandafter\@iiiparbox\@mpargs{\unvbox\@tempboxa}}
      

\def\LTRthanks#1{\footnotemark
    \protected@xdef\@thanks{\@thanks
        \protect\LTRfootnotetext[\the\c@footnote]{#1}}%
}
\def\LTRfootnotetext{%
     \@ifnextchar [\@xLTRfootnotenext
       {\protected@xdef\@thefnmark{\thempfn}%
    \@LTRfootnotetext}}
\def\@xLTRfootnotenext[#1]{%
  \begingroup
     \csname c@\@mpfn\endcsname #1\relax
     \unrestored@protected@xdef\@thefnmark{\thempfn}%
  \endgroup
  \@LTRfootnotetext}
\def\RTLthanks#1{\footnotemark
    \protected@xdef\@thanks{\@thanks
        \protect\RTLfootnotetext[\the\c@footnote]{#1}}%
}
\let\@thanks\@empty
\def\RTLfootnotetext{%
     \@ifnextchar [\@xRTLfootnotenext
       {\protected@xdef\@thefnmark{\thempfn}%
    \@RTLfootnotetext}}
\def\@xRTLfootnotenext[#1]{%
  \begingroup
     \csname c@\@mpfn\endcsname #1\relax
     \unrestored@protected@xdef\@thefnmark{\thempfn}%
  \endgroup
  \@RTLfootnotetext}


\endinput