#! /usr/local/bin/snobol4
#########################
#       pangreg.sno
#       version 0.3
#  30.10.2025
#  (c) guido.milanese@unicatt.it
# Licence: MIT
# Please see <https://opensource.org/license/mit>
#########################

*	Include files	
-include "args.inc"
-include "bq.sno"
-include "delete.inc"
-include "expro.inc"
-include "host.inc"
-include "ioerror.inc"
-include "newline.inc"
-include "grepl.inc"
-include "repl.inc"
-include "remove.inc"
-include "xp.inc"

*********************************************************
* SET VARIABLES
*********************************************************
	NL = newline()
	Guierrormsg1 = "Please choose a filename, e.g. <b>myfile.gabc</b>" NL 
+	"Extension must be <b>gabc</b>" NL NL TAB "Exiting"


*********************************************************
*	INTERNAL FUNCTIONS
*********************************************************

*-- Function GUIERRORMSG
*-- Error messages for GUI version
            define("guierrormsg(Pass)")	:(guierrormsg_end)
guierrormsg
  bq("zenity --error --title=ERRASTI! --width=400 --height=100 --text=" '"' Pass '"')
guierrormsg_rt										:(return)
guierrormsg_end

*-- Function OPENPROGRAMME
            define("openprogramme()")   
	Hugefont   = 'font=\"Times Bold 36\"'
	Largefont  = 'font=\"Times Bold 24\"'
	Mediumfont  = 'font=\"Times Bold 18\"'
	Normalfont = 'font=\"Times Bold 14\"'
	Smallfont  = 'font=\"Times Bold 12\"'
	:(openprogramme_end)

openprogramme
	Line1 = '\n\n\n\t\t\t' '<span ' Hugefont ' color=\"green\">PANGREG</span>\n\n'
   Line2 = '<span ' Largefont ' color=\"blue\">\t\t\tConversion among Gregorian Chant\n \t\t\tencodings and notations</span>\n\n'
   Line3 = '<span ' Normalfont ' color=\"blue\"><b>\t\t\tMICHAEL PRIN2022\t\t </b> </span>\n'
   Line4 = '<span ' Normalfont ' color=\"blue\"><b>\t\t\tMultimedial Italian CHant ArchivE\n\t\t\tof Liturgical melodies and texts (14th-18th centuries)\t\t </b> </span>\n\n'
   Line5 = '\t\tversion 0.4, 2025.06.24\n'
   Line6 = '\t\t<i>(c) 2024-2025 Guido Milanese -- guido.milanese@unicatt.it</i>\n\t\tLicense: MIT License\n'
   Line7 = '\n\n\n<span ' Mediumfont ' color=\"green\"><b><i>\t\t\tHit ESC to leave, OK or Return to continue</i></b> </span>\n\n'
   Errorline = '<span ' Normalfont ' color=\"blue\"><b>\t\t\tLeaving PANGREG</b> </span>\n'

openprogramme_bg
* changed windows size 22.03.2025
*	cls(clscomm)
	Openchoice = bq('zenity --info --width="800" --height="400" --no-wrap --timeout 100 --title="PANGREG" --text "' 
+  Line1 Line2 Line3 Line4 Line5 Line6  Line7 '" ; echo $?')
* if user hits "ESC" exits
    (
+     ~(EQ(Openchoice,0)) 
+     (goaway = execute('zenity --info --title="Exiting" --text "<span color=\"blue\"> <big>Leaving PANGREG</big></span>"'))
+   ) :s(end)


openprogramme_rt      :(return)
openprogramme_end

*#####################
* Function CONVCHOICE
*#####################
*-- Core of the programme
            define("convchoice(),Schoice_ret")   :(convchoice_end)
convchoice
convchoice_bg
		choice_ret = ''
        choice_ret = bq('zenity  --list  --title="PANGREG CHOICE" --text "Pick your choice" --height 340 --width 254 --radiolist --column "Pick" --column "Choice" FALSE "GABC --> ABC" FALSE "ABC --> Volpiano code" FALSE "ABC --> Volpiano HTML" FALSE "ABC --> GABC" FALSE "Volpiano code --> GABC" FALSE "Volpiano HTML > GABC" FALSE "XML --> ANY" FALSE "Read the license" FALSE "Quit the programme"')
* if no choice
* size of choice
	Schoice_ret = size(choice_ret)
* if it is GT than 0, go on
* if it is 0 sets CHR = 10 and returns
	(~(GT(Schoice_ret,0)) (CHR = 10)) :s(convchoice_rt)f(conv_values)
* if choice
conv_values
        ((choice_ret ? "GABC --> ABC")				   (CHR = '1')) 
        ((choice_ret ? "ABC --> Volpiano code")	(CHR = '2'))  
        ((choice_ret ? "ABC --> Volpiano HTML")	(CHR = '3'))  
        ((choice_ret ? "ABC --> GABC")				   (CHR = '4'))  
        ((choice_ret ? "(G)ABC --> XML")				(CHR = '5'))  
        ((choice_ret ? "ANY --> XML")				   (CHR = '6'))  
        ((choice_ret ? "XML --> ANY")			      (CHR = '7'))  
        ((choice_ret ? "Read the license")			(CHR = '8')) 
        ((choice_ret ? "Quit the programme")  		(CHR = '9'))  
convchoice_rt
			convchoice = CHR :(return)
