#!/usr/bin/env bash

## Extract the files from a Facebook backup archive and remove media files.
##
## Usage: unzip-fb <name>.zip
##
## Dependencies: GNU coreutils, GNU sed, Info-ZIP UnZip
##
## I don't use Facebook as a social networking service, but I do use Facebook
## Messenger occasionally because some people prefer it to email unfortunately.
## I want backups of the messages but not the media files.

set -euo pipefail
shopt -s inherit_errexit extglob globstar nocaseglob nullglob

die() { echo "${0##*/}: $1" >&2; exit 1; }

[[ " $* " =~ ' --help ' ]] && sed -n 's/^## \?//p' "$0" && exit
(( $# )) || die 'missing argument'
(( $# > 1 )) && die 'invalid argument'

archive=$1
[[ -f $archive ]] || die "file not found: $archive"
[[ $archive =~ \.zip$ ]] || die "backup archive must be a .zip file"
dest=${archive%.zip}
unzip -qd "$dest" "$archive"
rm "$dest"/**/*.+(avif|gif|jp*g|mp4|png|webp)
du -sh "$dest"
