#!/bin/bash

oversize_list="/srv/storage/space/lists/oversize-contents.txt.xz"
objstorage_root="/srv/softwareheritage/objects"
oversize_root="/srv/softwareheritage/oversize-objects"

dry_run="y"

act () {
	if [ "$dry_run" = "y" ] ; then
		echo "* dry run: $@"
	else
		"$@"
	fi
}

warn () {
	echo "* WARNING: $1"
}

xzcat "$oversize_list" | 
while read obj_id ; do
	old_path="${objstorage_root}/${obj_id:0:2}/${obj_id:2:2}/${obj_id:4:2}/${obj_id}"
	new_path="${oversize_root}/${obj_id:0:2}/${obj_id:2:2}/${obj_id}"

	if ! [ -f "$new_path" ] ; then
		warn "$obj_id is missing from oversize storage, not removing"
		continue
	else
		act rm "$old_path"
	fi
done

