--- /dev/null
+#!/usr/bin/env bash
+
+#
+# Convert Audible .aax files to mp3s.
+#
+
+die() {
+ echo $@
+ exit 1
+}
+
+usage() {
+ cat <<EOF
+$0: convert Audible .aax files to mp3s.
+
+-a [authcode] Set the authcode for the Audible file.
+-d Set debug mode, in which we do not delete temporary files.
+-h Display this help message.
+-i [file] The Audible file.
+-o [path] Set the final directory output path.
+EOF
+}
+
+debug=0
+while getopts "a:dhi:o:" flag; do
+ case $flag in
+ a) authcode=$OPTARG;;
+ d) debug=1;;
+ h) usage; exit 0;;
+ i) input=$OPTARG;;
+ o) output=$OPTARG;;
+ *) usage; exit 1;;
+ esac
+done
+shift $((OPTIND-1))
+
+which AAXtoMP3 &> /dev/null || die "Failed to locate AAXtoMP3. Please install it."
+which audiobooker &> /dev/null || die "Failed to locate audiobooker. Please install it."
+which tagger.py &> /dev/null || die "Failed to locate tagger.py. Please install it."
+[[ -v authcode ]] || die "You must specify the authcode."
+[[ -v input ]] || die "You must specify the input file."
+[[ -f "${input}" ]] || die "Not a normal file: ${input}"
+[[ -v output ]] || die "You must specify the output directory."
+[[ -e "${output}" ]] && die "Output directory already exists: ${output}"
+mkdir "${output}" || die "Failed to create a directory at ${output}"
+rmdir "${output}" || die "Failed to rmdir the directory at ${output}"
+
+tmpdir="`mktemp -d -t audible-to-mp3.XXXXXXXXXX`" || exit 1
+[[ -v debug ]] || trap "rm -rf ${TMPDIR}; exit" INT TERM EXIT
+chmod 700 "${tmpdir}"
+echo "-> mkdir ${tmpdir}"
+
+cp -f "${input}" "${tmpdir}/input.aax" || die "failed to copy ${input} to ${tmpdir}/input.aax"
+echo "-> AAXtoMP3 --authcode ${authcode} --single ${tmpdir}/input.aax"
+AAXtoMP3 --authcode "${authcode}" --single "${tmpdir}/input.aax"
+[[ $? -eq 0 ]] || die "AAXtoMP3 failed."
+# For some reason, AAXtoMP3 doesn't give us much control over the output file name.
+# Let's find out what it is using the "find" command, and change it to something well-behaved.
+pushd "${tmpdir}" &> /dev/null || die "failed to pushd ${tmpdir}"
+find -name '*.mp3' -type f -print0 | xargs -0 -I{} mv {} "${tmpdir}/input.mp3"
+popd &> /dev/null
+[[ $? -eq 0 ]] || die "Failed to locate and move the AAXtoMP3 output."
+mkdir -p "${tmpdir}/output" || die "failed to create ${tmpdir}/output directory"
+pushd "${tmpdir}/output" &> /dev/null
+[[ $? -eq 0 ]] || die "Failed to pushd to ${tmpdir}/output"
+echo "-> echo y | audiobooker ../input.mp3"
+echo y | audiobooker ../input.mp3
+[[ $? -eq 0 ]] || die "audiobooker failed."
+popd &> /dev/null
+echo "-> mv ${tmpdir}/output ${output}"
+mv -f "${tmpdir}/output" "${output}" || die "failed to move the output directory to ${output}"
+echo "-> tagger.py -A "${output}""
+tagger.py -A "${output}"
+[[ $? -eq 0 ]] || die "tagger.py failed. Output remains in ${output}."