#!/bin/bash
# ZBLBUILD
# version 0.1c
# guido.milanese@unicatt.it
# 2019.08.26
#
# This interactive script helps in the setting of Biblatex options
#
# Copyright (c) 2019 Guido Milanese
# License:
# The LaTeX Project Public License License (LPPL)
# version 1.3c or later
# This work may be distributed and/or modified under the
# conditions of the LaTeX Project Public License, either version 1.3
# of this license or (at your option) any later version.
# The latest version of this license is in
#   http://www.latex-project.org/lppl.txt
# and version 1.3 or later is part of all distributions of LaTeX
# version 2005/12/01 or later.
#
# This work has the LPPL maintenance status `maintained'.
# 
# The Current Maintainer of this work is Guido Milanese.
#
# This work consists of the file zblbuild-select
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

################
# FUNCTIONS
################
#
# GENERAL:
# Function to build a radiolist and save the choice
doselect () {
mychoice=$(zenity --width=500 --height=500 \
		  --list --text "${message[@]}"\
		  --radiolist --column "Choice" --column "Name" --column "Comment-example"\
		  "${myarr[@]}")
		  retsel=$?
}

# Checks the value returned by previous function.
# if no choice calls again 'doselect'
rundoselect () {
while [ -z "$mychoice" ]; do 
			  if ((retsel != 0)); then
    zenity --error \
    --text="Cancelled -- Exiting"
    exit 1
          fi
			  doselect 
	done
}

# resets all the global values 
resetall () {
	message= ; myarr= ; mychoice=
	return ;
}

################
# CHECKDEP
################
# zenity installed?
checkdep () {
 if ! type zenity > /dev/null 2>&1; then
        echo "zenity is not installed. Exiting."; exit 1;
 fi		  
}

################
# OPENPROGRAMME
################
openprogramme () {
 zenity --info --width="500" --height="50" --no-wrap --timeout 10 --title="ZBL-BUILD" \
		--text "\n\n\n\t\t\t<big><b><span
color=\"blue\">ZBL-BUILD</span></b></big>
\t\t<span color=\"blue\"><b>Zenity Biblatex Build</b></span>\n
Sets basic BIBLATEX options,
with some options for the BIBLATEX-PHILOSOPHY styles\n
version 0.1c, 2019.08.26\n
<i>(c) 2019 Guido Milanese -- guido.milanese@unicatt.it</i>\n
\t\tLicense: LPPL\n
Warning: This utility sets a limited number of options:
please refer to the <i><b>Biblatex</b></i> manual (3.12, 10.30.2018),
and to the <i><b>Biblatex-Philosophy</b></i> manual (1.9.8a, 03.03.2018)"
}

################
# CHOOSEFILE
################
choosefile () { #
		  Fileyn= 		# resets 
		  Leaveyn=1		# the user does NOT want to leave the function
		  Bibfile=
		  Bibfile=`zenity --file-selection  --filename="/home/$USER"  --title="Select a BibTeX file"`
case $? in
         0)
                zenity --question --title="Select a BibTeX file" \
         --text="Would you like to add another file?" 
					 Fileyn=$? ;;
         1)
		zenity --question --title="Leaving?"\
                --text="No file selected!\nWould you like to leave?" 
			 Leaveyn=$? 
			 Fileyn=0
		 ;;
        -1)		 zenity --error \
			--text="Unexpected error"
                Fileyn=$? ;;
esac
# Gesamtbibfile is the complete list of files
# If Bibfile is empty does not do anything
# in order to avoid getting \addbibresource{}
# if Bibfile is not empty:
#  (1) Biblayex
#		(1) adds "\addbibresource{" ...'}' to $Bibfile
#		(2) adds $Bibfile to Gesamtbibfile with "\n"
#  (2) Markdown
#		(1) changes $Bibfile to: "-\t" $Bibfile "\n"
#		(2) adds $Bibfile to Gesamtbibfile -- no "\n"

	if [ ! -z "$Bibfile" ] ; then
#		then Bibfile="\\\\addbibresource{$Bibfile}" ; 
if [ $Bibmkd = "LaTeX" ]; then		  
	 Bibfile="\\\\addbibresource{$Bibfile}" ;
	 else Bibfile="-\t $Bibfile\n"
	 fi
