#!/bin/bash
set -e

if [[ "$1" ]]; then
   export license="$1"
else
   export license="$(make valueof-WORKSRC)/COPYING"
fi 

if ! [[ -f $license ]]; then
   echo "License file not found at: $license"
   exit 1
fi

license_hash=$(sha256sum "$license" | awk '{print $1}')
mkdir -vp /usr/gar/licenses
archived_license="/usr/gar/licenses/${license_hash}"
[[ -f $archived_license ]] || cp -v "$license" "$archived_license"
sed -r -i "s|LICENSE =.*$|LICENSE = ${license_hash}|g" Makefile
grep -E -H "^[[:blank:]]*LICENSE.*=" Makefile
ls -l "$archived_license"
