#!/bin/bash

#
# File .......... swuk_create_vivado_project
# Author ........ Steve Haywood
# Website ....... http://www.spacewire.co.uk
# Project ....... Common (SpaceWire UK Tutorial)
# Date .......... 25 Jun 2025
# Version ....... 2.0
# History .......
#   1.0 common/fw/src/script/create_vivado_project.sh
# Description ...
#   Very simple script to launch Vivado and run the TCL script. Should be run
# in the project host directory that contains the fw & sw subdirectories, for
# example :-
#
# user@host:~/swuk_tutorial/project$ swuk_create_vivado_project
# user@host:~/swuk_tutorial/project$ swuk_create_vivado_project --build
#

# Strict
#set -euo pipefail


################################################################################
# Main function.
# Arguments ...... ${@}
# Return ......... None
# Exit code ...... Status (0=success, 1=failure)
# Shared (In) .... None
# Shared (Out) ... None
main()
{
  # Declare argument constants
  local -r  c_optbuild="--build"  # Str: Build option name
  local -r  c_opthelp="--help"    # Str: Help option name
  local -Ar c_options=(           # ARR: Options & associated help information
    [${c_optbuild}]="Build the project after creation."
    [${c_opthelp}]="Display this help and exit."
  )
  local -ar c_optorder=(          # Arr: Help options display order
    ${c_optbuild}
    ${c_opthelp}
  )
  local -ar argv=(${@})           # Arr: Get argument values (space-separated) into array

  # Declare argument variables
  local     arg                   # Str: Current argument from argv array
  local     option                # Str: Current option from c_optorder array

  # Display help information
  if [[ " ${argv[*]} " =~ " ${c_opthelp} " ]]; then
    echo "Usage: $(basename ${0}) [OPTION]..."
    echo "Create a new Vivado project using the $(basename ${0}).tcl script & optionally build it."
    echo
    for option in ${c_optorder[@]}
    do
      echo "      ${option} $(printf ' %.0s' {1..12} | head -c $((12-${#option}))) ${c_options[${option}]}"
    done
    echo
    exit 0
  fi

  # Get & check the arguments
  for arg in ${argv[@]}; do
    if [[ ${arg:0:2} == "--" ]]; then  # Option
      [[ ! -v c_options[${arg}] ]] &&
        echo "Option (${arg}) is not recognised!" >&2 &&
        exit 1
    else
      echo "Unexpected argument (${arg}) found!" >&2
      exit 1
    fi
  done

  # Declare operational constants & variables
  local -r c_dir_script=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &> /dev/null && pwd)
                                  # Str: Script location directory
  local -r c_dir_fw="fw"          # Str: Firmware base directory
  local -r c_dir_vivado="vivado"  # Str: Vivado project directory
  local    tclargs=""             # Str: Arguments sent to TCL script

  # Sanity check directory structure
  [ ! -d ${c_dir_fw} ] &&
    echo "Firmware base directory ${c_dir_fw} does not exists!" >&2 &&
    exit 1

  [ -d ${c_dir_fw}/${c_dir_vivado} ] &&
    echo "Vivado project directory ${c_dir_fw}/${c_dir_vivado} already exists!" >&2 &&
    exit 1

  # Create & move into the new project directory
  mkdir -p ${c_dir_fw}/${c_dir_vivado}
  cd ${c_dir_fw}/${c_dir_vivado}

  # Check for build argument
  [[ " ${argv[*]} " =~ " ${c_optbuild} " ]] && tclargs="-tclargs build"

  # Launch Vivado
  vivado -nojournal -nolog -notrace -mode gui -source "${c_dir_script}/$(basename ${0}).tcl" ${tclargs} &

  # Move back out of project directory
  cd ../..
}


################################################################################
# Opening gambit.
# Arguments ...... ${@}
# Return ......... None
# Exit code ...... Status (0=success, 1=failure)
# Shared (In) .... None
# Shared (Out) ... None
main ${@}
exit 0
