#!/bin/bash
#
# Copyright 2015 Ettore Chimenti <ek5.chimenti@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#

set -e

dts_dir="arch/arm/boot/dts/"
preudoo="imx6sx-udoo-neo"

boards=( basicks basic extended full )
videos=( hdmi lvds7 lvds15 NONE )

HEAD="/*
* Author: Ettore Chimenti <ek5.chimenti@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This dts is been autogenerated by scripts/make-udoo-dtbs
*
*/

/dts-v1/;

#include \"imx6sx-udoo-neo.dtsi\""

dtsi_chooser(){

  DTSI="${preudoo}-${1}.dtsi"

  if [ ! -f "${dts_dir}/$DTSI" ] 
  then 
    echo "ERR: DTSI for UDOO NEO $b not found" >&2 
    exit 1 
  fi

  echo $DTSI
}


for b in ${boards[*]}
do
  unset BOARD B

  if [ $b != 'NONE' ]
  then

    DTSI=$(dtsi_chooser $b)
    B=-$b
    BOARD="#include \"$DTSI\"\n"

  fi

  for m in m4 NONE
  do
    unset M4 M

    if [ $m != 'NONE' ]
    then

      M=-$m
      [[ $b =~ basic ]] && m="basic-m4"

      DTSI=$(dtsi_chooser $m)
      M4="#include \"$DTSI\"\n"

    fi

    for v in ${videos[*]}
    do
      unset VIDEO V

      if [ $v != 'NONE' ]
      then

        DTSI=$(dtsi_chooser $v)
        V=-$v
        VIDEO="#include \"$DTSI\"\n"

      fi

      DTS="$dts_dir/${preudoo}${B}${V}${M}.dts"

      cat << EOF > "$DTS"
$HEAD 
EOF

      DTSI=$(dtsi_chooser externalpins)
      EXT="#include \"$DTSI\"\n"
      
      echo -en ${BOARD}${M4}${VIDEO}${EXT} >> "$DTS"

      unset DTS

    done
  done
done

