scripts/repo: Create gerrit project

 * also refactor script

Change-Id: I3192baaa7c6fc10cadfc63c905c268541a52c7a1
diff --git a/repo/repo-settings.sh b/repo/repo-settings.sh
index ba457be..cb8b7c3 100755
--- a/repo/repo-settings.sh
+++ b/repo/repo-settings.sh
@@ -5,20 +5,44 @@
 	exit 1
 fi
 
-LEAF_VERSION=$(cat .repo/manifests/snippets/leaf.xml  | grep -i '<default revision' | cut -f2 -d '"' | cut -f3 -d '/')
+PORT="29418"
+GERRIT="review.leafos.org"
+GERRIT_PROJECTS=$(ssh -n -p "$PORT" "$GERRIT" gerrit ls-projects)
 
-for REPO in $(cat .repo/manifests/snippets/leaf.xml | grep -E 'LeafOS-Project|LeafOS-Blobs|LeafOS-Devices' | cut -f4 -d '"'); do
-	echo $REPO
-	curl -X POST \
-	-H "Accept: application/vnd.github+json" \
-	-H "Authorization: token $GH_TOKEN" \
-	https://api.github.com/orgs/$(echo $REPO | cut -f1 -d '/')/repos \
-	-d "{\"name\":\"$(echo $REPO | cut -f2 -d '/')\",\"private\":false,\"has_issues\":false,\"has_projects\":false,\"has_wiki\":false}" \
-	--silent > /dev/null
-	curl -X PATCH \
-	-H "Accept: application/vnd.github+json" \
-	-H "Authorization: token $GH_TOKEN" \
-        https://api.github.com/repos/$REPO \
-	-d "{\"has_issues\":false,\"has_projects\":false,\"has_wiki\":false,\"default_branch\":\"$LEAF_VERSION\"}" \
-	--silent > /dev/null
+LEAF_VERSION=$(grep -i '<default revision' .repo/manifests/snippets/leaf.xml | cut -f2 -d '"' | cut -f3 -d '/')
+
+grep -E 'LeafOS-Project|LeafOS-Blobs|LeafOS-Devices' .repo/manifests/snippets/leaf.xml | cut -f4 -d '"' | while IFS= read -r PROJECT; do
+	ORG=$(echo "$PROJECT" | cut -f1 -d '/')
+	REPO=$(echo "$PROJECT" | cut -f2 -d '/')
+
+	echo "$PROJECT"
+
+	# Github
+	curl -s -X POST \
+		-H "Accept: application/vnd.github+json" \
+		-H "Authorization: token $GH_TOKEN" \
+		"https://api.github.com/orgs/$ORG/repos" \
+		-d "{
+		\"name\":\"$REPO\",
+		\"private\":false,
+		\"has_issues\":false,
+		\"has_projects\":false,
+		\"has_wiki\":false
+		}" >/dev/null
+
+	curl -s -X PATCH \
+		-H "Accept: application/vnd.github+json" \
+		-H "Authorization: token $GH_TOKEN" \
+		"https://api.github.com/repos/$PROJECT" \
+		-d "{
+		\"has_issues\":false,
+		\"has_projects\":false,
+		\"has_wiki\":false,
+		\"default_branch\":\"$LEAF_VERSION\"
+		}" >/dev/null
+
+	# Gerrit
+	if ! [[ $GERRIT_PROJECTS =~ $PROJECT ]]; then
+		ssh -n -p "$PORT" "$GERRIT" gerrit create-project "$PROJECT" -b "$LEAF_VERSION"
+	fi
 done