#!/bin/bash
# -*- mode: shell-script-mode; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
#
# Copyright (C) 2012-13 O.S. Systems Software LTDA.
# Authored-by:  Otavio Salvador <otavio@ossystems.com.br>
# Adopted to Angstrom:  Khem Raj <raj.khem@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
RPBcleanup() {
        unset MACHINETABLE MACHLAYERS DISTROTABLE DISTROLAYERS DISTRO_DIRNAME OEROOT
        unset ITEM SCRIPTS EULA EULA_MACHINE REPLY READ_EULA
        unset usage oldmach

        if [ -n "$BUILDDIR" ]; then
                export BUILDDIR
        fi
}
trap RPBcleanup RETURN

if [ "$(whoami)" = "root" ]; then
    echo "ERROR: do not use the BSP as root. Exiting..."
    return
fi

OEROOT=$(pwd)
cd "$OEROOT"
if [ -n "$ZSH_VERSION" ]; then
    setopt sh_word_split
    setopt clobber
elif [ -n "$BASH_VERSION" ]; then
    set +o noclobber
fi

usage () {
    cat <<EOF

Usage: [DISTRO=<DISTRO>] [MACHINE=<MACHINE>] source ${BASH_SOURCE[0]} [BUILDDIR]

If no MACHINE is set, list all possible machines, and ask user to choose.
If no DISTRO is set, list all possible distros, and ask user to choose.
If no BUILDIR is set, it will be set to build-DISTRO.

EOF
}

