#! /bin/sh
#
# Configuration script for Developer Boards.

# Do not change or delete the "Configure version:" tag, 
# it is vital for the update script!
# Configure version: R2_01-0

SOURCE_REV=R1_8_1
COMPR_SUFFIXES=".tar.gz .tgz"
SOURCE="configure-files/source"
FUNCTIONS_FILE="configure-files/source/functions"
PRE_FILE="configure-files/pre"
POST_FILE="configure-files/post"
#Packages are named with the revision and '-' exhchanged with '/'
SOURCE_PKT="configure-files-source-"$SOURCE_REV
ROOT="http://developer.axis.com/download/distribution"
DISTFILES=distfiles
_default_product="devboard_82+"

# Include customer preconfigure file if it exist
[ -f "$PRE_FILE" ] && . $PRE_FILE

cp_product_files () {
    if [ -f $DEV_BOARD_PRODUCT/.devboard_platform ]; then
	cp $DEV_BOARD_PRODUCT/defconfig .config
	cp $DEV_BOARD_PRODUCT/kernelconfig-2.6 .
	cp $DEV_BOARD_PRODUCT/kernelconfig .
	ln -sf $DEV_BOARD_PRODUCT/.devboard_platform
    else
	echo "*** Error: no such product \"$DEV_BOARD_PRODUCT\"" >&2
	echo "*** Available products:" >&2
	for _file in *;do
	    [ -d $_file ] &&
	    [ -f $_file/.devboard_platform ] &&
	    echo -e "\t$_file" >&2
	done
	exit 1
    fi
    return 0
}

if [ -z "$DEV_BOARD_PRODUCT" ]; then
    if [ -L .devboard_platform ]; then
	DEV_BOARD_PRODUCT=$(readlink .devboard_platform | sed -e 's/\.devboard_platform//' -e 's/\///')
	echo "### Selected product: \"$DEV_BOARD_PRODUCT\" ###" >&2
    else
	DEV_BOARD_PRODUCT=$_default_product
	echo "### Selected product: \"$DEV_BOARD_PRODUCT\" ###" >&2
	echo "    To select another product run:"
	echo "    DEV_BOARD_PRODUCT=<product name> ./configure"
	cp_product_files
    fi
else
    if [ -L .devboard_platform ] && [ "$DEV_BOARD_PRODUCT" = "$(readlink .devboard_platform | sed -e 's/\.devboard_platform//' -e 's/\///')" ];then
        echo "### Selected product: \"$DEV_BOARD_PRODUCT\" ###" >&2
    else
    	echo "The specified product is different than the currently configured. Changing product"
	echo "will overwrite configuration files for the currently configured product."
	read -p "Do you want to change product [yn]? " _CH_PROD
	if [ "$_CH_PROD" = "y" ];then
	   echo "### Selected product: \"$DEV_BOARD_PRODUCT\" ###" >&2
	   cp_product_files
	   [ -f os/linux/.config ] && rm os/linux/.config
	   [ -f os/linux-2.6/.config ] && rm os/linux-2.6/.config
	else
	   DEV_BOARD_PRODUCT=$(readlink .devboard_platform | sed -e 's/\.devboard_platform//' -e 's/\///')
	   echo "### Selected product: \"$DEV_BOARD_PRODUCT\" ###" >&2
	fi
    fi
fi


if [ ! -f "$FUNCTIONS_FILE" ]; then
    case "$ROOT" in
	:ext:*|:pserver:*)
	    cvs -d "$ROOT" co -r "$SOURCE_REV" "$SOURCE" || exit 1
	    ;;
	
	ftp://*|http://*|https://*)
	    if ! wget -V >/dev/null; then 
		exit 1
	    fi
	    _gotit=no
	    for _suffix in $COMPR_SUFFIXES; do
		if wget -nc -q $ROOT/$SOURCE_PKT$_suffix; then
		    _gotit=yes
		    break
		fi
	    done
	    echo "** Fetched \"$SOURCE\""
	    if [ "$_gotit" = "no" ]; then
		exit 1
	    fi
	    if ! tar zxf $SOURCE_PKT$_suffix; then
		exit 1
	    fi
	    if ! rm -f $SOURCE_PKT$_suffix; then
		exit 1
	    fi
	    ln -sfn source-$SOURCE_REV $SOURCE
	    ;;
	
	*)
	    exit 1
	    ;;
    esac
fi

. "$FUNCTIONS_FILE" || exit 1

repository_root "$ROOT"
if ! conditional_reexec "$SOURCE" rev="$SOURCE_REV"; then
    echo "*** Error: in conditional_reexec" >&2
    exit 1
fi

. .config || exit 1

include configure-files/devboard/devboard rev=tag--devboard-R2_01

# Release information
DEV_BOARD_VERSION="2.01"

case $DEV_BOARD_PRODUCT in
    devboard_82)
	DEV_BOARD_PART_NUMBER="00000"
	;;
    devboard_82+)
	DEV_BOARD_PART_NUMBER="00000"
	;;
    devboard_lx)
	DEV_BOARD_PART_NUMBER="00000"
	;;
    devboard_lx_ide)
	DEV_BOARD_PART_NUMBER="00000"
	;;
    *)
	DEV_BOARD_PART_NUMBER="00000"
	;;
esac

release "$AXIS_CONFIG_PRODUCT_FULL_NAME release $DEV_BOARD_VERSION" part=$DEV_BOARD_PART_NUMBER
mkdir -p packages/param/brand/devboard
echo "PRODFULLNAME=\"Axis Developer Board\"" > packages/param/brand/devboard/brand.conf

# Factorydefault image with JIMAGEID for automatic factorydefault
jimage "$DEV_BOARD_VERSION"

# Include customer postconfigure file if it exist
[ -f "$POST_FILE" ] && . $POST_FILE

# Generate Makefile
output Makefile
