function help() {
cat <<EOF
Invoke ". build/envsetup.sh" from your shell to add the following functions to your environment:

== before all ==
- config:        config <arch_name>

== build project ==
- mbase:       Build ice_base.
- mxfce:       Build ice_xfce.
- mgnome:      Build ice_gnome.
- msdk:        Build SDK.

== jump directory ==
- ctarget:   Jump to target directory.
- cdeb:    Jump to target deb directory.
- cimage:  Jump to target image directory.

== grep file ==
- cgrep:    Greps on all local C/C++ files.

Look at the source to view more functions. The complete list is:
EOF
    T=$(gettop)
    local A
    A=""
    for i in `cat $T/openembedded-core/envsetup.sh | sed -n "/^[ \t]*function /s/function \([a-z_]*\).*/\1/p" | sort | uniq`; do
      A="$A $i"
    done
    echo $A
}
function cgrep()
{
    find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f \( -name '*.c' -o -name '*.cc' -o -name '*.cpp' -o -name '*.h' \) -print0 | xargs -0 grep --color -n "$@"
}

function gettop
{
    local TOPFILE=openembedded-core/envsetup.sh
    if [ -n "$MPSOC_TOP" -a -f "$MPSOC_TOP/$TOPFILE" ] ; then
        # The following circumlocution ensures we remove symlinks from TOP.
        (\cd $MPSOC_TOP; PWD= /bin/pwd)
    else
        if [ -f $TOPFILE ] ; then
            # The following circumlocution (repeated below as well) ensures
            # that we record the true directory name and not one that is
            # faked up with symlink names.
            PWD= /bin/pwd
        else
            local here="${PWD}"
            while [ "${here}" != "/" ]; do
                if [ -f "${here}/${TOPFILE}" ]; then
                    (\cd ${here}; PWD= /bin/pwd)
                    break
                fi
                here="$(dirname ${here})"
            done
        fi
    fi
}

function mbase()
{
	local T=$(gettop)
    [ -z "$T" ] \
    && echo "Couldn't locate the top of the tree.  Try setting TOP." \
    && return

	if [ "$1" ] ; then
        config $1
    fi
	export YOCTO_TARGET_IMAGE_DIR="thead-build/ice-base"
	echo -e
	echo "YOCTO_TARGET_IMAGE_DIR=$YOCTO_TARGET_IMAGE_DIR"
	echo -e
    if [ -e $T/openembedded-core/oe-init-build-env ] ; then
      source $T/openembedded-core/oe-init-build-env $T/$YOCTO_TARGET_IMAGE_DIR
	  bitbake core-image-minimal
	fi
}

function msdk()
{
	local T=$(gettop)
    [ -z "$T" ] \
    && echo "Couldn't locate the top of the tree.  Try setting TOP." \
    && return

	if [ "$1" ] ; then
        config $1
    fi
	export YOCTO_TARGET_IMAGE_DIR="thead-build/ice-base"
	echo -e
	echo "YOCTO_TARGET_IMAGE_DIR=$YOCTO_TARGET_IMAGE_DIR"
	echo -e
    if [ -e $T/openembedded-core/oe-init-build-env ] ; then    
	    source $T/openembedded-core/oe-init-build-env $T/$YOCTO_TARGET_IMAGE_DIR
	    bitbake core-image-minimal -c populate_ext
	fi
}

function mxfce()
{
	local T=$(gettop)
    [ -z "$T" ] \
    && echo "Couldn't locate the top of the tree.  Try setting TOP." \
    && return

	if [ "$1" ] ; then
        config $1
    fi
	export YOCTO_TARGET_IMAGE_DIR="thead-build/ice-xfce"
	echo -e
	echo "YOCTO_TARGET_IMAGE_DIR=$YOCTO_TARGET_IMAGE_DIR"
	echo -e
    if [ -e $T/openembedded-core/oe-init-build-env ] ; then
        source $T/openembedded-core/oe-init-build-env $T/$YOCTO_TARGET_IMAGE_DIR
	    bitbake core-image-minimal-xfce
	fi
}

function mgnome()
{
	local T=$(gettop)
    [ -z "$T" ] \
    && echo "Couldn't locate the top of the tree.  Try setting TOP." \
    && return

	if [ "$1" ] ; then
        config $1
    fi
	export YOCTO_TARGET_IMAGE_DIR="thead-build/ice-gnome"
	echo -e
	echo "YOCTO_TARGET_IMAGE_DIR=$YOCTO_TARGET_IMAGE_DIR"
	echo -e
    if [ -e $T/openembedded-core/oe-init-build-env ] ; then
        source $T/openembedded-core/oe-init-build-env $T/$YOCTO_TARGET_IMAGE_DIR
	    bitbake core-image-gnome
	fi
}

