#!/bin/bash
endmenu() {
echo "Bye bye."
exit 0;
}
getDestinationFile()
{
retValue=$(zenity --file-selection --save --file-filter='PDF files (pdf) | *.pdf' --title="Select a PDF file (Destination)")
case $? in
0)
destinationPDFFile=$retValue
;;
esac
}
getOriginFiles()
{
retValue=$(zenity --file-selection --multiple --file-filter='PDF files (pdf) | *.pdf' --title="Select a PDF files to merge")
case $? in
0)
originPDFFiles=$retValue
;;
esac
}
showOriginFiles()
{
declare -i index
index=1
mylistFiles=()
tmpmylistFiles=()
IFS='|'
read -ra files <<< "$originPDFFiles"
for file in "${files[@]}";
do
tmpmylistFiles+=("$file" )
done
readarray -t tmpmylistFiles < <(printf '%s\n' "${tmpmylistFiles[@]}" | sort)
for file in "${tmpmylistFiles[@]}";
do
mylistFiles+=("$index" "$file" )
let index++
done
retValue=$(zenity --list --hide-header --height=400 --width=700 --title='Files To Merge' --column=ID --column=Files "${mylistFiles[@]}" )
echo $retValue
}
mergeFiles()
{
mylistFiles=()
IFS='|'
continua=1
if [ -z "${destinationPDFFile}" ]; then
zenity --info --text="No destination file selected."
continua=0
fi
if [ $continua == 1 ]; then
if [ -z "${originPDFFiles}" ]; then
zenity --info --text="No select files to merge."
continua=0
fi
fi
if [ $continua == 1 ]; then
read -ra files <<< "$originPDFFiles"
for file in "${files[@]}";
do
echo $file
mylistFiles+=("$file" )
let index++
done
readarray -t mylistFiles < <(printf '%s\n' "${mylistFiles[@]}" | sort)
qpdf --empty --pages "${mylistFiles[@]}" -- "${destinationPDFFile}"
fi
}
mainmenu()
{
choice=$(zenity --height=250 --list --hide-header --text "Select action" --ok-label="Select" --cancel-label="Exit" \
--title "Main Menu - Merge file" --radiolist --column="ID" --column="Action" \
1 "File Destination" 2 "Add Files To Merge" 3 "View Files To Merge" 4 "Merge" )
case $choice in
"File Destination")
getDestinationFile
mainmenu
;;
"Add Files To Merge")
getOriginFiles
mainmenu
;;
"View Files To Merge")
showOriginFiles
mainmenu
;;
"Merge")
mergeFiles
mainmenu
;;
esac
}
mainmenu