if [ $Bibmkd = "LaTeX" ]; then		  
		Gesamtbibfile="$Gesamtbibfile\n$Bibfile"
		else
		Gesamtbibfile="$Gesamtbibfile$Bibfile"
fi		

	fi

if [ $Leaveyn = 0 ];	# ----------------
   then return 1		# leaves if chosen
	fi						# ----------------

if [ $Fileyn = 0 ];	# --------------
   then choosefile 	# loops if chosen
  fi						# --------------
}

################
# DOCHOOSEFILE
################
# simple wrap for choosefile:
# in this way the message is displayed only the first time
# and also adds a comment string before Gesamtbibfile (see choosefile)
# if the variable is not empty
dochoosefile () {
zenity --info --no-wrap --title "BibTeX files" \
--text "In the next window you can choose \
one or more <b>BibTeX</b> files."
choosefile
	if [ ! -z "$Gesamtbibfile" ]; then
				  if [ $Bibmkd = "LaTeX" ];
				  then Gesamtbibfile="%\n%BibTeX-file(s)$Gesamtbibfile";
				  else Gesamtbibfile=$Gesamtbibfile
				  fi
	fi
}

################
# FINALMESSAGE
################
# final msg before showing Biblatex configuration
finalmessage () {
zenity --info --no-wrap --title "Done!" \
--text "In the next window you can edit your <b>Biblatex</b> configuration \
(e.g. add options, correct your choices...) \n\
and copy/paste to your LaTeX file or YAML block. \n\
If you prefer, open and edit 'biblatex.opt' \
(in your home folder) with any text editor"
}

################
# BYEBYE
################
byebye () {
zenity --info --timeout="7" --title="Byebye" \
--text="\n\nThank you for using <b>ZBL-BUILD</b>!"
}
################
# Csquotes
################
csquotes () {
message="$Premsg 1a. Set a language for csquotes: $Pstmsg"
declare -a myarr=(\
	True  "autostyle=once" "Uses the document main language"\	  
	False "autostyle=true" "Adapts to current language"\	  
	False "austrian=quotes" " "\
	False "austrian=guillemets" " "\
	False "croatian=quotes" " "\
	False "croatian=guillemets" " "\
	False "croatian=guillemets*" " "\
	False "danish=quotes" " "\
	False "danish=guillemets" " "\
	False "danish=topquotes" " "\
	False "english=american" " "\
	False "english=british" " "\
	False "french=quotes" " "\
	False "french=quotes*" " "\
	False "french=guillemets" " "\
	False "french=guillemets*" " "\
	False "german=quotes" " "\
	False "german=guillemets" " "\
	False "german=swiss" " "\
	False "italian=guillemets" " "\
	False "italian=quotes" " "\
	False "norwegian=guillemets" " "\
	False "norwegian=quotes" " "\
	False "portuguese=portuguese" " "\
	False "portuguese=brazilian" " "\
	False "spanish=spanish" " "\
	False "spanish=mexican" " "\
	False "swedish=quotes" " "\
	False "swedish=guillemets" " "\
	False "swedish=guillemets*" " "\
		  		  );
	rundoselect
	Csquotes="\usepackage[$mychoice]{csquotes}\n"
	resetall
}

################
# Backend
################
backend () {
message="$Premsg 1b. Select backend: $Pstmsg"
declare -a myarr=(\
		  True  "biber" "Default backend of Biblatex"\
		  False "bibtex" "Legacy BibTeX -- Ascii"\
		  False "bibtex8" "BibTeX -- Latin1"\
		  );
	rundoselect
	Backend=$mychoice
	resetall
	}

#################
# Style - general
#################
setbasicstyle () {
 message="$Premsg 2. Basic style family: $Pstmsg"
 declare -a myarr=(\
		  False "authordate" "Smith 2003"\
		  False "authortitle" "Smith, The Book"\
		  False "numeric" "[1]"\
		  False "philosophy" "Humanities-oriented: several flavours"\
		  False "reading" "It simply loads the authortitle style"\
		  False "verbose" "J. Smith, The Book, Oxford 2003"\
		  False "debug" "Prints the entry key. For debugging only"\
		  False "draft" "Uses the entry keys in citations"\
		  );
	rundoselect
	Basicstyle=$mychoice
	echo $Basicstyle
	resetall
	}
