nix-configs

Personal NixOS and home-manager configuration files
Log | Files | Refs

payslips.nix (1423B)


      1 { pkgs, vars, ... }:
      2 
      3 let
      4   payslips = pkgs.writeShellScriptBin "payslips" ''
      5     destination_default="/tank/paperwork/personal/workplace/wages"
      6     destination_dir="''${1:-$destination_default}"
      7 
      8     mkdir -p "$destination_dir"
      9 
     10     shopt -s nullglob
     11 
     12     for file in ./*.PDF; do
     13         filename=$(basename "$file")
     14 
     15         # Match: "10 - 31052026.PDF"
     16         if [[ "$filename" =~ ^[0-9]+[[:space:]]-[[:space:]]([0-9]{2})([0-9]{2})([0-9]{4})\.PDF$ ]]; then
     17             day="''${BASH_REMATCH[1]}"
     18             month="''${BASH_REMATCH[2]}"
     19             year="''${BASH_REMATCH[3]}"
     20 
     21             new_filename="''${year}-''${month}-''${day}.pdf"
     22             output_path="$destination_dir/$new_filename"
     23 
     24             # Avoid overwriting existing files
     25             if [[ -e "$output_path" ]]; then
     26                 output_path="$destination_dir/''${year}-''${month}-''${day}-$(date +%s).pdf"
     27             fi
     28 
     29             # Remove PDF password protection
     30             ${pkgs.qpdf}/bin/qpdf --password=${vars.secrets.pdfpassword} --decrypt "$file" "$output_path"
     31 
     32             if [[ $? -eq 0 ]]; then
     33                 rm "$file"
     34                 echo "Processed: $file -> $output_path"
     35             else
     36                 echo "Failed to process: $file"
     37             fi
     38         else
     39             echo "Skipped (filename did not match expected format): $filename"
     40         fi
     41     done
     42   '';
     43 in {
     44   environment.systemPackages = [ payslips ];
     45 }