function ctarget()
{
    T=$(gettop)
    local T=$(gettop)
    [ -z "$T" ] \
        && echo "Couldn't locate the top of the tree.  Try setting TOP." \
        && return
    \cd $T/$YOCTO_TARGET_IMAGE_DIR
}

function cdeb()
{
    T=$(gettop)
    local T=$(gettop)
    [ -z "$T" ] \
        && echo "Couldn't locate the top of the tree.  Try setting TOP." \
        && return
    \cd $T/$YOCTO_TARGET_IMAGE_DIR/tmp-glibc/deploy/deb
}

function cimage()
{
    T=$(gettop)
    local T=$(gettop)
    [ -z "$T" ] \
        && echo "Couldn't locate the top of the tree.  Try setting TOP." \
        && return
    \cd $T/$YOCTO_TARGET_IMAGE_DIR/tmp-glibc/deploy/images
}

function gen_config_combo()
{
	export ARCH_MENU_CHOICES=(riscv csky)
}

function print_config_menu()
{
    local uname=$(uname)
    echo
    echo "You're building on" $uname
    echo
    echo "Config menu... pick a combo:"

    local i=1
    local choice
    for choice in ${ARCH_MENU_CHOICES[@]}
    do
        echo "     $i. $choice"
        i=$(($i+1))
    done
    echo
}

 
function config
{
    local T=$(gettop)
	[ -z "$T" ] && return -1
	
	local last=riscv
	
	# install config menu
	gen_config_combo

    # select platform
    local select
    if [ "$1" ] ; then
        select=$1
    else
        print_config_menu
        echo -n "Which would you like?"
        [ -n "${last}" ] && echo -n " [Default ${last}]"
        echo -n ": "
        read select
    fi

    if [ -z "${select}" ]; then
        select=${last}
    elif (echo -n $select | grep -q -e "^[0-9][0-9]*$"); then
        [ $select -le ${#ARCH_MENU_CHOICES[@]} ] \
            && select=${ARCH_MENU_CHOICES[$(($select-1))]}
    elif (echo -n $select | grep -q -e "^[^\-][^\-]*-[^\-][^\-]*$"); then
        select="$select"
    else
        echo
        echo "Invalid config combo: $select" >&2
        return 1
	fi
 	# config info
    export YOCTO_BUILD_TOP=$(gettop)
    export YOCTO_TARGET_ARCH="$select"

	# Get the exact value of a build variable.
	echo -e
	echo "YOCTO_BUILD_TOP=$(gettop)"
	echo "YOCTO_TARGET_ARCH=${YOCTO_TARGET_ARCH}"
	echo -e
	
	rm -rf $T/meta-arch
    ln -s $T/meta-$YOCTO_TARGET_ARCH  $T/meta-arch
}

function envsetup
{
    if [ "x$SHELL" != "x/bin/bash" ]; then
        case `ps -o command -p $$` in
            *bash*)
                ;;
            *)
                echo -n "WARNING: Only bash is supported, "
                echo "use of other shell would lead to erroneous results"
                ;;
        esac
    fi

    # check top of SDK
    if [ ! -f "${PWD}/openembedded-core/envsetup.sh" ]; then
        echo "ERROR: Please source envsetup.sh in the root of SDK"
        return -1
    else
        export YOCTO_TOP="$(PWD= /bin/pwd)"
    fi

    # reset these variables.
    export VARIANT_CHOICES=yocto

    # Execute the contents of any vendorsetup.sh files we can find.
    local vendors vendor
    verdors="$(find -L target -maxdepth 4 -name 'vendorsetup.sh' 2>/dev/null | sort)"
    for verdor in ${verdors}
    do
        source ${verdor}
    done
    echo "Setup env done! Please run config next."

	local T=$(gettop)
    [ -z "$T" ] \
    && echo "Couldn't locate the top of the tree.  Try setting TOP." \
    && return

	export YOCTO_TARGET_IMAGE_DIR="thead-build/ice-base"
    if [ -e $T/openembedded-core/oe-init-build-env ] ; then
        source $T/openembedded-core/oe-init-build-env $T/$YOCTO_TARGET_IMAGE_DIR >/dev/null
	fi
}

#### MAIN ####
envsetup

