#!/bin/bash

ANDROID_BUILD_TOP="$(dirname $(realpath "$0"))"
CHECK="$(echo $1 | grep -q check && echo 1)"
GERRIT_USERNAME="$(git config review.review.lineageos.org.username)"
GERRIT_SSH="ssh://$GERRIT_USERNAME@review.lineageos.org:29418"

function repopick() {
    if [[ "$CHECK" = "1" ]]; then
        echo -n "${@: -1} - " ; ssh $GERRIT_SSH gerrit query ${@: -1} --format=JSON | jq --slurp -r '.[0].status'
    else
        ${ANDROID_BUILD_TOP}/lineage/scripts/repopick/repopick.py -g $GERRIT_SSH $@ &
    fi
}

function repopickchain() {
    if [[ "$CHECK" = "1" ]]; then
        echo -n "$1 - " ; ssh $GERRIT_SSH gerrit query $1 --format=JSON | jq --slurp -r '.[0].status'
    else
        `${ANDROID_BUILD_TOP}/gen_chain $@`
    fi
}

function rc() {
    echo `${ANDROID_BUILD_TOP}/gen_chain $@ | cut -b10-`
}

#
# _examples_
#
# # repo/1
# changes=(
#     2137 # hello
#     `rc 2137` # hello (chain)
# )
# repopick ${changes[@]}
#
# # repo/2
# repopickchain 2137 # hello (chain)
#
# # repo/3
# repopick 2137 # hello
#

## Platform
# build/make
repopickchain 497048 # Enable PRODUCT_ENABLE_UFFD_GC by default

# packages/apps/Settings
repopickchain 491435 # Require "tap to show" in SIM settings

# Sensitive Phone Numbers
repopick -t 24-sensitive-pn

## backuptool
repopick -t 24-backuptool

wait
