%
%  Haim Bar      haim.bar@uconn.edu
%  HaiYing Wang  haiying.wang@uconn.edu
%
%  This package is based on an ongoing work by Haim Bar and HaiYing Wang, and comments and questions are welcome!

\newif\ifruncode
% Change to \runcodefalse if you want to suspend code execution
\runcodetrue
\DeclareOption{run}{\runcodetrue}
\DeclareOption{cache}{\runcodefalse}

\newif\ifminted
\mintedtrue
\DeclareOption{nominted}{\mintedfalse}

% Some editors terminate all child processes after LaTeX compiling such as Emacs with Auctex. For this case, use the nohup option. It set the variable notnohup to be false, and the server will not be terminated by the parent process.
\newif\ifnotnohup
\notnohuptrue
\DeclareOption{nohup}{\notnohupfalse}

\DeclareOption{R}{
  % create a configuration file for R server if it does not exist.
  \IfFileExists{R.config}{}{
    \newwrite\tempfile
    \immediate\openout\tempfile=R.config
    \immediate\write\tempfile{[SERVERCONFIG]}
    \immediate\write\tempfile{PORT = 65432}
    \immediate\write\tempfile{DEBUGFILE = Rdebug.txt}
    \immediate\write\tempfile{PIPETIMEOUT = 300}
    \immediate\closeout\tempfile
  }
  % Start the server. Need to run just once. Can comment out after the first
  % compilation, but remember to terminate the server
  \ifnotnohup
  {\immediate\write18{python3 -c 'from talk2stat.talk2stat import server,client; server("./","R") if not client("./","R","``` ```") else print("server is already running")'}}
  \else
  {\immediate\write18{nohup python3 -c 'from talk2stat.talk2stat import server,client; server("./","R") if not client("./","R","``` ```") else print("server is already running")' &}}
  \fi  
}

\DeclareOption{julia}{
  % create a configuration file for R server if it does not exist.
  \IfFileExists{julia.config}{}{
    \newwrite\tempfile
    \immediate\openout\tempfile=julia.config
    \immediate\write\tempfile{[SERVERCONFIG]}
    \immediate\write\tempfile{PORT = 65431}
    \immediate\write\tempfile{DEBUGFILE = juliadebug.txt}
    \immediate\write\tempfile{PIPETIMEOUT = 300}
    \immediate\closeout\tempfile
  }
  % Start the server. Need to run just once. Can comment out after the first
  % compilation, but remember to terminate the server
  \ifnotnohup
  {\immediate\write18{python3 -c 'from talk2stat.talk2stat import server,client; server("./","julia") if not client("./","julia","``` ```") else print("server is already running")'}}
  \else
  {\immediate\write18{nohup python3 -c 'from talk2stat.talk2stat import server,client; server("./","julia") if not client("./","julia","``` ```") else print("server is already running")' &}}
  \fi  
}

\DeclareOption{matlab}{
  % create a configuration file for R server if it does not exist.
  \IfFileExists{matlab.config}{}{
    \newwrite\tempfile
    \immediate\openout\tempfile=matlab.config
    \immediate\write\tempfile{[SERVERCONFIG]}
    \immediate\write\tempfile{PORT = 65430}
    \immediate\write\tempfile{DEBUGFILE = matlabdebug.txt}
    \immediate\write\tempfile{PIPETIMEOUT = 300}
    \immediate\closeout\tempfile
  }
  % Start the server. Need to run just once. Can comment out after the first
  % compilation, but remember to terminate the server
  \ifnotnohup
  {\immediate\write18{python3 -c 'from talk2stat.talk2stat import server,client; server("./","matlab") if not client("./","matlab","``` ```") else print("server is already running")'}}
  \else
  {\immediate\write18{nohup python3 -c 'from talk2stat.talk2stat import server,client; server("./","matlab") if not client("./","matlab","``` ```") else print("server is already running")' &}}
  \fi  
}

\DeclareOption{python}{
  % create a configuration file for Python server if it does not exist.
  \IfFileExists{python.config}{}{
    \newwrite\tempfile
    \immediate\openout\tempfile=python.config
    \immediate\write\tempfile{[SERVERCONFIG]}
    \immediate\write\tempfile{PORT = 65433}
    \immediate\write\tempfile{DEBUGFILE = pythondebug.txt}
    \immediate\write\tempfile{PIPETIMEOUT = 300}
    \immediate\closeout\tempfile
  }
  % Start the server. Need to run just once. Can comment out after the first
  % compilation, but remember to terminate the server
  \ifnotnohup
  {\immediate\write18{python3 -c 'from talk2stat.talk2stat import server,client; server("./","python") if not client("./","python","``` ```") else print("server is already running")'}}
  \else
  {\immediate\write18{nohup python3 -c 'from talk2stat.talk2stat import server,client; server("./","python") if not client("./","python","``` ```") else print("server is already running")' &}}
  \fi  
}

