#!/bin/bash

set -e

BASE="$HOME/.pwn/binwalk"

if [[ ! -d "$BASE/bin" ]]; then
	if [[ ! -d "$BASE/src" ]]; then
		git clone --depth 1 --recursive \
			https://github.com/ReFirmLabs/binwalk.git \
			"$BASE/src"
	fi
	pushd "$BASE/src"
	python3 setup.py install --prefix "$BASE"
	popd
fi

pythonversion="$(python3 -c 'import sys; print(f"{sys.version_info.major}.{sys.version_info.minor}")')"
export PYTHONPATH="$BASE/lib/python$pythonversion/site-packages:$PYTHONPATH"

"$BASE/bin/binwalk" "$@"
