#!/usr/bin/env bash

## Extract the files from a Facebook backup archive and remove unwanted files.
##
## Usage: unzipfb <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.

set -euo pipefail
shopt -s inherit_errexit globstar

die() { echo -e "$(basename "$0"): $1" >&2; exit 1; }

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

zip_file=$1; [[ -f $zip_file ]] || die "file not found: $zip_file"
dest=${zip_file/.zip}

unzip -q "$zip_file" -d "$dest"
rm -rf "${dest:?}"/**/{comments_and_reactions,*.{gif,jpg,mp4,png}}
du -sh "$dest"