Skip to content

Instantly share code, notes, and snippets.

@jpauwels
Forked from dentarg/install_pdftk.sh
Last active May 23, 2017 12:30
Show Gist options
  • Save jpauwels/a8ece12ea6bc4fa6e41aeeb7d596b127 to your computer and use it in GitHub Desktop.
Save jpauwels/a8ece12ea6bc4fa6e41aeeb7d596b127 to your computer and use it in GitHub Desktop.
Install PDFTk without touching up the permissions
#!/usr/bin/env bash
# This is based on this excellent gist https://gist.github.com/jvenator/9672772a631c117da151
# Nothing of this is my original work, except that I made the download link an argument
# to this script, so it installs on OSX 10.11
#
# Thank you jvenator & sethetter
set -e
error() { info "$1"; exit 1; }
have() { command -v "$1" >/dev/null; }
usage() { echo "usage: $0 LINK_TO_SETUP_PKG"; }
have "brew" || error "Need homebrew to be installed"
[[ "$1" ]] || { usage; exit 1; }
[[ "$1" == "--help" || "$1" == "-h" ]] && { usage; exit; }
BASE_TMP_DIR=`mktemp -d -t pdftk`
TMP_DIR="$BASE_TMP_DIR/pdftk_package"
PKG_FILE="$BASE_TMP_DIR/pdftk_download.pkg"
BREW_DIR="$(brew --cellar)/pdftk"
if [ -d $BREW_DIR ]; then
echo -n "$BREW_DIR exists, remove? (y/n) "
read answer
if echo "$answer" | grep -iq "^y" ;then
brew unlink pdftk
rm -rf $BREW_DIR
else
echo "$BREW_DIR was not removed, exiting."
exit 1
fi
fi
# OSX <10.11: https://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/pdftk_server-2.02-mac_osx-10.6-setup.pkg
# OSX 10.11: https://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/pdftk_server-2.02-mac_osx-10.11-setup.pkg
echo "Downloading to $PKG_FILE"
curl -o $PKG_FILE $1
# Extract the Mac OS X server install package
pkgutil --expand $PKG_FILE $TMP_DIR
# Give the Payload file the proper gzip file extension and unzip it
mv $TMP_DIR/pdftk.pkg/Payload $TMP_DIR/pdftk.pkg/Payload.gz
gunzip $TMP_DIR/pdftk.pkg/Payload.gz
# List the contents of the archive to stdout; do not restore the contents to disk.
# cpio -i -t < $TMP_DIR/pdftk.pkg/Payload
# Use cpio to unarchive the resulting file
cd $TMP_DIR
cpio -iv < $TMP_DIR/pdftk.pkg/Payload
# Create Homebrew directories
mkdir -p $BREW_DIR/2.02/bin
mkdir -p $BREW_DIR/2.02/lib
mkdir -p $BREW_DIR/2.02/share/man/man1
# Move the relevant extracted files to their appropriate locations
mv $TMP_DIR/bin/pdftk $BREW_DIR/2.02/bin/pdftk
mv $TMP_DIR/lib/* $BREW_DIR/2.02/lib/
mv $TMP_DIR/man/pdftk.1 $BREW_DIR/2.02/share/man/man1/pdftk.1
brew link pdftk
echo "Done. You might wanna run 'brew doctor' to make sure everything is fine with your brew install"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment