\NeedsTeXFormat{LaTeX2e}[1994/06/01]
\ProvidesPackage{bibletext}
  [2016/02/18 v0.1 Insert Bible texts]

\DeleteShortVerb{\|}

\RequirePackage{pgfkeys}

\newif\ifbibletextRef
\newif\ifbibletextOmitChapter
\newif\ifbibletextOmitRepeatedChapter
\pgfkeys{
  /bibletext/.is family, /bibletext,
  default/.style={
    translation=kjv,
    ref=false,
    omitchapter=false,
  },
  translation/.estore in = \bibletextTranslation, % unused for now
  ref/.is if=bibletextRef,
  omitchapter/.is if=bibletextOmitChapter,
  omitrepeatedchapter/.is if=bibletextOmitRepeatedChapter,
}
\newcommand\bibletext[2][]{%
  \pgfkeys{/bibletext, default, #1}%
  \ifbibletextRef%
    \ifbibletextOmitRepeatedChapter%
      \input{|"bible -f '#2' | grep -o '[[:digit:]].*' | head -n1"}%
      \input{|"bible -f '#2' | grep -o '[[:digit:]][[:digit:]]*[[:space:]].*' | tail -n+2"}%
    \else\ifbibletextOmitChapter%
      \input{|"bible -f '#2' | grep -o '[[:digit:]][[:digit:]]*[[:space:]].*'"}%
    \else%
      \input{|"bible -f '#2' | grep -o '[[:digit:]].*'"}%
    \fi\fi%
  \else%
    \input{|"bible -f '#2' | cut -d' ' -f2-"}%
  \fi%
}

\endinput

