#!/usr/bin/env bash
#
# Copyright 2016 The Kubernetes Authors All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Set PROGram name
PROG=${0##*/}
########################################################################
#+
#+ NAME
#+     $PROG - Find a good Kubernetes build from Jenkins
#+
#+ SYNOPSIS
#+     $PROG  [--official] [<release branch>]
#+            [--github-token=<token>] [--exclude-suites="<suite> ..."]
#+            [--allow-stale-build] [--limit=N]
#+     $PROG  [--helpshort|--usage|-?]
#+     $PROG  [--help|-man]
#+
#+ DESCRIPTION
#+     Scan defined Jenkins instances for 2 contiguous successful runs
#+     to validate the state of the tree and then return build and release
#+     versions for use by the release tools or just for fun.
#+
#+     With no args, $PROG works on master.  Otherwise it works on the named
#+     release branch (ie. release-1.2) and optionally produces
#+     --official versions.
#+
#+ OPTIONS
#+     [--official]          - Produce an official release version for a
#+                             release branch
#+     [--github-token=]     - Must be specified if GITHUB_TOKEN not set
#+     [--exclude-suites=]   - Space separated list of CI suites regex to
#+                             exclude from go/nogo criteria
#+     [--allow-stale-build] - Keep going even if the build is behind HEAD.
#+                             This should only be used to display CI status.
#+                             Branch cuts can't actually use stale builds.
#+     [--limit=]            - Limit the primary check to a count of N
#+                             (Default: 100)
#+     [--help | -man]       - display man page for this script
#+     [--usage | -?]        - display in-line usage
#+
#+ EXAMPLES
#+     $PROG                 - Produce versions for master
#+     $PROG release-1.2     - Produce versions for release-1.2
#+
#+ FILES
#+
#+ SEE ALSO
#+     common.sh                 - base function definitions
#+     anago                     - release tool
#+
#+ BUGS/TODO
#+
########################################################################
# If NO ARGUMENTS should return *usage*, uncomment the following line:
#usage=${1:-yes}

source $(dirname $(readlink -ne $BASH_SOURCE))/lib/common.sh
source $TOOL_LIB_PATH/gitlib.sh
source $TOOL_LIB_PATH/releaselib.sh

# Validate command-line
common::argc_validate 1 || common::exit 1 "Exiting..."

###############################################################################
# common::cleanexit prog-specific override function
# Do stuff here to clean up after this specific script
# @param exit code
#
common::cleanexit () {
  rm -rf $LOCAL_CACHE
  common::timestamp end
  exit ${1:-0}
}

###############################################################################
# MAIN
###############################################################################
# BEGIN script
common::timestamp begin

# Force verbose flag
FLAGS_verbose=1

# Initialize and save up to 10 (rotated logs)
MYLOG=$TMPDIR/$PROG.log
common::logfileinit $MYLOG 10

gitlib::github_api_token
common::check_packages jq
common::check_pip_packages yq
common::set_cloud_binaries

# Blank for master
RELEASE_BRANCH=${POSITIONAL_ARGV[0]:-"master"}

if (($FLAGS_official)) && [[ "$RELEASE_BRANCH" == "master" ]]; then
  common::exit 1 "Can't do official releases on master!"
fi

# Are we branching to a new branch?
if gitlib::branch_exists $RELEASE_BRANCH; then
  TESTING_BRANCH=$RELEASE_BRANCH
  BRANCH_OFF_MASTER=0
else
  (($FLAGS_official)) && \
   common::exit 1 "Can't do official releases when creating a new branch!"
  TESTING_BRANCH=master
  BRANCH_OFF_MASTER=1
fi

logecho
release::set_build_version $TESTING_BRANCH $LOCAL_CACHE \
                           "$FLAGS_exclude_suites" \
                           ${FLAGS_limit:-"100"} || common::exit 1
release::set_release_version $JENKINS_BUILD_VERSION $RELEASE_BRANCH \
                             $BRANCH_OFF_MASTER || common::exit 1

common::exit 0
