#!/usr/bin/env bash

URL=$1
if [[ "$URL" =~ ^https*:\/\/web\.archive\.org\/web\/[0-9]+ ]]
then
	DST=`echo $1 | sed -e 's/https*:\/\// /g;s/:[0-9]\+//;' | cut -d' ' -f3`
else
	DST=`echo $1 | sed -e 's/https*:\/\// /g' | cut -d' ' -f2`
	if [[ "$DST" =~ photobucket\.com\/ ]]
	then
		echo "Applying Photobucket ref header..."
		REFERER='https://s.photobucket.com/'
	fi
fi

DIR=${DST%/*}
FILE=${DST##*/}

echo Getting ${URL}
echo Creating ${DIR}
mkdir -p "${DIR}"
pushd "${DIR}"
echo Saving ${FILE}
wget -4 --referer "${REFERER}" "${URL}"
popd

echo
echo https://forum.mm2c.com/a/${DIR}/${FILE}

