#!/bin/sh
# TODO: Rewrite this in a proper language
set -eu
list() {
	# shellcheck disable=2086
	curl -s -X "GET" \
		"$1" \
		$HEADERS |
		jq -r "$FILTER"
}

_VAR_REGEX='(?<=^DEVDIRS_)[A-Za-z_-]*(?==)'
get_path() {
	reponame=$(echo "$1" | rev | cut -d '/' -f 1 | rev)
	echo " $BLOCK_REPOS " | grep -q " $reponame " && return 1
	for var in $(env | grep -oP "$_VAR_REGEX"); do
		repos=$(eval "echo \$DEVDIRS_$var | xargs -r")
		dir=$(echo "$var" | cut -d '_' -f 2)
		if echo " $repos " | grep -q " $reponame "; then
			return
		fi
	done
	if ! $BLOCK_UNSORTED; then
		echo "$BASE/unsorted/$reponame"
		return
	fi
}

warn() {
    tput el
	tput setaf 1
	tput bold
	echo "$@"
	tput sgr0
}

info() {
    tput el
	tput setaf 4
	tput bold
	echo "$@"
	tput sgr0
}

success() {
    tput el
	tput setaf 2
	tput bold
	echo "$@"
	tput sgr0
}

scroll() {
    # info "executing $*"
	tput setaf 8
    tput sc
	while read -r line; do
        tput el
		echo "$line"
        tput rc
	done
	tput sgr0
}

# pull() {
#
# }
#
# clone() {
#
# }

# FIX: pulling isn't functional yet
ensure() {
	repo=$1
	dir=$(get_path "$repo") || {
		warn "$repo blocked ✗"
		return
	}
    [ -z "$dir" ] && return
	if [ -d "$dir/.git" ]; then
		git -C "$dir" status | grep -iq 'working tree clean' || {
			warn "$repo unclean tree "
			git -C "$dir" status | tail -n=+3 | head -n=+2
			return
		}
        tput sc
		info "$repo getting pulled 󰚭"
		git -C "$dir" pull 2>&1 | scroll || {
            tput rc
            warn "$repo pull failed "
            return
        }
        tput el
        tput cuu1
        success "$repo pulled "
        target=$(echo "$repo" | rev | cut -d'/' -f1 | rev)
		if [ -x "$config_dir/hooks/update/$target" ]; then
			cd "$dir"
            tput sc
            info "Running $target update hook"
			"$config_dir/hooks/update/$target" | scroll || {
				warn "failed to run update hook for $target ✗"
			}
            tput el
            tput cuu1
            success "Ran $target update hook successfully ✓"
			cd -
		fi
		return
	fi
	mkdir -p "$dir" || {
		warn "mkdir -p $dir failed ✗"
		return
	}
    tput sc
	info "$repo getting cloned 󰚭"
	# shellcheck disable=2086
	git clone $CLONE_FLAGS "$repo" "$dir" 2>&1 | scroll  || {
        tput rc
		warn "$repo clone failed ✗"
		return
	}
    tput el
    tput cuu1
    success "$repo cloned "
    target=$(echo "$repo" | rev | cut -d'/' -f1 | rev)
	if [ -x "$config_dir/hooks/install/$target" ]; then
		cd "$dir"
        tput sc
        info "Running $target install hook"
		"$config_dir/hooks/install/$target" | scroll || {
			warn "failed to run install hook for $target ✗"
		}
        tput el
        tput cuu1
        success "Ran $target install hook successfully ✓"
		cd -
	fi
	return
}

# ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
# ░  ░░░░  ░░░      ░░░        ░░   ░░░  ░
# ▒   ▒▒   ▒▒  ▒▒▒▒  ▒▒▒▒▒  ▒▒▒▒▒    ▒▒  ▒
# ▓        ▓▓  ▓▓▓▓  ▓▓▓▓▓  ▓▓▓▓▓  ▓  ▓  ▓
# █  █  █  ██        █████  █████  ██    █
# █  ████  ██  ████  ██        ██  ███   █
# ████████████████████████████████████████

# shellcheck disable=1091
. "${XDG_CONFIG_HOME:-"$HOME/.config"}/clonedev/config"
config_dir=${XDG_CONFIG_HOME:-$HOME/.config}/clonedev

# shellcheck disable=2015
mkdir -p "$BASE" && cd "$BASE" || {
	warn "Failed to create/cd $BASE ✗"
}

for target in $TARGETS; do
	for repo in $(list "$API_URL/$target/$ENDPOINT" | sed 's|.git$||' | xargs); do
		ensure "$repo"
	done
done
for repo in $(echo "$EXTRA_REPOS" | xargs); do
	ensure "$repo"
done