#! /bin/sh
#
# Installation script for Axis Developer Boards.

DEFAULT_DEV_BOARD_PRODUCT="devboard_82+"

FUNCTIONS_REV=R1_8_1
FUNCTIONS_PATH=configure-files/source-$FUNCTIONS_REV
FUNCTIONS_FILE=$FUNCTIONS_PATH/functions
FUNCTIONS_PKT=`echo $FUNCTIONS_PATH|sed 's/\//-/g'`
FUNCTIONS_SUFFIX=".tar.gz .tgz"
DISTFILES=distfiles
REPOSITORY_ROOT="http://developer.axis.com/download/distribution"

if [ $# -ge 1 ]; then
    if [ ! -f $1/.devboard_platform ]; then
	echo "  * Error $1 is not a supported platform"
	exit 1
    fi
    DEV_BOARD_PRODUCT=$1
    shift
else
    ALTERNATIVES=`ls */.devboard_platform|sed 's/\/\.devboard_platform//g'`
fi

echo "*** Install script for Axis Developer Board/Device Server SDK  ***"
echo "=================================================================="

if ! wget -V >/dev/null; then 
    echo "  * Error wget not found, wget is needed to fetch the necessary packages!"
    exit 1
fi

if [ ! -d "$DISTFILES" ]; then
    mkdir $DISTFILES
    echo " If you already have an SDK installed, you can let this script"
    echo " copy distribution files from that installation. This can possibly"
    echo " reduce the amount of data that has to be downloaded."
    read -p  " * Do you want to copy distfiles now [yn]? (default: n): " _copy_distfiles
    echo ""
    while [ "$_copy_distfiles" = "y" ];do
	echo " Enter the path to the installed SDK which contain a $DISTFILES directory."
	echo " For example: \"../devboard-R2_00\""
	read -p " * Path: " _dist_path
	if [ -d $_dist_path/$DISTFILES ];then
	    echo -n " Copying files..."
	    for _file in $_dist_path/$DISTFILES/*;do
		[ -f $_file ] &&
		[ -f $_file.md5 ] &&
		cp $_file $DISTFILES &&
		cp $_file.md5 $DISTFILES
	    done
	    echo " done"
	    echo ""
	    _copy_distfiles=done
	else
	    echo " Could not locate: $_dist_path/$DISTFILES"
	    read -p " * Would you like to try again [yn] (default: n)? " _copy_distfiles
	fi
    done
fi

### Get the funtions file and source it
if [ ! -f "$FUNCTIONS_FILE" ]; then
    _gotit=no
    for _suffix in $FUNCTIONS_SUFFIX; do
	if [ -f $DISTFILES/$FUNCTIONS_PKT$_suffix ]; then
	    mv $DISTFILES/$FUNCTIONS_PKT$_suffix .
	    _gotit=yes
	    break
	fi
    done
    if [ $_gotit = "no" ]; then        
        for _suffix in $FUNCTIONS_SUFFIX; do
	    if wget -nc $REPOSITORY_ROOT/$FUNCTIONS_PKT$_suffix; then
		_gotit=yes
		break
	    fi
	done
    fi
    if [ $_gotit = "no" ]; then
	echo "  * Error fetching $FUNCTIONS_FILE"
	exit 1
    fi
    if ! tar zxf $FUNCTIONS_PKT$_suffix; then
	echo "  * Error uncompressing $FUNCTIONS_PKT$_suffix"
	exit 1
    fi
    if ! mv $FUNCTIONS_PKT$_suffix $DISTFILES; then
	echo "  * Error removing $FUNCTIONS_PKT$_suffix"
	exit 1
    fi
    if [ ! -f $FUNCTIONS_FILE ]; then
	echo "  * Error $FUNCTIONS_FILE revision $FUNCTIONS_REV is not found"
	exit 1
    fi
    ln -sfn source-$FUNCTIONS_REV configure-files/source
fi

. $FUNCTIONS_FILE

### Setup repository root
repository_root "$REPOSITORY_ROOT"

### Get the tools directory, needed for aconfig
tool tools/build rev=R2_12_4

### Set up aconfig (AXIS configure)
aconfig_targets configure-files/common/AC_common

### Get config packets
fetch configure-files/devboard/devboard rev=tag--devboard-R2_01
fetch configure-files/common/common rev=tag--devboard-R2_01

### Get AC_... files
fetch configure-files/common/AC_common rev=tag--devboard-R2_01
fetch configure-files/devboard/AC_common rev=tag--devboard-R2_01
fetch configure-files/devboard/AC_general rev=tag--devboard-R2_01
fetch configure-files/devboard/AC_network rev=tag--devboard-R2_01
fetch configure-files/devboard/AC_web rev=tag--devboard-R2_01

output Makefile

### Preconfigure a product
echo ""
echo " Select wich product to use. This selection will affect which"
echo " configuration files the SDK will use. Select the product in"
echo " the list below that closest resemblels your product."
echo ""
echo " Alternatives:"
if [ -z $DEV_BOARD_PRODUCT ]; then
    i=0
    for arg in $ALTERNATIVES 
      do
      i=$(($i+1))
      DESCR=`cat $arg/.devboard_platform`
      echo  "  $i. $arg  -  \"$DESCR\""
      if [ $arg = $DEFAULT_DEV_BOARD_PRODUCT ]; then
	  DEFAULT_INDEX=$i
      fi
    done
    MAX_INDEX=$i
    read -p  " * Specify product (default: $DEFAULT_INDEX. $DEFAULT_DEV_BOARD_PRODUCT): " DEV_BOARD_PRODUCT_INDEX
    [ -z $DEV_BOARD_PRODUCT_INDEX ] && DEV_BOARD_PRODUCT_INDEX=$DEFAULT_INDEX
    
    case "$DEV_BOARD_PRODUCT_INDEX" in
	[0-$MAX_INDEX])
	#Ok
	    ;;
	*)
	    echo "*** Error $DEV_BOARD_PRODUCT_INDEX is not a valid option"
	    exit 1
	    ;;
    esac
    
    if [ "$DEV_BOARD_PRODUCT_INDEX" -ge "1" ] && [ "$DEV_BOARD_PRODUCT_INDEX" -le "$MAX_INDEX" ]; then
	i=0
	for arg in $ALTERNATIVES 
	  do
	  i=$(($i+1))
	  if [ $i = $DEV_BOARD_PRODUCT_INDEX ]; then
	      DEV_BOARD_PRODUCT=$arg
	  fi
	done
	[ -z "$DEV_BOARD_PRODUCT" ] && DEV_BOARD_PRODUCT=$DEFAULT_DEV_BOARD_PRODUCT
	if [ ! -f $DEV_BOARD_PRODUCT/defconfig ]; then
	    echo "*** Uknown product $DEV_BOARD_PRODUCT, $DEV_BOARD_PRODUCT/.config"
	    echo "  * file doesn't exist"
	    exit 1
	fi
    else
	echo "*** Illegal index"
	exit 1
    fi
fi

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
echo ""
echo " Configured for: $DEV_BOARD_PRODUCT - Install complete"
echo ""

### Next step
echo " +-------------------------------------------------------------+"
echo " | To setup product (Next step):                               |"
echo " |                                                             |"
echo " | Optional              Required                              |"
echo " | ---------------       ----------------------                |"
echo " | 1.                    2.                3.                  |"
echo " | make config      -->  ./configure  -->  make                |"
echo " | make menuconfig                                             |"
echo " | make xconfig                                                |"
echo " |                                                             |"
echo " | If the configuration step is skipped (the first step) the   |"
echo " | default configuration will be used when building the image. |"
echo " |                                                             |"
echo " | Use ./configure --help for more information about           |"
echo " | configure options.                                          |"
echo " +-------------------------------------------------------------+"
exit 0
