payslips.nix (1347B)
1 { pkgs, ... }: 2 3 let 4 payslips = pkgs.writeShellScriptBin "payslips" '' 5 # Default destination directory, or use argument 6 destination_default="/tank/paperwork/personal/workplace/wages" 7 destination_dir="''${1:-$destination_default}" 8 pdftotext_bin="${pkgs.poppler-utils}/bin/pdftotext" 9 10 # Process matching files 11 for file in ./????????-????-????-????-????????????-result.pdf; do 12 [[ -e "$file" ]] || continue # Skip if no match 13 filename=$(basename "$file") 14 # Extract the date 15 date=$("$pdftotext_bin" "$file" - | grep -oE '[0-9]{2}-[0-9]{2}-[0-9]{4}' | head -n1) 16 if [[ "$date" =~ ^([0-9]{2})-([0-9]{2})-([0-9]{4})$ ]]; then 17 day="''${BASH_REMATCH[1]}" 18 month="''${BASH_REMATCH[2]}" 19 year="''${BASH_REMATCH[3]}" 20 new_filename="''${year}-''${month}-''${day}.pdf" 21 22 # Avoid overwriting existing files 23 if [[ -e "$destination_dir/$new_filename" ]]; then 24 new_filename="''${year}-''${month}-''${day}-$(date +%s).pdf" 25 fi 26 27 mv "$file" "$destination_dir/$new_filename" 28 echo "Moved: $file to $destination_dir/$new_filename" 29 else 30 echo "Skipped (no valid date found in PDF): $filename" 31 fi 32 done 33 ''; 34 in { 35 environment.systemPackages = [ payslips ]; 36 }