{ pkgs, ... }:
{
  home.packages = with pkgs; [
    (writeShellApplication {
      name = "nxmd";
      text = let
        ascii_size = (pkgs.writers.writePython3Bin "ascii_size" {
          # libraries = with pkgs.python311Packages; [ ];
          flakeIgnore = [];
          } /* python */ ''
            from sys import argv

            with open(argv[1], "r", encoding='utf-8') as f:
                raw = f.read()

            sraw = raw.split("\n")

            linestart = 0
            lineend = 0
            started = False
            for linenum in range(len(sraw)):
                if sraw[linenum].strip() == "```ascii":
                    linestart = linenum
                    started = True

                if started and sraw[linenum].strip() == "```":
                    lineend = linenum
                    break

            maxwidth = 0
            for linenum in range(linestart, lineend):
                linewidth = len(sraw[linenum])
                if linewidth > maxwidth:
                    maxwidth = linewidth

            print(f"Maximum length of the `ascii` code clock is {maxwidth} characters.")

            px = (1 / maxwidth) * 1500
            vw = (1 / maxwidth) * 150

            snew = sraw
            snew[3] = "aaw: " + str(round(vw, 2))
            snew[4] = "aawm: " + str(round(px, 2))

            new = "\n".join(snew)

            with open(argv[1], "w", encoding='utf-8') as f:
                f.write(new)
            print(f"The resulting font sizes are {px}px (aawm) and {vw}vw (aam).")
            print(f"You can now use the push operation on {argv[1]}.")
          ''
        );
      in /* bash */ ''
        ord=$(date +"%e" | awk '{printf("%d%s\n", $1, ($1==11||$1==12||$1==13)?"th":((($1%10)==1)?"st":((($1%10)==2)?"nd":((($1%10)==3)?"rd":"th"))))}')
        nxdate="$(date +'%A the ')" 
        nxdate="$nxdate""$ord"
        nxdate="$nxdate""$(date +' of %B %Y')"

        if [[ $# -eq 0 ]]; then
          echo "Error: No arguments provided."
          echo "Usage: $0 push <markdown-file>"
          exit 1
        fi

        if [[ "$1" == "push" ]]; then
          if [[ ! "$2" =~ \.md$ ]]; then
            echo "Error: Second argument must be a markdown (.md) file"
            echo "Usage: $0 push <markdown-file>"
            exit 1
          fi
          pandoc \
            "$2" \
            --highlight-style=breezeDark \
            --standalone \
            --mathjax \
            --ascii \
            --template /var/nginx/assets/template.html \
            --metadata pdate="$nxdate" \
            --verbose --from markdown-markdown_in_html_blocks+raw_html \
            -o /var/nginx/webroot/"$(basename "$2" .md )".html 

        	echo pushed "$2" 
        elif [[ "$1" == "ascii-size" ]]; then
          if [[ ! "$2" =~ \.md$ ]]; then
            echo "Error: Second argument must be a markdown (.md) file"
            echo "Usage: $0 ascii-size <markdown-file>"
            exit 1
          fi
          ${ascii_size}/bin/ascii_size "$2"
        
        else
          echo "Error: First argument must be 'push'"
          echo "Usage: $0 push <markdown-file>"
          exit 1
        fi
      '';
    })
  ];
}
