{ pkgs, ... }@all: with all;
{
	home.packages = with pkgs; [
    ghostscript
    (writeShellApplication rec {
      name = "nxgs";
      text = /* bash */ ''
        print_help() {
            echo "Usage: ${name} {flip <file(s)>|rotate <file(s)>|merge <Out File Name> <files>}"
        }
        if [ $# -lt 1 ]; then
            print_help; exit 1;
        fi
        case "$1" in
          flip)
            for FILE in "''${@:2}"; do
              NXGSTEMP=$(mktemp)
              gs -o "$NXGSTEMP" -sDEVICE=pdfwrite -c "<</Orientation 2>> setpagedevice" -f "$FILE"
              mv "$NXGSTEMP" "$FILE"
            done 
          ;;
          rotate)
            for FILE in "''${@:2}"; do
              NXGSTEMP=$(mktemp)
              gs -o "$NXGSTEMP" -sDEVICE=pdfwrite -c "<</Orientation 1>> setpagedevice" -f "$FILE"
              mv "$NXGSTEMP" "$FILE"
            done 
          ;;
          merge)
            gs -dBATCH -dNOPAUSE -q -sOUTPUTFILE="$3" -sDEVICE=pdfwrite "''${@:3}"
          ;;
          interactive-merge)
            echo -n "Enter Name of the merged file without extension: "; read -r MERGED 
            NXGSTEMP=$(mktemp)
            echo "''${@:2}" | tr " " "\n" > "$NXGSTEMP"
            $EDITOR "$NXGSTEMP"
            mapfile -t FILES < "$NXGSTEMP"
            gs -dBATCH -dNOPAUSE -q -sOUTPUTFILE="''${MERGED}.pdf" -sDEVICE=pdfwrite "''${FILES[@]}"
            rm "$NXGSTEMP"
          ;;
          *)
            echo "Error: Unknown command '$1'"
            print_help
            exit 1
          ;;
      esac
      exit 0
      '';
    })
  ];
}
