diff --git a/install.sh b/install.sh
index 8a8db35..e78fc18 100755
--- a/install.sh
+++ b/install.sh
@@ -8,12 +8,12 @@ echo
if [ $(logname) == root ]; then
echo "Don't run this as root"
- echo ""
+ echo
exit 1
fi
if [ $(whoami) != root ]; then
echo "Use sudo to become root."
- echo ""
+ echo
exit 1
fi
@@ -22,18 +22,18 @@ fi
# all_users=$(awk -F: '($3>=1000)&&($1!="nobody"){print $1}' /etc/passwd)
check_dependencies() {
- local missing=0
- for cmd in pdfinfo zenity lp seq paste awk grep; do
- if ! command -v "$cmd" >/dev/null 2>&1; then
- LOG "Error: Required command '$cmd' not found."
- missing=1
- fi
- done
- if (( missing )); then
- echo "Missing required dependencies. See $LogFile for details." >&2
- exit 1
+ local missing=0
+ for cmd in pdfinfo zenity lp seq paste awk grep; do
+ if ! command -v "$cmd" > /dev/null 2>&1; then
+ echo "Error: Required command '$cmd' not found."
+ missing=1
fi
- echo "Finished dependency check."
+ done
+ if ((missing)); then
+ echo "Missing required dependencies." >&2
+ exit 1
+ fi
+ echo "Finished dependency check."
}
check_dependencies
@@ -44,9 +44,8 @@ echo "These are your installed printers:"
echo
declare printers_array
i=0
-for p in $all_printers
-do
- i=$(( $i + 1 ))
+for p in $all_printers; do
+ i=$(($i + 1))
echo $i. $p
printers_array[$i]=$p
done
@@ -59,8 +58,7 @@ first_printer=${printers_array[$chosen_printer]}
function setup_duplexer {
first_printer=$1
- if [ -z "$first_printer" ]
- then
+ if [ -z "$first_printer" ]; then
echo "No printer submitted. You trickster!"
exit 1
else
@@ -117,7 +115,7 @@ function setup_duplexer {
cp -praf /etc/cups/ppd/$first_printer.ppd /etc/cups/ppd/Manual_Duplexer_$first_printer.ppd
-# sed -i 's/"0 hpcups"/"duplex_print_filter"/g' /etc/cups/ppd/Manual_Duplexer_$first_printer.ppd
+ # sed -i 's/"0 hpcups"/"duplex_print_filter"/g' /etc/cups/ppd/Manual_Duplexer_$first_printer.ppd
sed -i '/^*cupsFilter/d' /etc/cups/ppd/Manual_Duplexer_$first_printer.ppd
echo '*cupsFilter: "application/pdf 0 duplex_print_filter"' >> /etc/cups/ppd/Manual_Duplexer_$first_printer.ppd
@@ -126,7 +124,6 @@ function setup_duplexer {
echo '*cupsFilter: "application/vnd.cups-raster 0 duplex_print_filter"' >> /etc/cups/ppd/Manual_Duplexer_$first_printer.ppd
echo '*cupsFilter: "application/vnd.cups-pdf 0 duplex_print_filter"' >> /etc/cups/ppd/Manual_Duplexer_$first_printer.ppd
-
sleep 1
service cups restart &> /dev/null
@@ -154,13 +151,12 @@ echo
echo "(Y/N) followed by [ENTER]:"
read approve
-if [ $approve == "Y" ]
-then
+if [ $approve == "Y" ]; then
echo
echo
echo
- setup_duplexer $first_printer
- else
+ setup_duplexer $first_printer
+else
echo
echo "Nothing was changed. Maybe use capital Y ?"
echo
diff --git a/manual_duplex_linux_1.0.tar.gz b/manual_duplex_linux_1.0.tar.gz
deleted file mode 100644
index bea03c6..0000000
Binary files a/manual_duplex_linux_1.0.tar.gz and /dev/null differ
diff --git a/usr/lib/cups/backend/duplex-print b/usr/lib/cups/backend/duplex-print
old mode 100755
new mode 100644
index 044fcbb..6c4d4aa
--- a/usr/lib/cups/backend/duplex-print
+++ b/usr/lib/cups/backend/duplex-print
@@ -8,37 +8,23 @@
# Return even page numbers from 1..num_pages
even_pages() {
- num_pages=$1
- i=2
- out=
- while [ "$i" -le "$num_pages" ]; do
- out="${out}${i},"
- i=$((i+2))
- done
- printf '%s\n' "${out%,}"
-}
-
-get_running_user() {
- if [[ -n "$user" ]]; then
- echo "$user"
- return
- fi
- login=$(logname 2>/dev/null)
- if [[ -n "$login" ]]; then
- echo "$login"
- return
- fi
- echo "$(whoami)"
+ num_pages=$1
+ i=2
+ out=
+ while [ "$i" -le "$num_pages" ]; do
+ out="${out}${i},"
+ i=$((i + 2))
+ done
+ printf '%s\n' "${out%,}"
}
#Uncomment this to log to /var/log/cups/error_log
LogFile=/var/log/cups/error_log
LOG() {
- echo "$(date) --- $@" >> $LogFile
+ echo "$(date) --- $@" >> $LogFile
}
# Output "Device Discovery" information on stdout:
-if test "$#" = "0"
-then
+if [ "$#" = "0" ]; then
echo 'direct duplex-print "Unknown" "(!) Manual_duplexer unit for linux. (!)"'
exit 0
fi
@@ -62,198 +48,197 @@ filename="/var/spool/cups/duplex/$job.duplex"
short_edge=0
long_edge=0
-LOG ---
-LOG Starting duplexer job
-LOG "Script called by $user and runs as $(whoami) with logname $(logname)"
+donation_text="Finding my work useful? Click here to buy me a coffee."
+printer_icon="/usr/share/manual_duplex_linux/printer.png"
+
+LOG "---"
+LOG "Starting duplexer job for user $user"
lp_args=
-for opt in $options
-do
- lp_args="$lp_args -o $opt"
+for opt in $options; do
+ lp_args="$lp_args -o $opt"
done
#test if file exists
-while [ ! -f "$filename" ]
-do
+while [ ! -f "$filename" ]; do
sleep 1
done
page_count=$(pdfinfo "$filename" | grep Pages | awk '{print $2}')
LOG "page count: $page_count"
-# Export display for zenity
-export DISPLAY=$(who | grep "^$user" | awk '{print $NF}' | grep -oE ':[0-9]+')
+# Export display for zenity
+pid=$(pgrep -u $user Xwayland | head -n1)
+if [ -n $pid ]; then
+ eval "$(
+ tr '\0' '\n' < /proc/$pid/environ |
+ grep -E '^(DISPLAY|WAYLAND_DISPLAY|XAUTHORITY|XDG_RUNTIME_DIR)='
+ )"
+ export DISPLAY WAYLAND_DISPLAY XAUTHORITY XDG_RUNTIME_DIR
+else
+ export DISPLAY=$(who | grep "^$user" | awk '{print $NF}' | grep -oE ':[0-9]+')
+fi
+
# Calculate width (Assuming you want one-third of the screen width)
screen_width=$(sudo -u "$user" xdpyinfo -display "$DISPLAY" | awk '/dimensions:/ {print $2}' | cut -d'x' -f1)
-zenity_width=$(( screen_width / 3 ))
-
-if [ $(( $page_count )) -gt 1 ]; then
- #show user interface
- LOG "$(date) --- Will Show user interface to get page_edge."
- LOG "$(date) --- Running on display number $DISPLAY"
- # Display the Zenity dialog
- button_clicked=$(sudo --user="$user" zenity --question \
- --switch \
- --title="$title" \
- --text="Which edge to choose for paper binding?\n\nFinding my work useful? Click here to buy me a coffee." \
- --icon-name="document-print" \
- --window-icon="/usr/share/manual_duplex_linux/printer.png" \
- --extra-button="Short_Edge" \
- --extra-button="Long_Edge" \
- --extra-button="Cancel" \
- --display="$DISPLAY" \
- --width="$zenity_width" 2>>"$LogFile")
-
- # Capture the exit status
- last_command_return=$?
- # Log that the user interface was shown
- LOG "$(date) --- User interface has been shown. Zenity returned >> $last_command_return << and button_clicked >> $button_clicked << "
-
- # Handle the user's response
- case "$button_clicked" in
- Short_Edge)
- LOG "$(date) --- Short edge has been chosen."
- short_edge=1
- ;;
- Long_Edge)
- LOG "$(date) --- Long edge has been chosen."
- long_edge=1
- ;;
- Cancel)
- LOG "$(date) --- Operation cancelled by user."
- exit 1
- ;;
- *)
- LOG "$(date) --- An unexpected error occurred. Exit code: $last_command_return."
- exit 1
- ;;
- esac
+zenity_width=$((screen_width / 3))
+
+if [ "$page_count" -gt 1 ]; then
+ #show user interface
+ LOG "Showing user interface on DISPLAY=$DISPLAY to get page_edge."
+ # Display the Zenity dialog
+ button_clicked=$(sudo --user="$user" zenity --question \
+ --switch \
+ --title="$title" \
+ --text="Which edge to choose for paper binding?\n\n$donation_text" \
+ --icon="$printer_icon" \
+ --extra-button="Short_Edge" \
+ --extra-button="Long_Edge" \
+ --extra-button="Cancel" \
+ --width=$zenity_width 2>> "$LogFile")
+
+ # Capture the exit status
+ last_command_return=$?
+ # Log that the user interface was shown
+ LOG "User interface has been shown. Zenity returned >> $last_command_return << and button_clicked >> $button_clicked <<"
+
+ # Handle the user's response
+ case "$button_clicked" in
+ Short_Edge)
+ LOG "Short edge has been chosen."
+ short_edge=1
+ ;;
+ Long_Edge)
+ LOG "Long edge has been chosen."
+ long_edge=1
+ ;;
+ Cancel)
+ LOG "Operation cancelled by user."
+ exit 1
+ ;;
+ *)
+ LOG "An unexpected error occurred. Exit code: $last_command_return."
+ exit 1
+ ;;
+ esac
fi
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
#if only one page, just send the printing command
-if [ $(( $page_count )) -eq 1 ]; then
- LOG "printing ONE page"
+if [ "$page_count" -eq 1 ]; then
+ LOG "printing ONE page"
- lp -s -d $USE_PRINTER -n "$numcopies" $lp_args -o collate=true -t "$title"-even "$filename" >> "$LogFile"
+ lp -s -d $USE_PRINTER -n "$numcopies" $lp_args -o collate=true -t "$title"-even "$filename" >> "$LogFile"
#if only 2 pages, print odd pages first and show user interface
-elif [ $(( $page_count )) -eq 2 ]; then
- LOG 2 pages were sent
- LOG Printing odd pages.
-
- lp -s -d $USE_PRINTER -n "$numcopies" $lp_args -o page-set=odd -o collate=true -o outputorder=normal -t "$title"-odd "$filename"
-
- #show user interface
- LOG Will Show user interface.
- LOG Running on display number "$DISPLAY"
- sudo --user="$user" zenity --question \
- --title="$title" \
- --text="Flip and reinsert the entire paper stack when printing has finished,\n\n then press Procceed.\n\n
- Finding my work usefull? Click here to buy me a cofee." \
- --icon-name="document-print" \
- --window-icon="/usr/share/manual_duplex_linux/printer.png" \
- --ok-label="Proceed" \
- --cancel-label="Cancel" \
- --display="$DISPLAY" \
- --width="$zenity_width" 2>>"$LogFile"
-
- # Capture the exit status
- last_command_return=$?
- # Log that the user interface was shown
- LOG "$(date) --- User interface has been shown. Zenity returned >> $last_command_return << and button_clicked >> $button_clicked << "
-
- if (($last_command_return==0)); then
- LOG printing even pages
- if [ $(( $short_edge )) -eq 1 ]; then
- lp -s -d $USE_PRINTER -n "$numcopies" $lp_args -o page-ranges=2 -o orientation-requested=5 -o collate=true -o outputorder=normal -t "$title"-even "$filename"
- else
- lp -s -d $USE_PRINTER -n "$numcopies" $lp_args -o page-ranges=2 -o collate=true -o outputorder=normal -t "$title"-even "$filename"
- fi
+elif [ "$page_count" -eq 2 ]; then
+ LOG "2 pages were sent"
+ LOG "Printing odd pages."
+
+ lp -s -d $USE_PRINTER -n "$numcopies" $lp_args -o page-set=odd -o collate=true -o outputorder=normal -t "$title"-odd "$filename"
+
+ #show user interface
+ LOG "Showing user interface on DISPLAY=$DISPLAY."
+ sudo --user="$user" zenity --question \
+ --title="$title" \
+ --text="Flip and reinsert the entire paper stack when printing has finished,\n\n then press Procceed.\n\n$donation_text" \
+ --icon="$printer_icon" \
+ --ok-label="Proceed" \
+ --cancel-label="Cancel" \
+ --width=$zenity_width 2>> "$LogFile"
+
+ # Capture the exit status
+ last_command_return=$?
+ # Log that the user interface was shown
+ LOG "User interface has been shown. Zenity returned >> $last_command_return <<"
+
+ if [ "$last_command_return" -eq 0 ]; then
+ LOG "printing even pages"
+ if [ "$short_edge" -eq 1 ]; then
+ lp -s -d $USE_PRINTER -n "$numcopies" $lp_args -o page-ranges=2 -o orientation-requested=5 -o collate=true -o outputorder=normal -t "$title"-even "$filename"
else
- LOG User aborted print job
- rm "$filename"
- exit 0
+ lp -s -d $USE_PRINTER -n "$numcopies" $lp_args -o page-ranges=2 -o collate=true -o outputorder=normal -t "$title"-even "$filename"
fi
+ else
+ LOG "User aborted print job"
+ rm "$filename"
+ exit 0
+ fi
#if more than 2 pages, print odd pages first and show user interface
-elif [ $(( $page_count )) -gt 2 ]; then
- LOG More than 2 pages were sent
- LOG Printing odd pages. it is important for these to be printed first. Do not change this.
-
- lp -s -d $USE_PRINTER -n "$numcopies" $lp_args -o page-set=odd -o collate=true -o outputorder=normal -t "$title"-odd "$filename"
-
- #show user interface
- LOG Will Show user interface.
-
- LOG Running on display number $DISPLAY
- sudo --user="$user" zenity --question \
- --title="$title" \
- --text="Flip and reinsert the entire paper stack when printing has finished,\n\n then press Procceed.\n\n
- Finding my work usefull? Click here to buy me a cofee." \
- --icon-name="document-print" \
- --window-icon="/usr/share/manual_duplex_linux/printer.png" \
- --ok-label="Proceed" \
- --cancel-label="Cancel" \
- --display="$DISPLAY" \
- --width=$zenity_width 2>>"$LogFile"
-
- # Capture the exit status
- last_command_return=$?
- # Log that the user interface was shown
- LOG "$(date) --- User interface has been shown. Zenity returned >> $last_command_return << and button_clicked >> $button_clicked << "
-
- if (($last_command_return==0)); then
- LOG printing even pages
-
- if (( page_count % 2 != 0 )); then
- LOG numpages is odd: one blank page will be printed after each copy.
- # Multiple print jobs, otherwise external app is needed as pdftk
- i=0
- while (( i < $numcopies )); do
- if [ $(( $short_edge )) -eq 1 ]; then
- lp -s -d $USE_PRINTER $lp_args \
- -o page-ranges="$(even_pages "$page_count")" \
- -o sides=one-sided -o orientation-requested=5 -o collate=true -o outputorder=normal -t "$title"-even \
- "$filename"
- else
- lp -s -d $USE_PRINTER $lp_args \
- -o page-ranges="$(even_pages "$page_count")" \
- -o sides=one-sided -o collate=true -o outputorder=normal -t "$title"-even \
- "$filename"
- fi
- lp -d $USE_PRINTER $SCRIPT_DIR/blank.pdf
- (( i++ ))
- done
- else
- LOG numpages is even: just one printjob for all copies
- # Send to printer
- if [ $(( $short_edge )) -eq 1 ]; then
- lp -s -d $USE_PRINTER \
- -n "$numcopies" $lp_args \
- -o sides=one-sided \
- -o orientation-requested=5 \
- -o collate=true \
- -o outputorder=normal \
- -o page-ranges="$(even_pages "$page_count")" \
- -t "$title"-even \
- "$filename"
- else
- # a printat 3 even si a scos 1 goală
- lp -s -d $USE_PRINTER \
- -n "$numcopies" $lp_args \
- -o page-ranges="$(even_pages "$page_count")" \
- -o sides=one-sided -o collate=true -o outputorder=normal -t "$title"-even \
- "$filename"
- fi
- fi
+elif [ "$page_count" -gt 2 ]; then
+ LOG "More than 2 pages were sent"
+ LOG "Printing odd pages. it is important for these to be printed first. Do not change this."
+
+ lp -s -d $USE_PRINTER -n "$numcopies" $lp_args -o page-set=odd -o collate=true -o outputorder=normal -t "$title"-odd "$filename"
+
+ #show user interface
+ LOG "Showing user interface on DISPLAY=$DISPLAY"
+ sudo --user="$user" zenity --question \
+ --title="$title" \
+ --text="Flip and reinsert the entire paper stack when printing has finished,\n\n then press Procceed.\n\n$donation_text" \
+ --icon="$printer_icon" \
+ --ok-label="Proceed" \
+ --cancel-label="Cancel" \
+ --width=$zenity_width 2>> "$LogFile"
+
+ # Capture the exit status
+ last_command_return=$?
+ # Log that the user interface was shown
+ LOG "User interface has been shown. Zenity returned >> $last_command_return <<"
+
+ if [ "$last_command_return" -eq 0 ]; then
+ LOG "printing even pages"
+
+ if [ $((page_count % 2)) -ne 0 ]; then
+ LOG "numpages is odd: one blank page will be printed after each copy."
+ # Multiple print jobs, otherwise external app is needed as pdftk
+ i=0
+ while [ "$i" -lt "$numcopies" ]; do
+ if [ "$short_edge" -eq 1 ]; then
+ lp -s -d $USE_PRINTER $lp_args \
+ -o page-ranges="$(even_pages "$page_count")" \
+ -o sides=one-sided -o orientation-requested=5 -o collate=true -o outputorder=normal -t "$title"-even \
+ "$filename"
+ else
+ lp -s -d $USE_PRINTER $lp_args \
+ -o page-ranges="$(even_pages "$page_count")" \
+ -o sides=one-sided -o collate=true -o outputorder=normal -t "$title"-even \
+ "$filename"
+ fi
+ lp -d $USE_PRINTER $SCRIPT_DIR/blank.pdf
+ ((i++))
+ done
else
- LOG User aborted print job
- rm "$filename"
- exit 0
+ LOG "numpages is even: just one printjob for all copies"
+ # Send to printer
+ if [ "$short_edge" -eq 1 ]; then
+ lp -s -d $USE_PRINTER \
+ -n "$numcopies" $lp_args \
+ -o sides=one-sided \
+ -o orientation-requested=5 \
+ -o collate=true \
+ -o outputorder=normal \
+ -o page-ranges="$(even_pages "$page_count")" \
+ -t "$title"-even \
+ "$filename"
+ else
+ # a printat 3 even si a scos 1 goală
+ lp -s -d $USE_PRINTER \
+ -n "$numcopies" $lp_args \
+ -o page-ranges="$(even_pages "$page_count")" \
+ -o sides=one-sided -o collate=true -o outputorder=normal -t "$title"-even \
+ "$filename"
+ fi
fi
+ else
+ LOG "User aborted print job"
+ rm "$filename"
+ exit 0
+ fi
fi
+
# Cleanup
trap 'rm -f "$filename"'
exit 0
diff --git a/usr/lib/cups/filter/duplex_print_filter b/usr/lib/cups/filter/duplex_print_filter
index f0f514e..98de7ff 100755
--- a/usr/lib/cups/filter/duplex_print_filter
+++ b/usr/lib/cups/filter/duplex_print_filter
@@ -1,5 +1,4 @@
-#! /bin/sh
-#log to /var/log/cups/error_log
+#!/bin/sh
set -x
# Command line arguments
@@ -9,7 +8,7 @@ title="$3"
numcopies="$4"
options="$5"
filename="$6"
-cp -praf $filename "/var/spool/cups/duplex/$job.duplex"
+cp -praf "$filename" "/var/spool/cups/duplex/$job.duplex"
# Pass file through
cat "$filename"