######################
# Style - final choice
######################
setfinalstyle () {
 message="$Premsg 3. Given your previous choice, select a style family $Pstmsg"

        if [ $Basicstyle = 'numeric' ];
        then
           declare -a myarr=(\
			  		False "numeric" "[8, 3, 1, 7, 2]"\
			  		False "numeric-comp" "[1���3, 7, 8]"\
			  		False "numeric-verb" "[8]; [3]; [1]; [7]; [2]"\
			  		False "alphabetic" "[Doe92; Doe95; Jon98]"\
			  		False "alphabetic-verb" "[Doe92]; [Doe95]; [Jon98]"\
					); 

        elif [ $Basicstyle = 'authordate' ];
        then
           declare -a myarr=(\
		False "authoryear" "Doe 1995a; Doe 1995b; Jones 1998"\	 
		False "authoryear-comp" "Doe 1995a,b; Jones 1998"\	 
		False "authoryear-ibid" "Replaces repeated citations by 'ibidem'"\	 
		False "authoryear-icomp" "Combines authoryear-comp + authoryear-ibid"\	 
				  );
        
    elif [ $Basicstyle = 'authortitle' ];
        then
           declare -a myarr=(\
		False "authortitle" "Doe, First title; Doe, Second title"\	 
		False "authortitle-comp" "Doe, First title, Second title"\	 
		False "authortitle-dw" "Doe: First title; Second title"\	 
		False "authortitle-ibid" "Replaces repeated citations by 'ibidem'"\	 
		False "authortitle-icomp" "Combines authortitle-comp + authortitle-ibid"\	 
		False "authortitle-terse" "Prints title only if >1 titles of same author"\	 
		False "authortitle-tcomp" "Combines authortitle-comp + authortitle-terse"\	 
		False "authortitle-ticomp" "Combines authortitle-icomp +	authortitle-terse"\
				  );

        elif [ $Basicstyle = 'philosophy' ];
        then
           declare -a myarr=(\
	   False "philosophy-classic" "Knuth (1984, 1986a,b,c,d)"\	 
		False "philosophy-modern" "Citations as classic, bibliography in blocks"\	 
	   False "philosophy-verbose" "1st citation full, then short form"\ ); 

        elif [ $Basicstyle = 'verbose' ];
        then
           declare -a myarr=(\
		False "verbose" "Doe 1995a; Doe 1995b; Jones 1998"\	 
		False "verbose-ibid" "Replaces repeated citations by 'ibidem'"\	 
		False "verbose-note" "Doe 1995a,b; Jones 1998"\	 
		False "verbose-inote" "Combines authoryear-comp + authoryear-ibid"\	 
		False "verbose-trad1" "Doe, First title; Doe, Second title"\	 
		False "verbose-trad2" "Doe, First title, Second title"\	 
		False "verbose-trad3" "Replaces repeated citations by 'ibidem'"\	 
				  ); 
        elif [ $Basicstyle = 'reading' ];
        then
           declare -a myarr=(\
		True "reading" "It simply loads the authortitle style"\	 
				  ); 

     elif [ $Basicstyle = 'draft' ];
        then
           declare -a myarr=(\
		True "draft" "Uses the entry keys in citations"\	 
				  ); 

     elif [ $Basicstyle = 'debug' ];
        then
           declare -a myarr=(\
		True "debug" "Prints the entry key. For debugging only"\	 
				  ); 
		  fi		 
	rundoselect
	Style=$mychoice
	resetall	
	}

################	
# Sorting order
################
setsorting () {
 message="$Premsg 4. Sorting order. $Pstmsg"

		  declare -a myarr=(\
		  True  "nty"   "Sort by name, title, year (default)"\
		  False "nyt"   "Sort by name, year, title"\
		  False "nyvt"  "Sort by name, year, volume, title"\
		  False "anyt"  "Sort by alphabetic label, name, year, title"\
		  False "anyvt" "Sort by alphabetic label, name, year, volume, title"\
		  False "ynt"   "Sort by year, name, title"\
		  False "ydnt"  "Sort by year (descending), name, title"\
		  False "none"  "No sort. Entries processed in citation order"\
		  False "debug" "Sort by entry key. For debugging only"\
		  );
	rundoselect
	Sorting=$mychoice
	resetall
}

