#!/bin/bash

# (C) 2022 Massimo Girondi - CC BY-NC-SA 4.0

PASSWD=$(cat password.txt)


remove_pass() {
        FILE=$1
        if ! [[ $FILE =~ "NOPASSWD" ]]; then
                echo "Working on ${FILE}"
                DIR=$(dirname "$FILE")
                NEW_NAME_BKP=bkp/${FILE}
                NEW_NAME="${FILE%.*}_NOPASSWD.pdf"
                mkdir -p bkp/${DIR}
                mv "$FILE" "${NEW_NAME_BKP}"
                qpdf --password=${PASSWD} --decrypt "${NEW_NAME_BKP}" "${NEW_NAME}"
        fi
}




while IFS= read -rd $'\0' file; do
    remove_pass "$file"
done < <(find . -iname "*.pdf" -not -path "./bkp/*" -print0)

