[macro-phraseman]
;
; Phrase/Greeting Manager for Asterisk (tested on v1.2.1)
;
; Allows you to record, review and delete phrases/greetings
; in a specified folder under /var/lib/asterisk/sounds
;
; When recording, it uses a temporary file as the record
; destination. Once you are satisfied with the greeting
; only then will the macro overwrite an existing file.
;
; Macro arguments:
; 	${ARG1} - directory under /var/lib/asterisk/sounds
;
exten => s,1,Answer
exten => s,n,Set(PhraseUser=${ARG1})
exten => s,n,Set(PhraseTmpID=${CHANNEL:-4})
exten => s,n,Wait(1)
exten => s,n,Playback(gm-welcome)
exten => s,n,Goto(s,mainmenu)

; mainmenu
exten => s,n(mainmenu),NoOp,-= GM: Main Menu =-
exten => s,n,Set(TIMEOUT(digit)=5)
exten => s,n,Set(TIMEOUT(response)=10)
exten => s,n,Read(MENUCHOICE|gm-main-menu)
exten => s,n,GotoIf($["${MENUCHOICE}" > "3"]?s|mainmenuinvalid)
exten => s,n,Wait(1)
exten => s,n,Read(PHRASEID|gm-greeting-number-pound)
exten => s,n,GotoIf($["${MENUCHOICE}" = "1"]?s|record)
exten => s,n,GotoIf($["${MENUCHOICE}" = "2"]?s|review)
exten => s,n,GotoIf($["${MENUCHOICE}" = "3"]?s|delete)
exten => s,n,Goto(s,mainmenu)

; mainmenuinvalid
exten => s,n(mainmenuinvalid),NoOp,-= GM: Invalid Menu Choice =-
exten => s,n,Playback(gm-invalid-menu-choice)
exten => s,n,Wait(1)
exten => s,n,Goto(s,mainmenu)

exten => s,n(record),NoOp,-= GM: Record Greeting =-
exten => s,n,Playback(gm-begin-recording)
exten => s,n,Wait(2)
exten => s,n,Record(${PhraseUser}/${PHRASEID}-${PhraseTmpID}.gsm)
exten => s,n,Wait(1)
exten => s,n,Playback(${PhraseUser}/${PHRASEID}-${PhraseTmpID})
exten => s,n,Wait(1)
exten => s,n,Read(OPTION|gm-confirm-save)
exten => s,n,GotoIf($["${OPTION}" = "1"]?s|save)
exten => s,n,Goto(s|deltmp)

exten => s,n(save),NoOp,-= GM: Save Greeting =-
exten => s,n,System(mv /var/lib/asterisk/sounds/${PhraseUser}/${PHRASEID}-${PhraseTmpID}.gsm /var/lib/asterisk/sounds/${PhraseUser}/${PHRASEID}.gsm)
exten => s,n,Playback(gm-greeting-saved)
exten => s,n,Wait(1)
exten => s,n,Goto(s,mainmenu)

exten => s,n(deltmp),NoOp,-= GM: Delete temporary file =-
exten => s,n,System(rm -f /var/lib/asterisk/sounds/${PhraseUser}/${PHRASEID}-${PhraseTmpID}.gsm)
exten => s,n,GotoIf($["${OPTION}" = "2"]?s|record)	; jump back if we are going to rerecord the greeting
exten => s,n,Playback(gm-greeting-not-saved)
exten => s,n,Wait(1)
exten => s,n,Goto(s,mainmenu)

exten => s,n(review),NoOp,-= GM: Review Greeting =-
exten => s,n,System(test -f /var/lib/asterisk/sounds/${PhraseUser}/${PHRASEID}.gsm)
exten => s,n,GotoIf($["${SYSTEMSTATUS}" = "APPERROR"]?s|greetingdoesnotexist)
exten => s,n,Playback(${PhraseUser}/${PHRASEID})
exten => s,n,Wait(1)
exten => s,n,Goto(s,mainmenu)

exten => s,n(delete),NoOp,-= GM: Delete Greeting =-
exten => s,n,System(test -f /var/lib/asterisk/sounds/${PhraseUser}/${PHRASEID}.gsm)
exten => s,n,GotoIf($["${SYSTEMSTATUS}" = "APPERROR"]?s|greetingdoesnotexist)
exten => s,n,Playback(${PhraseUser}/${PHRASEID})
exten => s,n,Wait(1)
exten => s,n,Read(OPTION|gm-confirm-delete)
exten => s,n,GotoIf($["${OPTION}" = "1"]?s|deleted)
exten => s,n,Playback(gm-greeting-not-deleted)
exten => s,n,Wait(1)
exten => s,n,Goto(s,mainmenu)

exten => s,n(deleted),NoOp,-= GM: Deleted Greeting =-
exten => s,n,System(rm -f /var/lib/asterisk/sounds/${PhraseUser}/${PHRASEID}.gsm)
exten => s,n,Playback(gm-greeting-deleted)
exten => s,n,Wait(1)
exten => s,n,Goto(s,mainmenu)

exten => s,n(greetingdoesnotexist),NoOp,-= GM: Greeting Does Not Exist =-
exten => s,n,Playback(gm-greeting-does-not-exist)
exten => s,n,Goto(s,mainmenu)
