#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'

if [ $# -lt 1 ] ; then
    echo "Usage: $(basename $0) PDF-NAME+"
    exit 1
fi

while (( "$#" )) ; do
    OLD_NAME="${1}"
    FILE_SLUG=$(pdfinfo $OLD_NAME | grep Title | cut -d: -f 2 | sed 's/ *$//g' | sed 's/^ *//g' | tr 'A-Z' 'a-z' | tr ' ' '-')

    if [ ! -z $FILE_SLUG ] ; then
        mv "$OLD_NAME" "${FILE_SLUG}.pdf"
    else
        echo "Cannot detect title in ${OLD_NAME}"
    fi

    shift
done
