1#!/usr/bin/env bash
 2
 3set -euo pipefail
 4
 5check_license () {
 6    local dir="$1"
 7    local allowed_licenses=()
 8    local agpl_crates=("crates/collab")
 9
10    local is_agpl=false
11    for agpl_crate in "${agpl_crates[@]}"; do
12        if [[ "$dir" == "$agpl_crate" ]]; then
13            is_agpl=true
14            break
15        fi
16    done
17
18    if [[ "$is_agpl" == true ]]; then
19        allowed_licenses=("LICENSE-AGPL")
20    else
21        allowed_licenses=("LICENSE-GPL" "LICENSE-APACHE")
22    fi
23
24    for license in "${allowed_licenses[@]}"; do
25        if [[ -L "$dir/$license" ]]; then
26            return 0
27        elif [[ -e "$dir/$license" ]]; then
28            echo "Error: $dir/$license exists but is not a symlink."
29            exit 1
30        fi
31    done
32
33    if [[ "$dir" == "crates/collab" ]]; then
34        echo "Error: $dir does not contain a LICENSE-AGPL symlink"
35    else
36        echo "Error: $dir does not contain a LICENSE-GPL or LICENSE-APACHE symlink"
37    fi
38    exit 1
39}
40
41git ls-files "**/*/Cargo.toml" | while read -r cargo_toml; do
42   check_license "$(dirname "$cargo_toml")"
43done