#####################
# General options?
#####################
generaloptyn () {
		  zenity --question --title="Additional options (general)" \
--text="Would you like to set some additional options?"
 proceed=$?		# most recent variable, i.e. return of zenity dialogue
		if ! [ $proceed = '0' ] ; 
	then styleoptyn ; setbibmd; dochoosefile ; printall ; byebye ; exit 0 ; # prints basic conf. & exits 0
			else generaloptset ; # setbibmd
		fi
		proceed=
}

#####################
# Style options?
#####################
styleoptyn () {
zenity --question  --title="Additional options for style $Basicstyle" \
--text="Would you like to set some options for <b><i>$Basicstyle</i></b>?"
 proceed=$?		# most recent variable, i.e. return of zenity dialogue
		if ! [ $proceed = '0' ] ; 
 then  dochoosefile ; finalmessage ; printall ; byebye ; exit 0 ; # prints & exits 0
 else  styleoptset  ; dochoosefile ; finalmessage ; printall ; byebye ; exit 0;	  
		fi
		proceed=
}

##########################################################
# VARIOUS TASKS
##########################################################

#####################
# GENERALOPTSET
#####################
# ADDITIONAL BIBLATEX COMMON OPTIONS:
# sets isbn, url, doi, eprint
generaloptset () {
# sets a variable from zenity using command substitution
S=`zenity --list --checklist --width=200 --height=200 --title="Choose the fields you wish to print" --column "Print?" --column "Fields" --separator=' ' \
    FALSE Isbn \
    FALSE Url \
    FALSE DOI \
    FALSE Eprint`
#####
# sets an array from variable
read -a addopt <<<$S
# scans the array and sets variables
for i in "${addopt[@]}"
do
		  case    $i in
			  Isbn)
								ISBNYN=true
								;;
	   	  Url)
					  			URLYN=true		  
								;;
           DOI)
								DOIYN=true
								;;			
		     Eprint)
								EPRINTYN=true
								;;			
esac
done
# if the variables are not set, setf them to FALSE
	if [ -z "$ISBNYN" ];   then ISBNYN=false; fi
	if [ -z "$URLYN" ];    then URLYN=false; fi
	if [ -z "$DOIYN" ];    then DOIYN=false; fi
	if [ -z "$EPRINTYN" ]; then EPRINTYN=false; fi

# builds a variable containing choices.
# if this function was not called BLCOMMON is empty & not printed
# (A) Biblatex (B) markdown
#
if [ $Bibmkd = "LaTeX" ]; then		  
		  BLCOMMON="%Options:common\n"
        BLCOMMON+="\tisbn=$ISBNYN,\n"
        BLCOMMON+="\turl=$URLYN,\n"
        BLCOMMON+="\tdoi=$DOIYN,\n"
        BLCOMMON+="\teprint=$EPRINTYN,\n"
else 		  
        BLCOMMON+="isbn=$ISBNYN,"
        BLCOMMON+="url=$URLYN,"
        BLCOMMON+="doi=$DOIYN,"
        BLCOMMON+="eprint=$EPRINTYN"
fi		  
}

#####################
# PHILSPEC
#####################
# sets options for "philosophy" flavours
# structure: if classic elif modern elif verbose
philspec () { # matches line 615
			 echo $Style
# 'if': matches the end of function
	if		
# classic 
  [ $Style = "philosophy-classic" ] 
	then
# PHSO = philosophy style options
# --------- CLASSIC -------------------
PHSO=`zenity --list --checklist --width=500 --height=225 \
--title="Classic style: see manual, p. 5" \
--text="<b>Options for philosophy-classic</b>" \
--column "True/false" --column "Fields" --column "Comment" --separator=' ' \
FALSE "square" "[] and not () in cit. and in author-year label of bibl."\
FALSE "nodate"  "Prints ��nodate�� when year or date is missing"`
#####
# sets an array from variable
read -a sphilopt <<<$PHSO
# scans the array and sets variables
for i in "${sphilopt[@]}"
do
		  case    $i in
			  square)
								SQUAREYN=true
								;;
	   	  nodate)
					  			NODATEYN=true		  
								;;
