#!/bin/bash
# aur-fetch - retrieve build files from the AUR
[[ -v AUR_DEBUG ]] && set -o xtrace
shopt -s extglob
argv0=fetch
XDG_CACHE_HOME=${XDG_CACHE_HOME:-$HOME/.cache}
XDG_CONFIG_HOME=${XDG_CONFIG_HOME:-$HOME/.config}
AUR_FETCH_USE_MIRROR=${AUR_FETCH_USE_MIRROR:-0}
AUR_LOCATION=${AUR_LOCATION:-https://aur.archlinux.org}
PS4='+(${BASH_SOURCE}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[1]}(): }'

# Author information for merge commits
export GIT_AUTHOR_NAME=aurutils
export GIT_AUTHOR_EMAIL=aurutils@localhost
export GIT_COMMITTER_NAME=aurutils
export GIT_COMMITTER_EMAIL=aurutils@localhost
export GIT_HTTP_USER_AGENT=aurutils

# Placeholder for repositories without commits
git_empty_object=$(git hash-object -t tree /dev/null)

# default options
existing=0 recurse=0 discard=0 sync=fetch

args_csv() {
    # shellcheck disable=SC2155
    local str=$(printf '%s,' "$@")
    printf '%s' "${str%,}"
}

# XXX: races with multiple fetch instances
results() {
    local mode=$1 prev=$2 current=$3 path=$4 dest=$5

    if [[ -w $dest ]]; then
        printf >> "$dest" '%s:%s:%s:file://%s\n' "$mode" "$prev" "$current" "$path"
    fi
}

sync_package_config() {
    case $(git config --get --type bool aurutils.rebase) in
        true)
            printf >&2 '%s: aurutils.rebase is set for %s\n' "$argv0" "$1"
            printf '%s' rebase ;;
        *)
            printf '%s' merge ;;
    esac
}

sync_should_merge() {
    local upstream=$1 dest=$2 pkg=$3

    # Check if last upstream commit can be reached from $dest
    if ! git merge-base --is-ancestor "$upstream" "$dest"; then
        return 0
    else
        # Print diagnostic with prefixed argv0 (unlike git)
        printf >&2 '%s: %s: already up to date\n' "$argv0" "$pkg"
        return 1
    fi
}

usage() {
    cat <<! | base64 -d
ICAgICAgICAgICAgIC4tLX5+LF9fCjotLi4uLiwtLS0tLS0tYH5+Jy5fLicKIGAtLCwsICAs
XyAgICAgIDsnflUnCiAgXywtJyAsJ2AtX187ICctLS4KIChfLyd+fiAgICAgICcnJycoOwoK
!
    printf >&2 'usage: %s [-Sefr] [--rebase|--reset|--merge] [--] pkgname...\n' "$argv0"
    exit 1
}

# option handling
opt_short='efrS'
opt_long=('auto' 'merge' 'reset' 'rebase' 'discard' 'existing' 'results:' 'ff'
          'ff-only' 'no-ff' 'no-commit' 'recurse')
opt_hidden=('dump-options' 'sync:')

if opts=$(getopt -o "$opt_short" -l "$(args_csv "${opt_long[@]}" "${opt_hidden[@]}")" -n "$argv0" -- "$@"); then
    eval set -- "$opts"
else
    usage
fi

unset rebase_args merge_args results_file
while true; do
    case "$1" in
        # fetch options
        -S|--auto)
            sync=auto ;;
        -f|--discard)
            discard=1 ;;
        -e|--existing)
            existing=1 ;;
        --merge)
            sync=merge ;;
        --rebase)
            sync=rebase ;;
        --reset)
            sync=reset ;;
        --results)
            shift; results_file=$(realpath -- "$1") ;;
        # git options
        --ff)
            merge_args+=(-ff) ;;
        --ff-only)
            merge_args+=(--ff-only) ;;
        --no-commit)
            merge_args+=(--no-commit) ;;
        --no-ff)
            merge_args+=(--no-ff); rebase_args+=(--no-ff) ;;
        # Compatibility options
        --sync)
            shift; sync=$1 ;;
        -r|--recurse)
            recurse=1 ;;
        --dump-options)
            printf -- '--%s\n' "${opt_long[@]}" ${AUR_DEBUG+"${opt_hidden[@]}"}
            printf -- '%s' "${opt_short}" | sed 's/.:\?/-&\n/g'
            exit ;;
        --) shift; break ;;
    esac
    shift
done

