#!/usr/bin/env bash
# Commands such as git pull return exit code if there are no changes
# set -e

if [[ -n "$ADDONS_GIT_REPOS" ]]; then
    gitlab_url="${GITLAB_URL:="https://gitlab.com"}"
    forgejo_url="${FORGEJO_URL:="https://codeberg.org"}"
    local_path="${LOCAL_PATH:="/var/lib/odoo/git"}"
    git_clone_depth="${GIT_CLONE_DEPTH:="999"}"

    # Setup git SSH key
    mkdir -p "$HOME/.ssh"
    chmod 700 "$HOME/.ssh"
    add-ssh-key


    # Make every git directory a safe directory
    git config --global --add safe.directory '*'

    # Clone git repo addons into local path
    log-entrypoint "Cloning git repos: $ADDONS_GIT_REPOS"
    addons_git_repos=$(echo "$ADDONS_GIT_REPOS" | tr ',' '\n')
    for addon_git_repo in $addons_git_repos; do

        # Supported urls:
        # ADDON_GIT_REPO=git@github.com:OCA/server-tools.git#16.0
        # ADDON_GIT_REPO=https://github.com/OCA/server-tools.git#16.0
        # ADDON_GIT_REPO=https://github.com/OCA/server-tools.git#18.0#8c5ea468176e8b960a0ae0a52128fc65e985e8a7

        git_url=$(echo "$addon_git_repo" | cut -d'#' -f1)
        git_branch=$(echo "$addon_git_repo" | cut -d'#' -f2)
        git_commit=$(echo "$addon_git_repo" | cut -d'#' -f3)
        git_hostname=$(parse-url "$git_url" hostname)
        git_proto=$(parse-url "$git_url" proto)
        git_path=$(parse-url "$git_url" path | sed 's/.git//g')
        git_local_path="$local_path/${git_hostname}/$git_path"

        ssh-keyscan -t rsa,dsa "$git_hostname" > "$HOME/.ssh/known_hosts" 2>/dev/null

        if [[ ! -d "$git_local_path/.git" ]]; then

            # Clone git repo and submodules
            mkdir -p "$git_local_path"

            # Add GitHub personal access token
            if [[ "${git_proto}${git_hostname}" = 'https://github.com' ]] && [[ -n "$GITHUB_USERNAME" ]]; then
                git_url="${git_proto}${GITHUB_USERNAME}:${GITHUB_PAT}@${git_hostname}/${git_path}.git"
            fi

            # Add GitLab personal access token
            if [[ "${git_proto}${git_hostname}" = "$gitlab_url" ]] && [[ -n "$GITLAB_USERNAME" ]]; then
                git_url="${git_proto}${GITLAB_USERNAME}:${GITLAB_PAT}@${git_hostname}/${git_path}.git"
            fi

            # Add Forgejo personal access token
            if [[ "${git_proto}${git_hostname}" = "$forgejo_url" ]] && [[ -n "$FORGEJO_USERNAME" ]]; then
                git_url="${git_proto}${FORGEJO_USERNAME}:${FORGEJO_PAT}@${git_hostname}/${git_path}.git"
            fi

            log-entrypoint "Clone ${git_url} branch $git_branch."
            git clone --depth "$git_clone_depth" --single-branch --branch "$git_branch" "$git_url" "$git_local_path"
        else
            log-entrypoint "Pull $git_branch branch in $git_local_path."
            git -C "$git_local_path" switch "$git_branch"
            git -C "$git_local_path" pull
        fi

        if [[ -n "$git_commit" ]]; then
            log-entrypoint "Checkout $git_commit commit in $git_local_path."
            git -C "$git_local_path" checkout "$git_commit"
        fi

        if [[ -f "$git_local_path/.gitmodules" ]]; then
            log-entrypoint "Init and update git submodules in $git_local_path."
            git -C "$git_local_path" submodule update --init --recursive --depth "$git_clone_depth"
        fi
    done

    # Remove git SSH key
    remove-ssh-key
fi