esac
done
# if the variables are not set, sets them to FALSE
	if [ -z "$SQUAREYN" ];    then SQUAREYN=false; fi
	if [ -z "$NODATEYN" ];    then NODATEYN=false; fi
# builds a variable containing choices.
# if this function was not called PHSPEC is empty & not printed
# for LaTeX preamble
	if [ $Bibmkd = "LaTeX" ]; then		  
		  PHSPEC="%Options:philosophy-classic\n"
        PHSPEC+="\tsquare=$SQUAREYN,\n"
        PHSPEC+="\tnodate=$NODATEYN,"
	else
# for YAML Markdown block			  
		  PHSPEC="\n"
        PHSPEC+="\tsquare=$SQUAREYN,"
        PHSPEC+="nodate=$NODATEYN,"
	fi		  
# end of CLASSIC style
#
# --------- MODERN -------------------
elif [ $Style = "philosophy-modern" ] ;
	then
# PHSO = philosophy style options
PHSO=`zenity --list --checklist --width=500 --height=225 \
--title="Philosophy-modern style: see manual, p. 5" \
--text="<b>Options for philosophy-modern</b>" \
--column "True/false" --column "Fields" --column "Comment" --separator=' ' \
FALSE "square" "[] and not () in cit. and in author-year label of bibl." \
TRUE "nodate"  "Prints ��nodate�� when year or date is missing" \
FALSE "yearleft"  "Prints date flushed left in bibliography" \
TRUE "restoreclassic"  "Restores 'classic style': see manual, p. 17"`
#####
# sets an array from variable
read -a sphilopt <<<$PHSO
# scans the array and sets variables
for i in "${sphilopt[@]}"
do
		  case    $i in
			  square)
								SQUAREYN=true
								;;
	   	  nodate)
					  			NODATEYN=true		  
								;;
           yearleft)
								YEARLEFTYN=true
								;;			
           restoreclassic)
	RESTORECLASSICYN="true\n%-->use:'\printbibliography[restoreclassic]'"
								;;			
esac
done
# if the variables are not set, sets them to FALSE
	if [ -z "$SQUAREYN" ];    		 then SQUAREYN=false; fi
	if [ -z "$NODATEYN" ];    		 then NODATEYN=false; fi
	if [ -z "$YEARLEFTYN" ];  		 then YEARLEFTYN=false; fi
	if [ -z "$RESTORECLASSICYN" ]; then RESTORECLASSICYN=false; fi
# builds a variable containing choices.
# if this function was not called PHSPEC is empty & not printed
# for LaTeX preamble
	if [ $Bibmkd = "LaTeX" ]; then		  
		  PHSPEC="%Options:philosophy-modern\n"
        PHSPEC+="\tsquare=$SQUAREYN,\n"
        PHSPEC+="\tnodate=$NODATEYN,\n"
        PHSPEC+="\tyearleft=$YEARLEFTYN,\n"
        PHSPEC+="\t%restoreclassic=$RESTORECLASSICYN,\n"
# for YAML markdown block
	else
		  PHSPEC="\n"
        PHSPEC+="\tsquare=$SQUAREYN,"
        PHSPEC+="nodate=$NODATEYN,"
        PHSPEC+="yearleft=$YEARLEFTYN,"
#       PHSPEC+="restoreclassic=$RESTORECLASSICYN,\n"
   fi	  
# end of MODERN style	
#
# --------- VERBOSE -------------------
  elif	[ $Style = "philosophy-verbose" ];
  then
# PHSO = philosophy style options 
PHSO=`zenity --list --checklist --width=500 --height=225 \
--title="Verbose style: see manual, p. 6" \
--text="<b>Options for philosophy-verbose</b>" \
--column "True/false" --column "Fields" --column "Comment" --separator=' '\
   FALSE "latinemph" "Prints ��ibid.�� italicised"\
   FALSE  "commacit"  "Adds comma after shorttitle if follwed by ��cit.��"`
		
#####
# sets an array from variable
read -a sphilopt <<<$PHSO
# scans the array and sets variables
for i in "${sphilopt[@]}"
do
		  case    $i in
			  latinemph)
								LATINEMPHYN=true
								;;
	   	  commacit)
					  			COMMACITYN=true		  
								;;