\DeclareOption{stopserver}{
  \AtEndDocument{
  %% stop the server when the pdf compilation is done.
    \immediate\write18{python3 -c 'from talk2stat.talk2stat import client; client("./","R","QUIT")'}
    \immediate\write18{python3 -c 'from talk2stat.talk2stat import client; client("./","julia","QUIT")'}
    \immediate\write18{python3 -c 'from talk2stat.talk2stat import client; client("./","matlab","QUIT")'}
    \immediate\write18{python3 -c 'from talk2stat.talk2stat import client; client("./","python","QUIT")'}
}}

\ProcessOptions*

\usepackage{morewrites}               % allow more than 16 \write
\usepackage[many]{tcolorbox}            % to put boxes around text
\tcbset{colframe=blue!25,colback=blue!10} % default colors for box output
\usepackage{xcolor}               % for highlighting
\usepackage{inputenc}
\usepackage{textgreek}
\usepackage{filecontents}
\usepackage{xifthen}
\usepackage{xparse}
\usepackage{xstring}

% Use minted if it is loaded; otherwise use fvextra
\ifminted
\usepackage[cache=false]{minted}
\setminted{fontsize=\footnotesize,breaklines=true} % minted default
\else
\usepackage{fvextra}
\fi

\immediate\write18{mkdir tmp}

\definecolor{bg}{rgb}{0.95,0.95,0.95} % code block background color

