# -*-shell-script-*-

# This file is loaded by the main build script and possibly
# sub scripts. It defines common utilities used at build time.
# All functions are prefixed with "wrs_" so they can be easily identified
# as local code within the scripts.

wrs_echo () {
    # actually, this echo is to stderr, for diagnostics
    echo -n "$(date "+%Y-%m-%d %H:%M:%S"): " >&2
    echo $* >&2
}

wrs_warn () {
    echo -n "$(date "+%Y-%m-%d %H:%M:%S"): " >& 2
    echo "Warning: $*" >& 2
}

wrs_err () {
    echo -n "$(date "+%Y-%m-%d %H:%M:%S"): " >& 2
    echo "Error: $*" >& 2
}

wrs_die () {
    if [ -n "$*" ]; then wrs_err $*; fi
    exit 1
}

# Set a default variable without typing the name twice as in VAR=${VAR:-val}
wrs_setenv_default () {
    name=$1
    value=$2
    if [ -z "$(eval echo \$$name)" ]; then
	eval ${name}="$value"
    fi
    export $name
}

# Show current variables to make the user confident things are fine
wrs_show_vars () {
    echo "";  echo "Building with the following parameters: "
    for n in $*; do
	val=$(env | grep "^$n=" | sed 's/^.*=//')
	# we may show optional variables (e.g.: CROSS_COMPILE)
	if [ "x$val" != "x" ]; then
	    printf "%-20s = %s\n" $n "$val"
	fi
    done
    # If the shell's stdout is a tty (not redirected), allow a delay
    stty -F /proc/$$/fd/1 2> /dev/null > /dev/null
    if [ $? -ne 0 ]; then return; fi

    echo -n "You may now ctrl-C for 3 seconds"
    for n in 1 2 3; do
	echo -n .; sleep 1
    done
    echo ""
}


wrs_check_tools () {
    local error=false
    for f in $*; do
	if [ -z "$(type -p $f)" ]; then
	    wrs_err "Can't find \"$f\" in your current PATH" >& 2
	    error=true
	fi
    done
    if $error; then
	wrs_die "Please install missing tools"
    fi
}

# While the function above is called once for all, the one below is called
# by each sub-script with a different list of required variables
wrs_check_vars () {
    local error=false
    for n in $*; do
        env | grep -q "^$n=" && continue
        wrs_echo "${WRS_SCRIPT_NAME}: Environment variable $n is not set" >&2
        error=true
    done
    if $error; then
	wrs_die
    fi
}

# Set and unset a variable in a config file. Used for buildroot/kernel stuff
# Activate and deactivate a configuration option
wrs_set_config () {
    local config
    if [ "x$2" = "x" ]; then config=".config"; else config="$2"; fi
    sed -i "s/# $1 is not set/$1=y/" $config
}

wrs_unset_config () {
    local config
    if [ "x$2" = "x" ]; then config=".config"; else config="$2"; fi
    sed -i "s/$1=y/# $1 is not set/" $config
}

# We'll need to set CROSS_COMPILE to the buildroot one, if unset
wrs_default_compiler () {
    hostdir=$WRS_OUTPUT_DIR/build/buildroot-2016.02/output/host
	if [ -e $hostdir/usr/bin/arm-linux-gcc ]; then
	    wrs_setenv_default CROSS_COMPILE $hostdir/usr/bin/arm-linux-
	fi
}

# See documentation about use of the download-info file. Unfortunately,
# a loop in a pipe is run in a sub-shell, so variables can't be just set.
# Therefore, we use a temporary file here
wrs_download () {
    file=$1
    DLLIST=${WRS_BASE_DIR}/download-info

    #wrs_echo "Looking for file $file"
    T=$(mktemp /tmp/wrs-download-XXXXXX)
    grep -v '^#' $DLLIST | grep -v '^[ 	]*$' | \
	while read name md5 upstream; do
	    #echo "md5 $md5 name $name u $upstream"
	    if [ "$name" != "$file" ]; then continue; fi
	    echo md5=\"$md5\" > $T
	    echo upstream=\"$upstream\" >> $T
	    break
    done
    . $T
    rm -f $T

    mkdir -p ${WRS_DOWNLOAD_DIR} || wrs_die "mkdir ${WRS_DOWNLOAD_DIR}"

    # If it exists, we are done
    output=${WRS_DOWNLOAD_DIR}/$file
    if [ -f $output ]; then
	ourmd5=$(cat $output | md5sum | sed 's/ .*$//')
	if [ "$ourmd5" = "$md5" ]; then
	    #wrs_echo "Skip download of $file"
	    return
	else
	    wrs_warn "File $file is corrupted, re-downloading"
	    rm $output
	fi
    fi

    # Download from upstream
    wget -T 15 -t 1 -q $upstream -O $output
    ourmd5=$(cat $output | md5sum | sed 's/ .*$//')
    if [ -s $output -a "$ourmd5" = "$md5" ]; then
	wrs_echo "Retrieved $file from upstream"
	return
    else
	rm -f $output
    fi

    # Download from buildroot
    wget -q http://sources.buildroot.net/$file -O $output
    ourmd5=$(cat $output | md5sum | sed 's/ .*$//')
    if [ -s $output -a "$ourmd5" = "$md5" ]; then
	wrs_echo "Retrieved $file from sources.buildroot.net"
	return
    else
	rm -f $output
    fi
    wrs_err "Cannot download $file"
}


# Every time wrs_functions is loaded, re-set WRS_MAKE_J
export WRS_MAKE_J_NUMBER=$(expr $(grep ^processor /proc/cpuinfo | wc -l) + 1)
export WRS_MAKE_J=-j$WRS_MAKE_J_NUMBER


# Also, check the configuration each time this is executed (i.e., each step)
WRS_CONFIG_DIR=$(dirname "$WRS_BASE_DIR")
if [ -f "$WRS_CONFIG_DIR/.config" ]; then
    # If Kconfig changed since last build, add missing items
    (cd "$WRS_CONFIG_DIR" && yes "" | make oldconfig)
else
    echo "No configuration, using default" >& 2
    (cd "$WRS_CONFIG_DIR" && make defconfig)
    if [ $? -ne 0 ]; then wrs_die "Configuration failed"; fi
fi

# buildroot definitions
buildroot_zipname="buildroot-2016.02.tar.bz2"
buildroot_dirname="buildroot-2016.02"

# and import the configuration variables
. $WRS_CONFIG_DIR/.config