esac
done
# if the variables are not set, setf them to FALSE
	if [ -z "$LATINEMPHYN" ]; then LATINEMPHYN=false; fi
	if [ -z "$COMMACITYN" ];  then COMMACITYN=false; fi

# builds a variable containing choices.
# if this function was not called BLCOMMON is empty & not printed
# for LaTeX preamble
	if [ $Bibmkd = "LaTeX" ]; then		  
		  PHSPEC="%Options:philosophy-verbose\n"
        PHSPEC+="\tlatinemph=$LATINEMPHYN,\n"
        PHSPEC+="\tcommacit=$COMMACITYN\n"
# for YAML markdown block		  
   else
		  PHSPEC="\n"
        PHSPEC+="\tlatinemph=$LATINEMPHYN,"
        PHSPEC+="commacit=$COMMACITYN,"
	fi		  
fi # end of PHISPEC
}  # matches the beginning of function: 475

###############
# STYLEOPTSET
###############
# sets options style by style
# --> todo -- works only for philosophy!
styleoptset () {
		  if [ $Basicstyle = "philosophy" ];
        then
zenity --info --no-wrap --title="Style ��$Basicstyle�� options" --text=\
"The following windows help setting the common options \n\
of style <b>��$Basicstyle��</b>.\n\
For advanced options please refer to manuals."
# (a) options for basic style philosophy -- 1. true/false
# PHCA = Philosophy Common Options (A)
# FALSE "scauthors" "Author, editor etc. in small caps (cite/biblio)" \
# the above option deleted -- does not work any more? 2019.08.25		  
PHCA=`zenity --list --checklist --width=600 --height=300 --title="Global options
for ��philosophy��: see manual, pp. 14-17" --column "True/false" --column "Options" --column "Comment" --separator=' ' \
FALSE "shorthandintro" "Language-specific expression e.g. ��hence-forth cited as��" \
FALSE "scauthorsbib" "Author, editor etc. in small caps (biblio only)" \
FALSE "scauthorscite" "Author, editor etc. in small caps (cite only)" \
FALSE "lowscauthors" "Author, editor etc. in lowercase small caps" \
FALSE "inbeforejournal" "Prints 'in' before journaltitle (@article)" \
FALSE "annotation" "Prints annotation field in bibliography" \
FALSE "library" "Prints library field in citations and bib."`
#####

# sets an array from variable PHCA
read -a phopt <<<$PHCA
# scans the array and sets variables
for i in "${phopt[@]}"
do
		  case    $i in
			  shorthandintro)
								SHORTHANDINTROYN=true
								;;
#	   	  scauthors)
#					  			SCAUTHORSYN=true -- removed, see above: 2019.08.25
#								;;
	   	  scauthorsbib)
					  			SCAUTHORSBIBYN=true		  
								;;
	   	  scauthorscite)
					  			SCAUTHORSCITEYN=true		  
								;;
	   	  lowscauthors)
					  			LOWSCAUTHORSYN=true		  
								;;
	   	  inbeforejournal)
					  			INBEFOREJOURNALYN=true		  
								;;
	   	  annotation)
					  			ANNOTATIONYN=true		  
								;;
	   	  library)
					  			LIBRARYYN=true		  
								;;
esac
done
# if the variables are not set, setf them to FALSE
	if [ -z "$SHORTHANDINTROYN" ];   then SHORTHANDINTROYN=false; fi
#	if [ -z "$SCAUTHORSYN" ];        then SCAUTHORSYN=false; fi -- removed 2019.08.25 
	if [ -z "$SCAUTHORSBIBYN" ];     then SCAUTHORSBIBYN=false; fi
	if [ -z "$SCAUTHORSCITEYN" ];    then SCAUTHORSCITEYN=false; fi
	if [ -z "$LOWSCAUTHORSYN" ];     then LOWSCAUTHORSYN=false; fi
	if [ -z "$INBEFOREJOURNALYN" ];  then INBEFOREJOURNALYN=false; fi
	if [ -z "$ANNOTATIONYN" ];       then ANNOTATIONYN=false; fi
	if [ -z "$LIBRARYYN" ];          then LIBRARYYN=false; fi

