#!/bin/bash

# TODO
# This is a copy of a script from dist-git and is only here until the original
# is merged and released. The original should then be used and this should be deleted.
# https://github.com/release-engineering/dist-git/pull/24

Usage() {
    cat <<EOF
Usage:
    $0 <package_git_directory> <package_lookaside_directory>

    Removes all tarballs in <package_lookaside_directory> except
    for tarballs that are referenced by the latest commit of each
    branch in <package_git_directory>.
EOF
}

die() { echo "$*" 1>&2 ; exit 1; }

if [[ $# != 2 ]]; then
    Usage
    exit 1
fi

pkg_git_dir="$1"
pkg_lookaside_dir="$2"

if [ ! -d "$pkg_git_dir" ]; then
    echo "$pkg_git_dir is not a valid directory."
    exit 1
fi
if [ ! -d "$pkg_lookaside_dir" ]; then
    echo "$pkg_lookaside_dir is not a valid directory."
    exit 1
fi

pushd "$pkg_git_dir" > /dev/null || exit 1

allowlist=()

# find sources that are referenced by the latest commit in any of the branches
for branch in $(git for-each-ref --format="%(refname:short)" refs/heads); do
    while read -r line; do
        set -- $line
        hash=$1
        filename=$2
        # skip projects using the new format
        test $# -eq 0 && continue
        test $# -ne 2 && die "Unsupported format. Only the old '<SUM> <FILENAME' format is used."
        allowlist+=("$hash","$filename")
    done < <(git show "$branch":sources)
done

# remove all source files that are not referenced
while read -r file; do
    old_IFS=$IFS
    IFS=/
    set -- $file
    IFS=$old_IFS

    # safety measure, if this is really the layout we expect the first and
    # third part matches
    test "$1" = "$3" || continue

    filename=$1
    hash=$2

    keep=false
    for source in "${allowlist[@]}"; do
        IFS=','
        set -- $source
        IFS=$old_IFS
        # keep sources where tarname and hash match the referenced ones
        if test "$1" = "$hash" -a "$2" = "$filename"; then
            keep=true
            break
        fi
    done

    "$keep" && continue
    unlink "$pkg_lookaside_dir/$file"
done < <( cd "$pkg_lookaside_dir" || exit 1 ; find . -mindepth 3 -maxdepth 3 -type f  -printf '%P\n' )
