#!/bin/bash
#############################################################################
#                                                                           #
# This file is part of the "ubuntu" module of the otoolbox project.         #
#                                                                           #
# This script is open-source and intended for automation purposes.          #
# It is distributed in the hope that it will be useful, but WITHOUT         #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY        #
# or FITNESS FOR A PARTICULAR PURPOSE.                                      #
#                                                                           #
# Use of this script is entirely at your own risk.                          #
#                                                                           #
# Copyright (c) The otoolbox contributors.                                  #
#############################################################################
set -e

function resolve_script_path() {
    local source="${BASH_SOURCE[0]}"
    while [[ -L "$source" ]]; do
        local dir
        dir="$(cd -P "$(dirname "$source")" >/dev/null 2>&1 && pwd)"
        source="$(readlink "$source")"
        [[ $source != /* ]] && source="$dir/$source"
    done
    echo "$(cd -P "$(dirname "$source")" >/dev/null 2>&1 && pwd)"
}

SCRIPT_PATH=$(resolve_script_path)
source "$SCRIPT_PATH/otoolbox-common"

VERBOSE=false
DEBUG=false

function show_help() {
    cat << EOF
Usage: $(basename "$0") [OPTIONS]

Pull, sync, and push changes for shielded organization repositories.

OPTIONS:
  --help, -h      Show this help message and exit
  --verbose       Enable verbose output
  --debug         Enable debug mode

EOF
}

while [[ $# -gt 0 ]]; do
    case "$1" in
        --help|-h)
            show_help
            exit 0
            ;;
        --verbose)
            export VERBOSE=true
            shift
            ;;
        --debug)
            export VERBOSE=true
            export DEBUG=true
            shift
            ;;
        *)
            echo "Unknown option: $1" >&2
            show_help >&2
            exit 2
            ;;
    esac
done

_ot_init

if [[ -z "$SHIELDED_ORGANIZATION" ]]; then
    _ot_log_error "SHIELDED_ORGANIZATION is not set."
    exit 1
fi

for dir in "$WORKSPACE_ROOT/$SHIELDED_ORGANIZATION"/*; do
    if [[ -d "$dir" ]]; then
        project="$(basename "$dir")"
        _ot_log_info "Pulling project: $project"
        cd "$dir"
        git config --global --add safe.directory "$dir"
        git pull >> "$LOG_FILE" 2>&1
    fi
done

_ot_log_info "Syncing changes to shielded organization repositories..."
cd "$WORKSPACE_ROOT"
if [[ "$WORKER_USER" == "$CURRENT_USER" ]]; then
    otoolbox repo sync-shielded >> "$LOG_FILE" 2>&1
else
    _ot_run_as_worker otoolbox repo sync-shielded >> "$LOG_FILE" 2>&1
fi

for dir in "$WORKSPACE_ROOT/$SHIELDED_ORGANIZATION"/*; do
    if [[ -d "$dir" ]]; then
        project="$(basename "$dir")"
        _ot_log_info "Pushing changes to $SHIELDED_ORGANIZATION/$project"
        cd "$dir"
        git config --global --add safe.directory "$dir"
        git add . >> "$LOG_FILE" 2>&1
        git commit -m "[SYNC] Update to the latest release on ${SYNC_DATE:-$(date +%Y-%m-%d)}" >> "$LOG_FILE" 2>&1 || true
        git push >> "$LOG_FILE" 2>&1
    fi
done