# (b) options for basic style philosophy -- 2. choices
message="$Premsg Choose relatedformat (manual, p. 21): $Pstmsg"
declare -a myarr=(\
		  True  "semicolon" "The ���related��� entry is preceded by a semicolon"\
		  False "parens" "Puts the ���related��� entry in parentheses"\
		  False "brackets" "Same as the previous option but with brackets"
		  );
	rundoselect
	Relatedformat=$mychoice
	resetall
	
message="$Premsg Choose publocformat (manual, p. 14): $Pstmsg"
declare -a myarr=(\
		  True  "publocyear" "Oxford University Press, Oxford 2007"\
		  False "locpubyear" "Oxford, Oxford University Press, 2007"\
		  False "loccolonpub" "Oxford: Oxford University Press, 2007"
		  );
	rundoselect
	Publocformat=$mychoice
	resetall

message="$Premsg Choose volnumformat (manual, p. 15): $Pstmsg"
declare -a myarr=(\
		  True  "plain"   "Journal Title, 5, 8"\
		  False "strings" "Journal Title, vol. 5, n. 8"\
		  False "parens"  "Journal Title (5, 8)"
		  );
	rundoselect
	Volnumformat=$mychoice
	resetall
	
message="$Premsg Choose volumeformat (manual, p. 15): $Pstmsg"
declare -a myarr=(\
		  True  "arabic"  "Book Title, vol. 12"\
		  False "roman"   "Book Title, vol. xii (lowercase)"\
		  False "romansc" "Book Title, vol. x���� (small caps)"\
		  False "Roman"   "Book Title, vol. XII (uppercase)"
		  );
	rundoselect
	Volumeformat=$mychoice
	resetall

message="$Premsg Choose editionformat (manual, p. 15): $Pstmsg"
declare -a myarr=(\
		  True  "arabic"  "Book Title, 3�� ed."\
		  False "roman"   "Book Title, iii ed. (lowercase)"\
		  False "romansc" "Book Title, ������ ed. (small caps)"\
		  False "Roman"   "Book Title, III ed. (uppercase)"\
		  False "superscript"   "Book Title, 2010�� (verbose only)"
		  );
	rundoselect
	Editionformat=$mychoice
	resetall
	philspec
# builds a variable containing true/false and choices.
# if this function was not called PHCOMMON is empty & not printed
# If Biblatex
if [ $Bibmkd = "LaTeX" ]; then
	PHCOMMON="%Options:philosophy-common\n"
# common 'philosophy' true/false	
	PHCOMMON+="\tshorthandintro=$SHORTHANDINTROYN,\n"
#	PHCOMMON+="\tscauthors=$SCAUTHORSYN,\n"
	PHCOMMON+="\tscauthorsbib=$SCAUTHORSBIBYN,\n"
	PHCOMMON+="\tscauthorscite=$SCAUTHORSCITEYN,\n"
	PHCOMMON+="\tlowscauthors=$LOWSCAUTHORSYN,\n"
	PHCOMMON+="\tinbeforejournal=$INBEFOREJOURNALYN,\n"
	PHCOMMON+="\tannotation=$ANNOTATIONYN,\n"
	PHCOMMON+="\tlibrary=$LIBRARYYN,\n"
# common 'philosophy' choices	
	PHCOMMON+="\trelatedformat=$Relatedformat,\n"
	PHCOMMON+="\tpublocformat=$Publocformat,\n"
	PHCOMMON+="\tvolnumformat=$Volnumformat,\n"
	PHCOMMON+="\tvolumeformat=$Volumeformat,\n"
	PHCOMMON+="\teditionformat=$Editionformat,\n"
	else
# if Markdown YAML
# NB: \n\t important: if the field is left empty
# no spurious \n\t will be written to the YAML block
	PHCOMMON="\n\t"
	PHCOMMON+="shorthandintro=$SHORTHANDINTROYN,"
#	PHCOMMON+="scauthors=$SCAUTHORSYN,"
	PHCOMMON+="scauthorsbib=$SCAUTHORSBIBYN,"
	PHCOMMON+="scauthorscite=$SCAUTHORSCITEYN,"
	PHCOMMON+="lowscauthors=$LOWSCAUTHORSYN,"
	PHCOMMON+="inbeforejournal=$INBEFOREJOURNALYN,"
	PHCOMMON+="annotation=$ANNOTATIONYN,"
	PHCOMMON+="library=$LIBRARYYN,"
