#!/bin/sh
#
# Requires qpdf.

set -e

if [ $# -lt 1 ]; then
    echo "Usage: $(basename $0) PDFFILE pagenumbers..."
    exit 1
fi

pdf=$1
shift

for p in $*; do
    case $p in
        1)
            slice=2-r1
        ;;
        *)
            bef=1-$((p-1))
            aft=$((p+1))-r1
            slice=$bef,$aft
            ;;
    esac
    qpdf $pdf --replace-input --pages . $slice --
done