#!/usr/bin/env bash

# run with
# curl -s https://git.dbogatov.org/-/snippets/52/raw/master/clone-all.sh | BASE=~/Desktop PAT=token bash -s -- --pull

set -e

GITLAB_SERVER=git.dbogatov.org

if [ -z "$PAT" ]
then
	echo "Must set PAT env variable to personal access token"
	exit 1
fi

if [ -z "$BASE" ]
then
	echo "Must set BASE env variable to base path (e.g. ~/Desktop)"
	exit 1
fi

PULL=false
if [[ $* == *--pull* ]]
then
	PULL=true
fi

cd $BASE

RESPONSE=$(
	curl \
		--head -s \
		--header "PRIVATE-TOKEN: $PAT" \
		"https://${GITLAB_SERVER}/api/v4/projects?simple=true&per_page=20" \
		| grep -Fi -e "x-total-pages" -e "x-total" \
		| tr '\r' ' '
)
COUNT=$(echo $RESPONSE | cut -d' ' -f2)
PAGES=$(echo $RESPONSE | cut -d' ' -f4)

echo "Total ${COUNT} in ${PAGES} pages"

counter=1
for i in $(seq 1 1 ${PAGES})
do
	echo "Page $i of $PAGES"

	readarray -t projects < <(
		curl \
			-s \
			--header "PRIVATE-TOKEN: $PAT" \
			"https://${GITLAB_SERVER}/api/v4/projects?simple=true&membership=true&page=$i&per_page=20" \
		| jq -r -c '.[] | .path_with_namespace'
	)
	for project in "${projects[@]}"
	do
		if [ -d "$project/.git" ] 
		then
			if [[ $PULL = true ]]
			then
				echo "Pulling $project ($counter of $COUNT)"
				cd $project
				git pull || true
				cd $BASE
				sleep 4
			else
				echo "$project already cloned  ($counter of $COUNT)"
			fi
		else
			echo "Cloning $project  ($counter of $COUNT)"
			mkdir -p $project
			cd $project
			git clone git@$GITLAB_SERVER:$project.git .
			cd $BASE
			sleep 4
		fi
		((counter=counter+1))
	done
done

echo "Done!"