#!/bin/bash

# avoid error message if there are no files
shopt -s nullglob

# Encrypt all unencrypted .pdf files
for f in *.pdf
do
  qpdf --is-encrypted $f
  if [ $? -ne 0 ]; then
    echo Encrypting $f
    PASSPHRASE=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 20 | head -n 1)
    qpdf --encrypt "" $PASSPHRASE 40 --modify=n --extract=n --annotate=n -- --replace-input $f
  else
    echo no encryption needed for $f
  fi
done

# Removing .aux files
echo Removing .aux files
for f in *.aux
do
  rm $f
done

# Removing .log files
echo Removing .log files
for f in *.log
do
  rm $f
done

# Removing .out files
echo Removing .out files
for f in *.out
do
  rm $f
done