# Default to only allowing fast-forward merges (as git-pull)
if (( ! ${#merge_args[@]} )); then
    merge_args=(--ff-only)
fi

# option validation
if [[ $sync == !(auto|merge|rebase|reset|fetch) ]]; then
    printf >&2 '%s: invalid --sync mode\n' "$argv0"
    exit 1
fi

if (( ! $# )); then
    printf >&2 '%s: no targets specified\n' "$argv0"
    exit 1
fi

# XXX: race with concurrent processes
if [[ -v results_file ]]; then
    : >"$results_file" || exit 1 # truncate file
fi

# Save stdin/depends in array (2 passes)
if (( recurse )); then
    mapfile -t packages < <(aur depends --reverse "$@" | tsort)
    wait "$!" || exit

elif (( $# == 1 )) && [[ $1 == "-" || $1 == "/dev/stdin" ]]; then
    mapfile -t packages
else
    packages=("$@")
    set --
fi

# Exit gracefully on empty stdin, e.g. when piping from `aur repo -u`.
if (( ! ${#packages[@]} )); then
    exit 0
fi

# Update revisions in local AUR mirror
declare -A local_clones

# With an AUR mirror, updates are retrieved in two steps. First, updates to the
# mirror are synchronized with `git-fetch`. Secondly, local clones of the miror
# are created and are updated with `git-fetch` and `git-merge` as usual.
if (( AUR_FETCH_USE_MIRROR )); then
    while IFS=':' read -r pkg head; do
        printf "Cloning into '%s'\n" "$pkg"
        git -C "$pkg" --no-pager log --pretty=reference -1

        if [[ -v results_file ]]; then
            results 'clone' "$git_empty_object" "$head" "$PWD/$pkg" "$results_file"
        fi
        local_clones[$pkg]=$head
    done < <(
        aur fetch--mirror --lclone "${packages[@]}"
    )
    wait "$!" || exit
fi

# Main loop
for pkg in "${packages[@]}"; do
    unset -f git

    # Local clone by fetch--mirror
    if [[ ${local_clones[$pkg]} ]]; then
        continue

    # Verify if the repository is hosted on AUR (#959)
    elif (( existing )) && ! git ls-remote --exit-code "$AUR_LOCATION/$pkg" >/dev/null; then
        printf >&2 '%s: warning: package %s is not in AUR, skipping\n' "$argv0" "$pkg"
        continue

    # Clone package if not existing
    elif [[ ! -d $pkg/.git ]]; then
        git clone "$AUR_LOCATION/$pkg" || exit 1

        head=$(git -C "$pkg" rev-parse --verify HEAD)
        [[ $head ]] && git -C "$pkg" --no-pager log --pretty=reference -1
  
        if [[ -v results_file ]]; then
            results 'clone' "$git_empty_object" "${head:-$git_empty_object}" "$PWD/$pkg" "$results_file"
        fi

    # Update existing git repository
    else
        # Per-package lock
        exec {fd}< "$pkg"/.git
        flock --wait 5 "$fd" || exit 1

        # Avoid issues with filesystem boundaries (#274)
        git() { command git -C "$pkg" "$@"; }

        # Retrieve per-package configuration (aurutils.rebase, #1007)
        if [[ $sync == 'auto' ]]; then
            sync_pkg=$(sync_package_config "$pkg")
        else
            sync_pkg=$sync
        fi

        # Retrieve new upstream commits
        git fetch origin || exit

        # Store original HEAD for --results output
        orig_head=$(git rev-parse --verify --quiet HEAD)
        orig_head=${orig_head:-$git_empty_object}

        # Merge in new history
        upstream=origin/HEAD

        case $sync_pkg in
            rebase)
                dest=HEAD
                if sync_should_merge "$upstream" "$dest" "$pkg"; then
                    if (( discard )); then
                        git checkout ./
                    fi
                    git rebase "${rebase_args[@]}" "$upstream"
                fi ;;
            merge)
                dest=HEAD
                if sync_should_merge "$upstream" "$dest" "$pkg"; then
                    if (( discard )); then
                        git checkout ./
                    fi
                    git merge "${merge_args[@]}" "$upstream"
                fi ;;
            reset)
                dest=$upstream
                git reset --hard "$dest"
                ;;
            fetch)
                dest=$upstream
                ;;
        esac || {
            printf >&2 '%s: failed to %s %s\n' "$argv0" "$sync_pkg" "$pkg"
            exit 1
        }
        head=$(git rev-parse --verify "$dest")

        if [[ -v results_file ]]; then
            results "$sync_pkg" "$orig_head" "$head" "$PWD/$pkg" "$results_file"
        fi
        exec {fd}<&- # release lock
    fi >&2 # print all git output to stderr
done

# vim: set et sw=4 sts=4 ft=sh:
