#!/bin/bash

# prompt default
ask_binary() {
  local YES
  local DEF
  if [ "$2" == "Y" ]; then
    DEF="[Y/n]"
  else
    DEF="[y/N]"
  fi
  while [ "$YES" != "Y" -a  "$YES" != "N" ]; do
    #echo -n "$1 $DEF "
    read -e -p "$1 $DEF " YES
    YES=${YES:0:1}
    if [ "$YES" == "y" ]; then YES="Y"; fi
    if [ "$YES" == "n" ]; then YES="N"; fi
    if [ "$YES" == "" ]; then YES="$2"; fi
  done
  if [ "$YES" == "Y" ]; then
    return 0
  else
    return 1
  fi
}

# prompt max
ask_number() {
  local N=0
  while (( $N <= 0 || $N > $2 )); do
    #echo -n "$1 [1-$2] "
    read -e -p "$1 [1-$2] " N
    if [ "$N" == "" ]; then N=0; fi
  done
  return $N
}

# prompt choice 1, choice 2, ...
ask_choice() {
  local n
  echo "$1"
  n=1
  shift
  while [ "$1" != "" ]; do
    echo "$n. $1"
    let n=$n+1
    shift
  done
  let n=$n-1
  ask_number "Enter number of your choice" $n
}

# list num
exclude_list() {
  local i
  local result
  local n=1
  for i in $1; do
    if (( $n != $2 )); then
      result="$result $i"
    fi
    let n=$n+1
  done
  echo $result
}

# list num
include_list() {
  local i
  local result
  local n=1
  for i in $1; do
    if (( $n == $2 )); then
      echo $i
      return
    fi
    let n=$n+1
  done
}

# list element
add_to_set() {
  local i
  for i in $1; do
    if [ "$i" == "$2" ]; then
      echo "$1"
      return
    fi
  done
  echo "$1 $2"
}

# arch
is_tssocket() {
  if [ "$1" == "ts4200" ]; then return 0; fi
  if [ "$1" == "ts4500" ]; then return 0; fi
  if [ "$1" == "ts4700" ]; then return 0; fi
  if [ "$1" == "ts4800" ]; then return 0; fi
  return 1;
}

# sub-arch
has_subsubarch() {
  if [ "$1" == "ts81x0" ]; then return 0; fi
  return 1;
}
has_subsubarch_subtest() {
  echo "false"
}

has_subsubarch_subarches() {
  echo "none tsrelay8"
}

is_tssocket_subtest() {
  echo "has_subsubarch"
}
is_tssocket_subarches() {
  echo "none ts81x0"
}

TOPS=""
ENVS="production debugging both"
# arch type
add_arch() {
  local N TMP LEN1 LEN2 LEN TMP2
  ask_choice "Which environment(s) do you want to compile $1 for?" $ENVS
  N=$?
  if [ "$TOP" == "" ]; then
    TMP=$1
  else
    TMP=$TOP
  fi
  TOPS=`add_to_set "$TOPS" $TMP`
  if (( $N & 1 )); then
    ALLARCHES="$ALLARCHES $1"
  fi
  if (( $N & 2 )); then
    LEN1=${#1}
    LEN2=${#TMP}
    let LEN=$LEN1-$LEN2
    TMP2=${1:$LEN2:$LEN}
    ALLARCHES="$ALLARCHES ${TMP}_debug$TMP2"
  fi
}

# super menu-name list
pick_arches() {
  local SUBARCHES="$3"
  local SUBARCHESDO=""
  local N TMP TMP2 X i MSG
  true
  if [ "$TOP" == "" ]; then
    TOP="$1"
  fi
  while (( ! $? )); do
    if [ "$1" == "" ]; then
      MSG="Which board would you like to build tsctl for?"
    else
      MSG="Which option would you like to add to the build for $1?"
    fi
    ask_choice "$MSG" $SUBARCHES
    N=$?
    TMP=`include_list "$SUBARCHES" $N`
    eval $2 $TMP
    X=$?
    #echo "$2 $TMP --- $X ---"
    if eval $2 $TMP; then
      TMP2=`eval "$2_subtest"`
      TMP3=`eval "$2_subarches"`
      if [ "$1" == "" ]; then
        pick_arches "$TMP" $TMP2 "$TMP3"
      else
        pick_arches "$1-$TMP" $TMP2 "$TMP3"
      fi
    fi
    SUBARCHESDO="$SUBARCHESDO $TMP"

    if [ "$TMP" == "none" ]; then
      add_arch $1
    elif (( $X == 1 )); then
      if [ "$1" == "" ]; then
        add_arch $TMP
      else
        add_arch $1-$TMP
      fi
    fi

   SUBARCHES=`exclude_list "$SUBARCHES" $N`
    if [ "$SUBARCHES" != "" ]; then
      if [ "$1" == "" ]; then
        MSG="Would you like to build tsctl for additional boards?"
      else
        MSG="Which like to add another option to the build for $1?"
      fi
      ask_binary "$MSG" "N"
    else
      false
    fi
  done
  TOP=""
}

ALLARCHES=""
ARCHES="linux86 ts4200 ts4500 ts4700 ts4800 ts7552 ts7553"
ARCHDO=""

pick_arches "" "is_tssocket" "$ARCHES"
for I in $ALLARCHES; do
  echo "Creating directories for $I"
  mkdir _$I product_$I
done
echo
echo "Now configuring cross-compiler prefixes."
echo "The prefix for a given platform is the entire path to gcc for that"
echo "platform, plus any common command prefix for the tools in that"
echo "directory, if applicable (for example 'arm-linux-')"
echo "If you are compiling directly on the board, leave the prefix blank."
echo "If you don't know the path, leave it blank now and then edit"
echo "Makefile.user before compiling to fill in the actual value."
for I in $TOPS; do
  read -e -p "Enter cross-compiler prefix for $I:" PREFIX
  echo "prefix_$I=$PREFIX" >> Makefile.user
  echo "prefix_${I}_debug=\$(prefix_$I)" >> Makefile.user
done

if [ -e Makefile.real ]; then
  mv Makefile.real Makefile
fi

echo
echo "Configuration complete.  You can now edit Makefile.user as needed,"
echo "and run 'make' to begin the build process."