# common 'philosophy' choices	
	PHCOMMON+="relatedformat=$Relatedformat,"
	PHCOMMON+="publocformat=$Publocformat,"
	PHCOMMON+="volnumformat=$Volnumformat,"
	PHCOMMON+="volumeformat=$Volumeformat,"
	PHCOMMON+="editionformat=$Editionformat,"
	fi
# end of philosophy -- default does not do anything but print a message
	else
zenity --info --width="250" --no-wrap --title "More soon!" --text \
"\n<span color=\"blue\"><b>Additional options for style\
\n\t\t��$Basicstyle��\n\
will be available in version 0.2</b></span>"
#   finalmessage 		  -- canc
	 fi # refer to function beginning to nest IF-S!
}

#############
# SETBIBMD
#############
setbibmd () {
 message="$Premsg 1. LaTeX or Markdown? $Pstmsg"
		  declare -a myarr=(\
		  False "LaTeX"    "LaTeX preamble"\
		  False "Markdown" "Markdown YAML block"\
		  );
	rundoselect
	Bibmkd=$mychoice
	resetall
}

###############
# PRINTALL
###############
# prepares a long variable & prints to a file
# if (1) = Biblatex (2) = markdown
printall () {
# if LaTeX		  
if [ $Bibmkd = "LaTeX" ]; then		  
		  BLOPT="$Csquotes"
        BLOPT+="\\usepackage\n"
        BLOPT+="\t[%\n"
        BLOPT+="\tbackend=$Backend,\n"
        BLOPT+="\tstyle=$Style,\n"
        BLOPT+="\tsorting=$Sorting,\n"
		  BLOPT+="$BLCOMMON"
		  BLOPT+="$PHCOMMON"
		  BLOPT+="$PHSPEC"
        BLOPT+="\t]%\n"
        BLOPT+="{biblatex}\n"
        BLOPT+="$Gesamtbibfile\n"
# if YAML Markdown block
else
		  BLOPT="citation_package:\t biblatex\n"
		  BLOPT+="biblio-style:\t $Style,backend=$Backend,\n"
		  BLOPT+="biblatexoptions:\t |\n\t
		  sorting=$Sorting,$BLCOMMON,$PHCOMMON,$PHSPEC"
#		  BLOPT+="$BLCOMMON,$PHCOMMON,$PHSPEC\n"
		  BLOPT+="\nbibliography:\n $Gesamtbibfile\n"
#		  BLOPT="${BLOPT//,,/,}" # change double comma to single -- USELESS
#		  BLOPT="${BLOPT//'\n\n'/'\n'}" # change double \n to single -- USELESS
fi		  
			
        printf %b $BLOPT >biblatex.opt 
# now the file is read by zenity		  
  zenity  --width="550" --height="600" --text-info --timeout 20 \
--title="Editable biblatex configuration" --editable \
			 --font=mono \
			 --filename=biblatex.opt
}

# Sets formatting codes for window titles
Premsg="<big><b>"
Pstmsg="</b></big>"

################
# Main
################
	checkdep					# check dependencies
	openprogramme			# info, version
	setbibmd					# sets Biblatex or MD
	csquotes					# sets csquotes options
 	backend					# sets backend
	setbasicstyle			# sets basic style
	setfinalstyle			# sets final style
	setsorting				# sets sorting order
	generaloptyn			# additional general options y/n
	styleoptyn				# additional style options y/n

################
# History
################
#								*	0.1c
#								*	Biblio PATH was hardcoded: bug, corrected
#  2019.08.16				*	0.1b
#								*  added author-title in general choice
#								*	Ref. to philosophy manual updated
#	2015.10.16				*	0.1a
#								*	Added editionformat=superscript (philosophy-verbose)
#								*	Minor improvements (e.g. English wording)
#								*	New license: LPPL
#	2015.08.25				First complete version (0.1)
#	2015.08.24				Beta version
#	2015.08.23				Alfa version
#	2015.08.20				First draft

################
# TODO
################
#	Options for more styles
#	Uniform windows sizes or not?

################
# BUGS
################
#	printf complains about missing unicode digit for \u
#		but see e.g.