convchoice_end

*********************************************************
*-- Openfile
*-- Opens a file according to the required choice
*-- and runs the SNOBOL programme required
*********************************************************
            define("openfile(variety)Par1,Par2")   :(openfile_end)
openfile
openfile_bg
	((ident(variety,1)) (extension = "gabc"))
	((ident(variety,2)) (extension = "abc"))
	((ident(variety,3)) (extension = "abc"))
	((ident(variety,4)) (extension = "abc"))
	(
+	~(INFILE = bq('zenity --file-selection --filename="" --width=50 --height=50 --title "Choose a file" --file-filter="*."' extension ' --file-filter="All files | *"'))
+	(guierrormsg(Guierrormsg1))
+	)	:s(checkfiles_usgmsg)
* no filename?
	( (leq(INFILE,)) (guierrormsg(Guierrormsg1))) :s(checkfiles_usgmsg)
* wrong filename?
	(
+	~(INFILE ? '.' extension rpos(0))
+  (guierrormsg(Guierrormsg1))
+	) :s(checkfiles_usgmsg)
************************************
* Options for the various operations
************************************
	:($("options_" Variety))
* GABCTOABC
options_1
	Opt_Stems = '0'
	Opt_Utf8  = 'NO'
		  file_options = bq('zenity  --list  --title="OPTIONS" --text "Pick your choice" --height 340 --width 254 --checklist --column "Pick" --column "Choice" TRUE "stems" TRUE "UTF8"') :(openfile_rt)
* ABCTOVOLPIANO
options_2
	file_options = 'C' :(openfile_rt)
* ABCTOHTML
options_3
	file_options = 'H' :(openfile_rt)
* ABCTOGABC
options_4
       file_options = bq('zenity  --list  --title="KEYS" --text "Pick your choice" --height 270 --width 150 --checklist --column "Pick" --column "Choice" FALSE "c1" FALSE "c2" FALSE "c3" FALSE "c4" FALSE "f2" FALSE "f3"') :(openfile_rt)
openfile_rt   openfile   =  INFILE "$$$" file_options :(return)
checkfiles_usgmsg :(freturn)	
openfile_end

************************
*-- Function OPON
************************
*-- Operate on one of the choices
            define("opon(Operation)Par1,Par2")   :(opon_end)
opon
opon_bg
	Terminal = "Opon " Operation
	:($("opon_" Operation))

* 1 -- GABCTOABC
opon_1
	Filename_and_options = openfile(1) :f(freturn)
	Filename_and_options ? break("$$$") . INFILE len(3) rem . File_options
	((File_options ? "stems") (Opt_Stems = ' '))
	((File_options ? "UTF8")  (Opt_UTF8 = ' '))
	bq("snobol4 gabctoabc.sno " INFILE ' ' Opt_Stems ' ' Opt_UTF8) :s(opon_rt)f(freturn)
* 2 -- ABCTOVOLPIANO_CODE
opon_2	
	Filename_and_options = openfile(2) :f(freturn)
	Filename_and_options ? break("$$$") . INFILE len(3) rem . File_options
	bq("snobol4 abctovol.sno " INFILE ' ' File_options) :s(opon_rt)f(freturn)
* 3 -- ABCTOHTML
opon_3	
	Filename_and_options = openfile(3) :f(freturn)
	Filename_and_options ? break("$$$") . INFILE len(3) rem . File_options
	bq("snobol4 abctovol.sno " INFILE ' ' File_options) :s(opon_rt)f(freturn)
opon_4
	Filename_and_options = openfile(4) :f(freturn)
	Filename_and_options ? break("$$$") . INFILE len(3) rem . File_options
	bq("snobol4 abctogabc.sno " INFILE ' ' File_options) :s(opon_rt)f(freturn)
opon_5	
opon_6	
opon_7	

opon_rt   opon   =  Pass                :(return)
opon_end


*#####################
* Function DOCHOICE
*#####################
*-- Executes choice
* CH is defined as LOCAL so it gets redefined at any call 
* of the function and not passed to the calling programme
            define("dochoice(CHC),CH")   :(dochoice_end)
dochoice
dochoice_bg
			CH = CHC
			(ident(CHC) (CHC = 10)) ;* if empty sets to 10
        :($("choice_" CH))
*choice_1	terminal = CH :(dochoice_rt)
* 1 - GABCTOABC
choice_1
	opon(1) :(dochoice_rt)
* choice_2	terminal = CH :(dochoice_rt)
choice_2
	opon(2) :(dochoice_rt)
choice_3
	opon(3) :(dochoice_rt)
choice_4
	opon(4) :(dochoice_rt)
choice_5	terminal = CH :(dochoice_rt)
choice_6	terminal = CH :(dochoice_rt)
choice_7	terminal = CH :(dochoice_rt)
* 8 - License
choice_8
	terminal = CH
	bq('zenity --text-info --height 470 --width 729 --title="MIT LICENSE" --filename="license-MIT.txt"')  :(return) 
* 9 - Quit
choice_9	terminal = CH :s(freturn)
* 10 No choice
choice_10	terminal = CH :(return)

dochoice_rt
	dochoice = CH	:(return)
dochoice_end


***********************
* MAIN
***********************
	openprogramme()
Choice	Choice =
         Choice = convchoice()
	dochoice(Choice) :s(Choice)f(end)
end

