BUNDLEVERSION='1.5.0-master'
export ONOS_NAME=onos-${BUNDLEVERSION}
export ONOS_KARAF_DIR=apache-karaf-3.0.3
export ONOS_PKG=${ONOS_NAME}.zip

# onos-service will use ONOS_APPS to start default apps
export ONOS_APPS="openflow-base,openflow,ovsdb,vtn"

# The url that this version of ONOS netvirt can use to know ONOS is fully up
export ONOS_BOOT_WAIT_URL='onos/vtn/networks'


function setup_onos_package {
    set -x
    # Another repo is https://github.com/opennetworkinglab/onos
    ONOS_REPO=${ONOS_REPO:-https://gerrit.onosproject.org/onos}
    ONOS_REPO_DEST=onos_git_repo
    export KARAF_VERSION=3.0.3
    ONOS_KARAF_FILE_VER=apache-karaf-$KARAF_VERSION
    ONOS_MAVEN_FILE_VER=apache-maven-3.3.9
    cd $ONOS_DIR
    git_clone $ONOS_REPO $ONOS_REPO_DEST master
    mkdir -p Downloads Applications
    cd Downloads
    if [[ "$OFFLINE" != "True" &&  ! -f "${ONOS_KARAF_FILE_VER}.tar.gz" ]]; then
        wget http://archive.apache.org/dist/karaf/3.0.3/apache-karaf-3.0.3.tar.gz
    fi
    if [[ "$OFFLINE" != "True" &&  ! -f "${ONOS_MAVEN_FILE_VER}-bin.tar.gz" ]]; then
        wget http://archive.apache.org/dist/maven/maven-3/3.3.9/binaries/apache-maven-3.3.9-bin.tar.gz
    fi
    if [[ "$OFFLINE" != "True" ]]; then
        install_package zip
    fi
    tar -zxf ${ONOS_KARAF_FILE_VER}.tar.gz -C ../Applications/
    tar -zxf ${ONOS_MAVEN_FILE_VER}-bin.tar.gz -C ../Applications/
    export ONOS_ROOT=$ONOS_DIR/$ONOS_REPO_DEST
    export MAVEN=$ONOS_DIR/Applications/$ONOS_MAVEN_FILE_VER
    export KARAF_TAR=$ONOS_DIR/Downloads/apache-karaf-$KARAF_VERSION.tar.gz
    export KARAF_ROOT=$ONOS_DIR/Applications/$ONOS_KARAF_FILE_VER
    ONOS_APPS_STACK=$ONOS_APPS
    source $ONOS_ROOT/tools/dev/bash_profile

    cd $ONOS_ROOT
    mvn -DskipTests clean install
    export ONOS_VERSION=$BUNDLEVERSION
    onos-package  -z

    # onos-package will set ONOS_APPS via cell, so reset it back
    export ONOS_APPS=$ONOS_APPS_STACK
    cp /tmp/${ONOS_NAME}.zip $ONOS_DIR/
    set +x
}
