#!/bin/bash

# Copyright (C) 2013 Red Hat Inc.
# SPDX-License-Identifier:  GPL-2.0+

# This script will download and install uboot

# usage message
usage() {
    echo "
Usage: $(basename ${0}) <options>

   --target=TARGET  - target board
   --list-targets   - list supported boards
   --media=DEVICE   - media device file (/dev/[sdX|mmcblkX])
   --tag=KOJI TAG   - koji tag to download build

Example: $(basename ${0}) --target=panda --media=/dev/mmcblk0"
}

# check the args
while [ $# -gt 0 ]; do
        case $1 in
                --debug)
                        set -x
                        ;;
                -h|--help)
                        usage
                        ;;
                --target*)
                        if echo $1 | grep '=' >/dev/null ; then
                                TARGET=$(echo $1 | sed 's/^--target=//')
                        else
                                TARGET=$2
                                shift
                        fi
                        ;;
                --media*)
                        if echo $1 | grep '=' >/dev/null ; then
                                MEDIA=$(echo $1 | sed 's/^--media=//')
                        else
                                MEDIA=$2
                                shift
                        fi
                        ;;
                --tag*)
                        if echo $1 | grep '=' >/dev/null ; then
                                KOJI_TAG=$(echo $1 | sed 's/^--tag=//')
                        else
                                KOJI_TAG=$2
                                shift
                        fi
                        ;;
                --list-targets)
                        LISTTARGETS=1
                        ;;
                *)
                        echo "$(basename ${0}): Error - ${1}"
                        usage
                        exit 1
                        ;;
        esac
        shift
done

if [ -d "/usr/share/arm-image-installer/boards.d" ]; then
        BOARDDIR="/usr/share/arm-image-installer/boards.d"
else
        DIR=$(dirname $0)
        BOARDDIR="${DIR}/boards.d"
fi

if [ ! -z "$LISTTARGETS" ]; then
        cd $BOARDDIR
        echo "Supported boards are:"
        echo ""
        echo "AllWinner SoC based:"
        ALLWINNER=$(find . -type l -ls|grep AllWinner|cut -d"/" -f2|cut -d" " -f1|sort)
        echo $ALLWINNER | sed -e 's/[[:space:]]/ /g' | fold -w 80 -s

        echo ""
        echo "i.Mx6 SoC based:"
        MX6=$(find . -type l -ls|grep /imx6|cut -d"/" -f2|cut -d" " -f1|sort)
        echo $MX6 | sed -e 's/[[:space:]]/ /g' | fold -w 80 -s

        echo ""
        echo "TI SoC based:"
        OMAP=$(find . -type l -ls|grep /omap|cut -d"/" -f2|cut -d" " -f1|sort)
        echo $OMAP | sed -e 's/[[:space:]]/ /g' | fold -w 80 -s

        echo ""
        echo "Marvell SoC based:"
        MVEBU=$(find . -type l -ls|grep /mvebu|cut -d"/" -f2|cut -d" " -f1|sort)
        echo $MVEBU | sed -e 's/[[:space:]]/ /g' | fold -w 80 -s

        TARGETS=$(ls -1 ${BOARDDIR})
        TARGETS=$(echo ${TARGETS} | sed -e 's/[[:space:]]/ /g')

        for board in $TARGETS
        do
                if [[ none == $board || $ALLWINNER =~ $board || $MX6 =~ $board || $OMAP =~ $board || $MVEBU =~ $board ]]; then
                        A=1
                else
                        OTHER="$OTHER $board"
                fi
        done
        echo ""
        echo "Other SoC based:"
        echo $OTHER
        exit 0
fi


# check if media exists
if [[ ! -e $MEDIA ]] ; then
        echo "Missing media"
        usage
        exit 1
fi

if [[ $TARGET = '' ]] ; then
        echo "Missing target"
        usage
        exit 1
fi
if [[ $KOJI_TAG != '' ]] ; then
        if [[ ! -f /usr/bin/koji ]]; then
                echo "Please install koji-utils for this option."
                exit 1
        else
                PREFIX='/tmp/root/'
                sudo rm -rf /tmp/root &> /dev/null
                mkdir $PREFIX

                #get the latest uboot
                pushd $PREFIX &> /dev/null
                if [ $KOJI_TAG = f22 ]; then
                        koji download-build --arch=armv7hl --latestfrom=$KOJI_TAG uboot-tools
                else
                        koji download-build --arch=noarch --latestfrom=$KOJI_TAG uboot-tools
                fi
                # unpack uboot
                rpm2cpio uboot-images*.rpm | cpio -idv &> /dev/null
                popd &> /dev/null
        fi
fi
# determine uboot and write to disk 
if [ "$TARGET" = "" ]; then
        echo "= No U-boot will be written."
        TARGET="Mystery Board"
else
        . "${DIR}/${BOARDDIR}/${TARGET}"
fi

echo "= Complete!"

# vi: tabstop=8 softtabstop=0 expandtab shiftwidth=8 smarttab