if [ $# -gt 1 ]; then
    usage
    return 1
fi


if [ -z "${DISTRO}" ]; then
   unset UNISOC_PRODUCT
fi

# create a common list of "<machine>(<layer>)", sorted by <machine>
# Blacklist OE-core and meta-linaro, we only want 96boards + vendor layers
MACHLAYERS=$(find layers -print | grep "conf/machine/.*\.conf" | grep "meta-unisoc" | grep -v "recovery\.conf" | sed -e 's/\.conf//g' -e 's/layers\///' | awk -F'/conf/machine/' '{print $NF "(" $1 ")"}' | LANG=C sort)

if [ -z "${MACHINE}" ]; then
    # whiptail
    which whiptail > /dev/null 2>&1
    if [ $? -eq 0 ]; then
        MACHINETABLE=
        MACHINE_ARRAY=("")
        i=0
        for ITEM in $MACHLAYERS; do
            tag=$(echo "$ITEM" | cut -d'(' -f1)
            MACHINE_ARRAY[$i]=$tag
            i=`expr $i + 1`
            info=$(echo "$ITEM" | cut -d'(' -f2 | cut -d')' -f1)
            MACHINE_ARRAY[$i]=" "$info
            i=`expr $i + 1`
            MACHINETABLE="${MACHINETABLE} $(echo "$ITEM" | cut -d'(' -f1) $(echo "$ITEM" | cut -d'(' -f2 | cut -d')' -f1)"
        done
        echo "${MACHINE_ARRAY[@]}"
        MACHINE=$(whiptail --title "Available Machines" --menu \
            "Please choose a machine" 0 0 20 \
            "${MACHINE_ARRAY[@]}" 3>&1 1>&2 2>&3)
    fi

    # dialog
    if [ -z "$MACHINE" ]; then
        which dialog > /dev/null 2>&1
        if [ $? -eq 0 ]; then
            MACHINETABLE=
            for ITEM in $MACHLAYERS; do
                MACHINETABLE="$MACHINETABLE $(echo "$ITEM" | cut -d'(' -f1) $(echo "$ITEM" | cut -d'(' -f2 | cut -d')' -f1)"
            done
            MACHINE=$(dialog --title "Available Machines" --menu "Please choose a machine" 0 0 20 $MACHINETABLE 3>&1 1>&2 2>&3)
        fi
    fi
fi

# create a common list of "<distro>(<layer>)", sorted by <distro>
# Blacklist OE-core and meta-linaro, we only want 96boards + vendor layers
DISTROLAYERS=$(find layers -print | grep "conf/distro/.*\.conf" | grep -v scripts | grep -v openembedded-core | grep -v meta-linaro | grep -E 'wayland|console|initgc|ubuntu|tiny' | sed -e 's/\.conf//g' -e 's/layers\///' | awk -F'/conf/distro/' '{print $NF "(" $1 ")"}' | LANG=C sort)

if [ -n "${DISTROLAYERS}" ] && [ -z "${DISTRO}" ]; then
    # whiptail
    which whiptail > /dev/null 2>&1
    if [ $? -eq 0 ]; then
        DISTROTABLE=
        DISTRO_ARRAY=("")
        i=0
        for ITEM in $DISTROLAYERS; do
            tag=$(echo "$ITEM" | cut -d'(' -f1)
            DISTRO_ARRAY[$i]=$tag
            i=`expr $i + 1`
            info=$(echo "$ITEM" | cut -d'(' -f2 | cut -d')' -f1)
            DISTRO_ARRAY[$i]=" "$info
            i=`expr $i + 1`
            DISTROTABLE="${DISTROTABLE} $(echo "$ITEM" | cut -d'(' -f1) $(echo "$ITEM" | cut -d'(' -f2 | cut -d')' -f1)"
        done
        echo "${DISTRO_ARRAY[@]}"
        DISTRO=$(whiptail --title "Available Distributions" --menu \
            "Please choose a distribution" 0 0 20 \
            "${DISTRO_ARRAY[@]}" 3>&1 1>&2 2>&3)
    fi

    # dialog
    if [ -z "$DISTRO" ]; then
        which dialog > /dev/null 2>&1
        if [ $? -eq 0 ]; then
            DISTROTABLE=
            for ITEM in $DISTROLAYERS; do
                DISTROTABLE="$DISTROTABLE $(echo "$ITEM" | cut -d'(' -f1) $(echo "$ITEM" | cut -d'(' -f2 | cut -d')' -f1)"
            done
            DISTRO=$(dialog --title "Available Distributions" --menu "Please choose a distribution" 0 0 20 $DISTROTABLE 3>&1 1>&2 2>&3)
        fi
    fi
fi

# choose a product type
PRODUCTTYPES=$(find layers -print | grep "conf/distro/include/product/.*\.inc" | grep -E 'distro-features'  | sed -e 's/distro-features-//g' -e 's/\.inc//g' -e 's/layers\///' | awk -F'/conf/distro/include/product/' '{print $NF "(" $1 ")"}' | sed -e 's/default(meta-unisoc)/default(full-feature-product)/g' | LANG=C sort)
#echo "PRODUCTTYPES: $PRODUCTTYPES"
#echo "DISTROLAYERS: $DISTROLAYERS"

if [ -n "${PRODUCTTYPES}" ] && [ -z "${UNISOC_PRODUCT}" ]; then
    # whiptail
    which whiptail > /dev/null 2>&1
    if [ $? -eq 0 ]; then
        PRODUCTTABLE=
        PRODUCT_ARRAY=("")
        i=0
        for ITEM in $PRODUCTTYPES; do
            tag=$(echo "$ITEM" | cut -d'(' -f1)
            # echo "DISTRO:${DISTRO}, tag: $tag"
            if [[ ${DISTRO} =~ "ubuntu" ]]; then
                if [[ $tag =~ "ubuntu" ]] || [[ $tag == "wifionly" ]]; then
                     echo "ubuntu product: $tag"
                 else
                     continue
                 fi
             elif [[ $tag =~ "ubuntu" ]]; then
                 echo "ubuntu product for non-ubuntu distro: $tag"
                 continue
            fi
            PRODUCT_ARRAY[$i]=$tag
            i=`expr $i + 1`
            info=$(echo "$ITEM" | cut -d'(' -f2 | cut -d')' -f1)
            PRODUCT_ARRAY[$i]=" "$info
            i=`expr $i + 1`
            PRODUCTTABLE="${PRODUCTTABLE} $(echo "$ITEM" | cut -d'(' -f1) $(echo "$ITEM" | cut -d'(' -f2 | cut -d')' -f1)"
        done
        echo "${PRODUCT_ARRAY[@]}"
        UNISOC_PRODUCT=$(whiptail --title "Available Distributions" --menu \
            "Please choose a product type" 0 0 20 \
            "${PRODUCT_ARRAY[@]}" 3>&1 1>&2 2>&3)
    fi

    # dialog
    if [ -z "$UNISOC_PRODUCT" ]; then
        which dialog > /dev/null 2>&1
        if [ $? -eq 0 ]; then
            PRODUCTTABLE=
            for ITEM in $PRODUCTTYPES; do
                PRODUCTTABLE="$PRODUCTTABLE $(echo "$ITEM" | cut -d'(' -f1) $(echo "$ITEM" | cut -d'(' -f2 | cut -d')' -f1)"
            done
            UNISOC_PRODUCT=$(dialog --title "Available Distributions" --menu "Please choose a product type" 0 0 20 $PRODUCTTABLE 3>&1 1>&2 2>&3)
        fi
    fi
fi


if [ -z "${USERDEBUG}" ]; then
    USERDEBUG=$(whiptail --title "Available Distributions" --menu "Please choose a engineering version" 0 0 20 \
        "user" "user-version" \
        "userdebug" "userdebug-version" \
        "eng" "eng-version"  3>&1 1>&2 2>&3)

    exitstatus=$?

    if [ $exitstatus = 0 ]; then
        echo "Your chosen USERDEBUG:" $USERDEBUG
    else
        echo "You chose Cancel."
    fi
fi

if [ -z "${SECBOOT_ENABLE}" ]; then
    SECBOOT_ENABLE=$(whiptail --title "Available Distributions" --menu "Please choose a secureboot status" 0 0 20 \
        "sec" "sec-version" \
        "nosec" "nosec-version" 3>&1 1>&2 2>&3)

    exitstatus=$?

    if [ $exitstatus = 0 ]; then
        echo "Your chosen SECBOOT_ENABLE:" $SECBOOT_ENABLE
    else
        echo "You chose Cancel."
    fi
fi

# If nothing has been set, go for 'nodistro'
if [ -z "$DISTRO" ]; then
    DISTRO="nodistro"
fi

# guard against Ctrl-D or cancel
if [ -z "$MACHINE" ]; then
    echo "To choose a machine interactively please install whiptail or dialog."
    echo "To choose a machine non-interactively please use the following syntax:"
    echo "    MACHINE=<your-machine> . ./setup-environment"
    echo ""
    echo "Press <ENTER> to see a list of your choices"
    read -r
    echo "$MACHLAYERS" | sed -e 's/(/ (/g' | sed -e 's/)/)\n/g' | sed -e 's/^ */\t/g'
    return
fi

if [ -z "${SDKMACHINE}" ]; then
    SDKMACHINE='x86_64'
fi

SCRIPTS="${OEROOT}"/prebuilts/scripts

# we can be called with only 1 parameter max, <build> folder, or default to build-$distro
BUILDDIR=build-$DISTRO
if [ $# -eq 1 ]; then
    BUILDDIR=$1
fi
BUILDDIR=$OEROOT/$BUILDDIR

BUILDDIR=$(readlink -f "$BUILDDIR")

# Clean up PATH, because if it includes tokens to current directories somehow,
# wrong binaries can be used instead of the expected ones during task execution
export PATH=$(echo "${PATH}" | sed 's/\(:.\|:\)*:/:/g;s/^.\?://;s/:.\?$//')
export PATH="${OEROOT}"/layers/openembedded-core/scripts:"${OEROOT}"/bitbake/bin:"${OEROOT}"/.repo/repo:"${OEROOT}"/prebuilts/scripts:"${OEROOT}"/prebuilts/scripts/tinytools:"${OEROOT}"/prebuilts/scripts/ubuntu_scripts:"${PATH}"
#remove duplicate path entries
export PATH=$(echo "$PATH" | awk -F: '{for (i=1;i<=NF;i++) { if ( !x[$i]++ ) printf("%s:",$i); }}' | sed 's/:$//')
# Make sure Bitbake doesn't filter out the following variables from our
# environment.

# VERSION chose
VERSION=`grep "^LAYERSERIES_COMPAT_core \?= \?\"" ${OEROOT}/layers/openembedded-core/meta/conf/layer.conf`

if [[ $VERSION =~ "kirkstone" ]]; then
    export BB_ENV_PASSTHROUGH_ADDITIONS="MACHINE DISTRO TCLIBC TCMODE GIT_PROXY_COMMAND http_proxy ftp_proxy https_proxy all_proxy ALL_PROXY no_proxy SSH_AGENT_PID SSH_AUTH_SOCK BB_SRCREV_POLICY SDKMACHINE BB_NUMBER_THREADS"
elif [[ $VERSION =~ "dunfell" ]]; then
    export BB_ENV_EXTRAWHITE="MACHINE DISTRO TCLIBC TCMODE GIT_PROXY_COMMAND http_proxy ftp_proxy https_proxy all_proxy ALL_PROXY no_proxy SSH_AGENT_PID SSH_AUTH_SOCK BB_SRCREV_POLICY SDKMACHINE BB_NUMBER_THREADS"
else
    echo "ERROR: not support ${VERSION}"
fi

# Helper command for building images for mixed 32bit/64bit
# ARM builds. The command allow to specify a secondary MACHINE
# and image that will be built next to the primary target.
# If no secondary image is specified the rpb-minimal-image image
# will be built.


bitbake_secondary_image () {
    BITBAKE_OPTIONS=""
    unset EXTRA_MACHINE
    unset SECONDARY_IMAGE

    SECONDARY_IMAGE='rpb-minimal-image'
    while [[ $# -gt 1 ]]
    do
        key="$1"
        case $key in
            --extra-machine)
            EXTRA_MACHINE="$2"
            shift
            ;;
            --secondary-image)
            SECONDARY_IMAGE="$2"
            shift
            ;;
        *)
            BITBAKE_OPTIONS=$BITBAKE_OPTIONS" "$1
        ;;
    esac
    shift
    done
    BITBAKE_OPTIONS=$BITBAKE_OPTIONS" "$1
    if [ -z "$EXTRA_MACHINE" ]
    then
        echo "   Error: you need to run $FUNCNAME with --extra-machine agument"
        echo
        echo "   Example:"
        echo "     $ $FUNCNAME --extra-machine hikey rpb-weston-image"
        return
    fi

    if [ "$EXTRA_MACHINE" == "$MACHINE" ]
    then
        echo "Error: the extra machine must be different from the machine you already set using setup-environment: "$MACHINE
        return
    fi

    echo "Building first image. MACHINE:" $EXTRA_MACHINE " DISTRO:" $DISTRO

    MACHINE=$EXTRA_MACHINE bitbake $SECONDARY_IMAGE

    if [ $? != 0 ]; then
        printf "Error building image"
        return
    fi
    echo "Building second image. MACHINE:" $MACHINE " DISTRO:" $DISTRO

    MACHINE=$MACHINE bitbake $BITBAKE_OPTIONS
}

mkdir -p "${BUILDDIR}"/conf && cd "${BUILDDIR}"

#if [ -f "conf/auto.conf" ]; then
#    oldmach=$(egrep "^MACHINE" "conf/auto.conf" | sed -e 's%^MACHINE ?= %%' | sed -e 's/^"//'  -e 's/"$//')
#fi
#if [ -e conf/checksum -a "${MACHINE}" = "$oldmach" ]
#then
#    sha512sum --quiet -c conf/checksum > /dev/null 2>&1
#    if [ $? -eq 0 ]
#    then
#       return
#    fi
#fi

# evaluate new checksum and regenerate the conf files
sha512sum "${SCRIPTS}"/setup-environment-internal 2>&1 > conf/checksum

# if conf already exit, reset it
if [ -f "conf/local.conf" ]; then
    echo "reset conf/local.conf"
    sed -i '/USERDEBUG=/d' ${SCRIPTS}/conf/local.conf
    sed -i '/SECBOOT_ENABLE=/d' ${SCRIPTS}/conf/local.conf
    sed -i '/ENG_IT=/d' ${SCRIPTS}/conf/bblayers.conf
fi

# add version related Var to local.conf, only in frist.
if [ ! -f "${BUILDDIR}/conf/local.conf" ]; then
    echo "version related Var"
    if [[ $VERSION =~ "kirkstone" ]]; then
        echo "#version related Var follow ${SCRIPTS}/conf/kirkstone-version-var.conf" >> ${SCRIPTS}/conf/local.conf
        cat ${SCRIPTS}/conf/kirkstone-version-var.conf >> ${SCRIPTS}/conf/local.conf
        echo "#end version related Var" >> ${SCRIPTS}/conf/local.conf
    elif [[ $VERSION =~ "dunfell" ]]; then
        echo "#version related Var follow ${SCRIPTS}/conf/dunfell-version-var.conf" >> ${SCRIPTS}/conf/local.conf
        cat ${SCRIPTS}/conf/dunfell-version-var.conf >> ${SCRIPTS}/conf/local.conf
        echo "#end version related Var" >> ${SCRIPTS}/conf/local.conf
    else
        echo "no version related Var"
    fi
fi

# link scripts conf to build/conf
ln -sf "${SCRIPTS}"/conf/local.conf conf/local.conf
ln -sf "${SCRIPTS}"/conf/bblayers.conf conf/bblayers.conf

# set USERDEBUG in local.conf
echo "USERDEBUG: ${USERDEBUG}"
if [ "$USERDEBUG" == "userdebug" ]; then
    echo 'USERDEBUG="userdebug"' >> conf/local.conf
    echo 'ENG_IT="userdebug"' >> conf/bblayers.conf
elif [ "$USERDEBUG" == "user" ]; then
    echo 'USERDEBUG="user"' >> conf/local.conf
    echo 'ENG_IT="user"' >> conf/bblayers.conf
elif [ "$USERDEBUG" == "eng" ]; then
    echo 'USERDEBUG="userdebug"' >> conf/local.conf
    echo 'ENG_IT="eng"' >> conf/bblayers.conf
fi

# set SECBOOT_ENABLE in local.conf
echo "SECBOOT_ENABLE: ${SECBOOT_ENABLE}"
if [ "$SECBOOT_ENABLE" == "nosec" ]; then
    echo 'SECBOOT_ENABLE="nosec"' >> conf/local.conf
elif [ "$SECBOOT_ENABLE" == "sec" ]; then
    echo 'SECBOOT_ENABLE="sec"' >> conf/local.conf
fi

ln -sf "${SCRIPTS}"/README.md README.md

#ln -sf "${SCRIPTS}" "${OEROOT}"/layers/

DISTRO_DIRNAME=$(echo "${DISTRO}" | sed 's#[.-]#_#g')

cat > conf/auto.conf <<EOF
DISTRO ?= "${DISTRO}"
MACHINE ?= "${MACHINE}"
SDKMACHINE ?= "${SDKMACHINE}"
UNISOC_PRODUCT ?= "${UNISOC_PRODUCT}"

# Extra options that can be changed by the user
INHERIT += "rm_work"
EOF
if [ ! -e conf/site.conf ]; then
    cat > conf/site.conf <<_EOF

SCONF_VERSION = "1"

# Where to store sources
DL_DIR = "${OEROOT}/source/tarballs"

# Where to save shared state
SSTATE_DIR = "${OEROOT}/sstate-cache"

TMPDIR = "${BUILDDIR}/tmp-${DISTRO_DIRNAME}"

# Go through the Firewall
#HTTP_PROXY        = "http://${PROXYHOST}:${PROXYPORT}/"

SSTATE_MIRRORS ?= "file://.* file:///yocto_cache/\${DISTRO_CODENAME}_25c/\${MACHINE}//PATH"

_EOF
fi

# Handle EULA , if needed. This is a generic method to handle BSPs
# that might (or not) come with a EULA. If a machine has a EULA, we
# assume that its corresponding layers has conf/EULA/$MACHINE file
# with the EULA text, which we will display to the user and request
# for acceptance. If accepted, the variable ACCEPT_EULA_$MACHINE is
# set to 1 in auto.conf, which can later be used by the BSP.
# If the env variable EULA_$MACHINE is set it is used by default,
# without prompting the user.
# FIXME: there is a potential issue if the same $MACHINE is set in more than one layer.. but we should assert that earlier
EULA=$(find ../layers -path "*/conf/eula/$MACHINE" -print | grep -v scripts | grep -v openembedded-core | grep -v meta-linaro || true)

if [ -n "$EULA" ]; then

    # remove '-' since we are constructing a bash variable name here
    EULA_MACHINE="EULA_$(echo "$MACHINE" | sed 's/-//g')"

    # NOTE: indirect reference / dynamic variable
    if [ -n "${!EULA_MACHINE}" ]; then
        # the EULA_$MACHINE variable is set in the environment, so we just configure
        # ACCEPT_EULA_$MACHINE in auto.conf
        echo "ACCEPT_EULA_$MACHINE = \"${!EULA_MACHINE}\"" >> conf/auto.conf
    else
        # so we need to ask user if he/she accepts the EULA:
        cat <<EOF

The BSP for $MACHINE depends on packages and firmware which are covered by an End
User License Agreement (EULA). To have the right to use these binaries
in your images, you need to read and accept the following...

EOF

        echo
        REPLY=
        while [ -z "$REPLY" ]; do
            echo -n "Would you like to read the EULA ? (y/n) "
            read -r REPLY
            case "$REPLY" in
                y|Y)
                    READ_EULA=1
                    ;;
                n|N)
                    READ_EULA=0
                    ;;
                *)
                    REPLY=
                    ;;
            esac
        done

        if [ "$READ_EULA" = 1 ]; then
            more -d "${EULA}"
            echo
            REPLY=
            while [ -z "$REPLY" ]; do
                echo -n "Do you accept the EULA you just read? (y/n) "
                read -r REPLY
                case "$REPLY" in
                    y|Y)
                        echo "EULA has been accepted."
                        echo "ACCEPT_EULA_$MACHINE = \"1\"" >> conf/auto.conf
                        ;;
                    n|N)
                        echo "EULA has not been accepted."
                        ;;
                    *)
                        REPLY=
                        ;;
                esac
            done
        fi
    fi
fi

cat <<EOF

Welcome to OpenEmbedded Reference Platform Build

For more information about OpenEmbedded see their website:
    http://www.openembedded.org/

Your build environemnt has been configured with:

    MACHINE = ${MACHINE}
    SDKMACHINE = ${SDKMACHINE}
    DISTRO = ${DISTRO}
    USERDEBUG = ${USERDEBUG}
    SECBOOT_ENABLE = ${SECBOOT_ENABLE}
    UNISOC_PRODUCT = ${UNISOC_PRODUCT}

You can now run 'bitbake <target>'

Some of common targets are:
    unisoc-wayland-image
    unisoc-console-image
    unisoc-prerootfs-image

EOF