\newcounter{codelisting}
\renewcommand{\thecodelisting}{\arabic{codelisting}}
\newenvironment{codelisting}[1][]{
 \par\vspace{\baselineskip}\noindent
  \refstepcounter{codelisting}
  \begin{ttfamily}
   \noindent \textbf{Listing~\thecodelisting. #1}
}
{
 \end{ttfamily}
  \noindent\ignorespacesafterend
 }  

% counter for code output temporary file names
\newcounter{codeOutput}
\setcounter{codeOutput}{0}

% a command to set the temporary file name for code output
\newcommand{\setvalue}[2]{
    \ifdefined #1
        \renewcommand{#1}{#2}
    \else
        \newcommand{#1}{#2}
    \fi
}
\setvalue{\tmpname}{} % initialize with a blank



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
% \showCode prints the source code, using minted for a pretty layout
% Arg #1 is the programming language,
% Arg #2 is the source file name,
% Args #3 and #4 are the first and last line to show (optional).

\NewDocumentCommand{\showCode}{m m O{} O{}}{% \-\\
  \IfFileExists{#2}{
    \ifthenelse{\isempty{#3}}{
    \ifminted
    \inputminted{#1}{#2}
    \vspace{-\medskipamount}
    \vspace{-\baselineskip}
    \else
    \VerbatimInput[fontsize=\footnotesize,linenos=true,frame=single,breaklines]{#2}
    \fi
  }{
    \ifminted
    \inputminted[firstline=#3, lastline=#4, firstnumber=1]{#1}{#2}
    \vspace{-\medskipamount}
    \vspace{-\baselineskip}
    \else
    \VerbatimInput[fontsize=\footnotesize,linenos=true,frame=single,breaklines, firstline=#3, lastline=#4, firstnumber=1]{#2}
    \fi
  } 
  }{
    \textcolor{red}{\textbf{showCode: File #2 does not exist!}}
  }

}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \runExtCode runs an external code.
% Arg #1 is the executable program,
% Arg #2 is the source file name,
% Arg #3 is the output file name (optional - if not given, the counter 
%        codeOutput is used).
% Arg #4 controls when to run the code (optional - if not given, it listens
%        to \runcode; run = force the code to run; cache or anything else =
%        use cache)
\NewDocumentCommand{\runExtCode}{m m m O{}}{
  \IfFileExists{#2}{
  \stepcounter{codeOutput}
  \ifthenelse{\isempty{#3}}
    { \setvalue{\tmpname}{tmp/\jobname_tmp\thecodeOutput.tex} }
    { \setvalue{\tmpname}{tmp/#3.tex} }
  % toggle \runcode above if you want to enable/disable code execution
    \ifthenelse{\isempty{#4}}
      { \ifruncode
        \immediate\write18{#1 #2 > \tmpname }
        \fi
      }{
        \ifstrequal{#4}{run}{\immediate\write18{#1 #2 > \tmpname }}{}
      }
  }{
    \textcolor{red}{\textbf{runExtCode: File #2 does not exist!}}
  }
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \includeOutput[Arg #1]{Arg #2} is used to embed the output from executed code.
% Arg #1 is the output file name (optional - if not given, the counter 
%        codeOutput is used).
% Arg #2 is the optional type output with default "vbox"
%         (vbox = verbatim in a box, tex = pure latex, or inline)

\NewDocumentCommand{\includeOutput}{m O{vbox}}{\-
  \ifthenelse{\isempty{#1}}
  {\setvalue{\tmpname}{tmp/\jobname_tmp\thecodeOutput.tex}}
  {\unskip\setvalue{\tmpname}{tmp/#1.tex}\unskip}\unskip
  % even if the code is not executed, but we ran it before, we can use a 
  % cached version if it exists.
  \IfFileExists{\tmpname}
  {
    \ifstrequal{#2}{vbox}
    {\begin{tcolorbox}
        \ifminted\unskip
          \inputminted{text}{\tmpname}\unskip
        \else\unskip
        \VerbatimInput[fontsize=\footnotesize,breaklines]{\tmpname}\unskip
        \fi
      \end{tcolorbox}}
    {\unskip\ifstrequal{#2}{inline}
      {\unskip\input{\tmpname}\unskip}
      {\input{\tmpname}}}}
  % if code execution disabled, and no cache:
  {\begin{tcolorbox}
      \textbf{Output file \tmpname~ not found}. Check the file name (it may be that the file name was given with the suffix .tex. If so, remove it). If the file name is correct the problem may be because code execution is disabled and no cache is available. If so, force the code to run again (using the [run] option).
    \end{tcolorbox}}}


\NewDocumentCommand{\checkZeroBytes}{m}{
  \immediate\write18{python3 -c 'import os; print("ZERO BYTES IN OUTPUT", file=open("#1", "a")) if os.path.getsize("#1") == 0 else True'}
}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \inln{Arg #1}{Arg #2}[Arg #3] is used to execute short source code
% and embed resulting output.
% Arg #1 is the executable program;
% Arg #2 is the source code
% Arg #3 is the type output (if skipped or with empty value the default type is
%        inline; vbox = verbatim in a box);

\NewDocumentCommand{\inln}{m m O{inline}}{\-
  \stepcounter{codeOutput}\unskip
  \unskip\setvalue{\tmpname}{tmp/\jobname_inln\thecodeOutput}\unskip\unskip\unskip
  \ifruncode % cache mode - don't try to run the code, just get the previous results
    \IfBeginWith{#2}{```}{\ifruncode\immediate\write18{#1 > \tmpname.tex}\unskip\fi}
    {\newwrite\tempfile
      \immediate\openout\tempfile=\tmpname.txt
      \immediate\write\tempfile{#2}
      \immediate\closeout\tempfile
      \ifruncode
      \immediate\write18{#1 \tmpname.txt > \tmpname.tex}\unskip
    \fi}
  \fi % end cache mode
  \IfFileExists{\tmpname.tex}
  {\checkZeroBytes{\tmpname.tex}\unskip
    \ifstrequal{#3}{vbox}
        {\begin{tcolorbox}
        \ifminted\unskip
          \inputminted{text}{\tmpname.tex}\unskip
        \else\unskip
        \VerbatimInput[fontsize=\footnotesize,breaklines]{\tmpname.tex}\unskip
        \fi
      \end{tcolorbox}}
    {\unskip\input{\tmpname.tex}\unskip}}
    {\textcolor{red}{\textbf{Output file \tmpname~ not found}}}}

\NewDocumentCommand{\runR}{O{python3 -c 'from talk2stat.talk2stat import client; client("./","R","#2")'} m m O{}}
{ 
  \def\runcmd{python3 -c 'from talk2stat.talk2stat import client; client("./","R","#2")'}
  \ifstrequal{#1}{\runcmd}
  {\runExtCode{#1}{}{#3}[#4]}
  {\runExtCode{#1}{#2}{#3}[#4]}
}

\NewDocumentCommand{\inlnR}{O{python3 -c 'from talk2stat.talk2stat import client; client("./","R","\tmpname.txt")'} m O{inline}}{\IfBeginWith{#2}{```}{\inln{python3 -c 'from talk2stat.talk2stat import client;client("./","R","#2")'}{#2}[#3]}{\inln{#1}{#2}[#3]}}

\NewDocumentCommand{\runJulia}{O{python3 -c 'from talk2stat.talk2stat import client; client("./","julia","#2")'} m m O{}}
{ 
  \def\runcmd{python3 -c 'from talk2stat.talk2stat import client; client("./","julia","#2")'}
  \ifstrequal{#1}{\runcmd}
  {\runExtCode{#1}{}{#3}[#4]}
  {\runExtCode{#1}{#2}{#3}[#4]}
}

\NewDocumentCommand{\inlnJulia}{O{python3 -c 'from talk2stat.talk2stat import client; client("./","julia","\tmpname.txt")'} m O{inline}}{\IfBeginWith{#2}{```}{\inln{python3 -c 'from talk2stat.talk2stat import client;client("./","julia","#2")'}{#2}[#3]}{\inln{#1}{#2}[#3]}}

\NewDocumentCommand{\runMatlab}{O{python3 -c 'from talk2stat.talk2stat import client; client("./","matlab","#2")'} m m O{}}
{ 
  \def\runcmd{python3 -c 'from talk2stat.talk2stat import client; client("./","matlab","#2")'}
  \ifstrequal{#1}{\runcmd}
  {\runExtCode{#1}{}{#3}[#4]}
  {\runExtCode{#1}{#2}{#3}[#4]}
}

\NewDocumentCommand{\inlnMatlab}{O{python3 -c 'from talk2stat.talk2stat import client; client("./","matlab","\tmpname.txt")'} m O{inline}}{\IfBeginWith{#2}{```}{\inln{python3 -c 'from talk2stat.talk2stat import client;client("./","matlab","#2")'}{#2}[#3]}{\inln{#1}{#2}[#3]}}


\NewDocumentCommand{\runPython}{O{python3 -c 'from talk2stat.talk2stat import client; client("./","python","#2")'} m m O{}}
{ 
  \def\runcmd{python3 -c 'from talk2stat.talk2stat import client; client("./","python","#2")'}
  \ifstrequal{#1}{\runcmd}
  {\runExtCode{#1}{}{#3}[#4]}
  {\runExtCode{#1}{#2}{#3}[#4]}
}

\NewDocumentCommand{\inlnPython}{O{python3 -c 'from talk2stat.talk2stat import client; client("./","python","\tmpname.txt")'} m O{inline}}{\IfBeginWith{#2}{```}{\inln{python3 -c 'from talk2stat.talk2stat import client;client("./","python","#2")'}{#2}[#3]}{\inln{#1}{#2}[#3]}}


%%%%%%%%
% For Python batch mode, implement saving and restoring session
% Arg #1 is the source file name,
% Arg #2 is the output file name

\NewDocumentCommand{\runPythonBatch}{m m}{%\-\\
   \stepcounter{codeOutput}
   \ifthenelse{\isempty{#2}} % set the output file name
        { \setvalue{\tmpname}{tmp/\jobname_tmp\thecodeOutput.tex} }
        { \setvalue{\tmpname}{tmp/#2.tex} }

   \IfFileExists{#1}{
     \ifruncode
          \newwrite\tempfile
          \immediate\openout\tempfile=tmp/\jobname_#1
          \immediate\write\tempfile{import dill}
          \immediate\write\tempfile{from os.path import exists}
          \immediate\write\tempfile{if exists('tmp/session'):}
          \immediate\write\tempfile{    dill.load_session('tmp/session')}
          \immediate\write\tempfile{}
          \newread\infile
          \openin\infile=#1
          \begingroup\endlinechar=-1
          \loop\unless\ifeof\infile
             \read\infile to\fileline  % Read one line and store it into \fileline
             \immediate\write\tempfile{\unexpanded\expandafter{\fileline}}  % print the content to copy.txt
          \repeat
          \endgroup
          \closein\infile
          \immediate\write\tempfile{dill.dump_session('tmp/session')}
          \immediate\write\tempfile{}
          \immediate\closeout\tempfile
          \immediate\write18{python3  tmp/\jobname_#1 > \tmpname}
       \fi
     }
     {
        \immediate\write18{cat /dev/null > \tmpname}
        \textcolor{red}{\textbf{runPythonBatch: File #1 does not exist!}}
     }
}


\endinput
