#!/bin/bash
# copyright Kevin Day 2006, under the FLL license
# part of the Kevux Project
# Extra paramaters will be ignored

# colors
color_reset="\\033[0m"
color_error="\\033[1;31m"
color_warning="\\033[0;33m"
color_title="\\033[1;33m"
color_highlight="\\033[1;32m"
color_notice="\\033[0;01m"
color_important="\\033[0;32m"

# a function to simplify load management
tkis_source_file(){
  if [ ! -f $1 ] ; then
   echo -e "${color_error}ERROR: Missing or Cannot Access $1$color_reset" 1>&2
   exit 1
 fi

  source $1
}

tkis_command_line(){
  # first look for only -l, to see if the color needs to be light
  for i in $2 ; do
    if [[ $i == "-l" || $i == "--light" ]] ; then
      color_error="\\033[1;31m"
      color_warning="\\033[0;31m"
      color_title="\\033[1;34m"
      color_highlight="\\033[1;34m"
      color_notice="\\033[0;01m"
      color_important="\\033[0;34m"
    elif [[ $i == "-n" || $i == "--no_color" ]] ; then
      color_error=
      color_warning=
      color_title=
      color_highlight=
      color_notice=
      color_important=
    fi
  done

  # help loop specific variables
  local loop_skip=
  local prev_option=

  # process parameters
  for i in $2 ; do
    if [[ $i == "-h" || $i == "--help" ]] ; then
      echo
      echo -e "${color_title}Turtle Kevux Installation Scripts$color_reset"
      echo -e "      ${color_notice}Version $1$color_reset"
      echo
      echo -e "${color_highlight}install$color_reset $color_notice<${color_reset}build_type$color_notice>$color_reset $color_notice<${color_reset}build program $color_notice[${color_reset}pass$color_notice] [${color_reset}version$color_notice]>$color_reset $color_notice[${color_reset}options$color_notice]$color_reset"
      echo -e "  ${color_important}build${color_reset}                specific set of packages/instructions to use, not used with: -d/--distribution"
      echo -e "  ${color_important}program${color_reset}              --upgrade/--execute only, use this program name or instruction name"
      echo -e "  ${color_important}pass${color_reset}                 --upgrade/--execute only, use this program name or instruction name"
      echo -e "  ${color_important}version${color_reset}              --upgrade only, force this version number"
      echo -e "  ${color_important}path${color_reset}                 --path only, set this path as the tkis_path"
      echo
      echo -e "${color_highlight}Options:$color_reset"
      echo -e " -${color_important}h${color_reset}, --${color_important}help${color_reset}            print this help screen"
      echo -e " -${color_important}v${color_reset}, --${color_important}version${color_reset}         print the version number"
      echo -e " -${color_important}R${color_reset}, --${color_important}rerun${color_reset}           reset, start rule from the top"
      echo -e " -${color_important}r${color_reset}, --${color_important}resume${color_reset}          reset, start rule from last position"
      echo -e " -${color_important}c${color_reset}, --${color_important}clean${color_reset}           remove all relating files in the work directory"
      echo -e " -${color_important}w${color_reset}, --${color_important}wipe${color_reset}            remove all files in the work directory"
      echo -e " -${color_important}C${color_reset}, --${color_important}complete-clean${color_reset}  remove all relating files in the work and tools directories"
      echo -e " -${color_important}D${color_reset}, --${color_important}debug${color_reset}           remove all files in the work directory"
      echo -e " -${color_important}l${color_reset}, --${color_important}light${color_reset}           provides color options that are readable on white/light background "
      echo -e " -${color_important}u${color_reset}, --${color_important}upgrade${color_reset}         forcefully upgrade/downgrade/reinstall a single specific package"
      echo -e " -${color_important}e${color_reset}, --${color_important}execute${color_reset}         forcefully execute a single specific instruction (not for packages)"
      echo -e " -${color_important}n${color_reset}, --${color_important}no_color${color_reset}        do not output in color"
      echo -e " -${color_important}p${color_reset}, --${color_important}path${color_reset}            path to the tkis scripts, this sets \"tkis_path\""
      echo -e " -${color_important}E${color_reset}, --${color_important}environment${color_reset}     do not process anything, just print the environment data"
      echo -e " -${color_important}f${color_reset}, --${color_important}force${color_reset}           force the re-installation of a system, regardless if already installed"
      echo -e " -${color_important}d${color_reset}, --${color_important}distribution${color_reset}    Perform the installation of a series of systems"
      echo -e " -${color_important}s${color_reset}, --${color_important}start_over${color_reset}      When installing a distribution (-d), do not resume from last position"
      echo
      exit 0
    elif [[ $i == "-v" || $i == "--version" ]] ; then
      echo $1
      exit 0
    elif [[ $i == "-r" ||  $i == "--resume" ]] ; then
      if [[ $tkis_command == "" ]] ; then
        tkis_command="resume"
      elif [[ $tkis_command == "upgrade" ]] ; then
        tkis_command="resume-upgrade"
      elif [[ $tkis_command == "execute" ]] ; then
        tkis_command="resume-execute"
      fi
    elif [[ $i == "-R" ||  $i == "--rerun" ]] ; then
      if [[ $tkis_command == "" ]] ; then
        tkis_command="rerun"
      elif [[ $tkis_command == "upgrade" ]] ; then
        tkis_command="rerun-upgrade"
      elif [[ $tkis_command == "execute" ]] ; then
        tkis_command="rerun-execute"
      fi
    elif [[ $i == "-w" ||  $i == "--wipe" ]] ; then
      if [[ $tkis_command == "" ]] ; then
        tkis_command="wipe"
      fi
    elif [[ $i == "-c" ||  $i == "--clean" ]] ; then
      if [[ $tkis_command == "" ]] ; then
        tkis_command="clean"
      elif [[ $tkis_command == "upgrade" ]] ; then
        tkis_command="clean-upgrade"
      elif [[ $tkis_command == "execute" ]] ; then
        tkis_command="clean-execute"
      fi
    elif [[ $i == "-C" ||  $i == "--complete-clean" ]] ; then
      if [[ $tkis_command == "" ]] ; then
        tkis_command="complete-clean"
      fi
    elif [[ $i == "-E" ||  $i == "--environment" ]] ; then
      if [[ $tkis_command == "" ]] ; then
        tkis_command="environment"
      fi
    elif [[ $i == "-D" || $i == "--debug" ]] ; then
      if [[ $tkis_print_debug == "" ]] ; then
        tkis_print_debug="yes"
      fi
    elif [[ $i == "-f" || $i == "--force" ]] ; then
      tkis_force_install="yes"
    elif [[ $i == "-p" || $i == "--path" ]] ; then
      if [[ $loop_skip == "" ]] ; then
        loop_skip="tkis_path"
        prev_option=$i
      else
        echo -e "${color_error}ERROR: You specified -p or --path, but did not supply the proper parameters for the previous option: $color_reset$color_notice$prev_option$color_reset" 1>&2
        exit 1
      fi
    elif [[ $i == "-u" ||  $i == "--upgrade" ]] ; then
      if [[ $loop_skip == "" ]] ; then
        tkis_command="upgrade"
        loop_skip="tkis_upgrade_1"
        prev_option=$i
      else
        echo -e "${color_error}ERROR: You specified -u or --upgrade, but did not supply the proper parameters for the previous option: $color_reset$color_notice$prev_option$color_reset" 1>&2
        exit 1
      fi
    elif [[ $i == "-e" ||  $i == "--execute" ]] ; then
      if [[ $loop_skip == "" ]] ; then
        tkis_command="execute"
        loop_skip="tkis_execute_1"
        prev_option=$i
      else
        echo -e "${color_error}ERROR: You specified -e or --execute, but did not supply the proper parameters for the previous option: $color_reset$color_notice$prev_option$color_reset" 1>&2
        exit 1
      fi
    elif [[ $i == "-d" ||  $i == "--distribution" ]] ; then
      if [[ $loop_skip == "" ]] ; then
        loop_skip="tkis_distribution_1"
      else
        echo -e "${color_error}ERROR: You specified -d or --distribution, but did not supply the proper parameters for the previous option: $color_reset$color_notice$prev_option$color_reset" 1>&2
        exit 1
      fi
    elif [[ $i == "-s" || $i == "--start_over" ]] ; then
      tkis_start_over="yes"
    elif [[ $i != "-n" && $i != "--no_color" && $i != "-l" && $i != "--light" ]] ; then
      if [[ $loop_skip != "" ]] ; then
        if [[ $loop_skip == "tkis_path" ]] ; then
          if [[ $(echo $i | grep -s '/$') == "" ]] ; then
            tkis_path=$i/
          else
            tkis_path=$i
          fi

          tkis_global_settings=${tkis_path}settings/global_settings
          loop_skip=
        elif [[ $loop_skip == "tkis_upgrade_1" ]] ; then
          tkis_program_name=$i
          loop_skip="tkis_upgrade_2"
        elif [[ $loop_skip == "tkis_upgrade_2" ]] ; then
          tkis_program_version=$i
          loop_skip="tkis_upgrade_3"
        elif [[ $loop_skip == "tkis_upgrade_3" ]] ; then
          tkis_program_rule=$i
          loop_skip=""
        elif [[ $loop_skip == "tkis_execute_1" ]] ; then
          tkis_program_name=$i
          loop_skip="tkis_execute_2"
        elif [[ $loop_skip == "tkis_execute_2" ]] ; then
          tkis_program_rule=$i
          loop_skip=""
        elif [[ $loop_skip == "tkis_distribution_1" ]] ; then
          tkis_distribution=$i
          loop_skip=""
        else
          echo -e "${color_error}ERROR: loop_skip was set to invalid data$color_reset" 1>&2
          exit 1
        fi
      else
        if [[ $tkis_process == "" ]] ; then
          tkis_process=$i
          tkis_resume_rule=${tkis_data}resume.$tkis_process.rule
          tkis_resume=${tkis_data}resume.$tkis_process.system
          tkis_local_error=${tkis_output}error.$tkis_process
        fi
      fi
    fi
  done

  if [[ $loop_skip == "tkis_path" ]] ; then
    echo -e "${color_error}ERROR: You passed \"-p\" or \"--path\" but did not supply a valid path!$color_reset" 1>&2
    exit 1
  elif [[ $loop_skip == "tkis_upgrade_1" ]] ; then
    echo -e "${color_error}ERROR: You passed \"-u\" or \"--upgrade\" but did not supply a valid program name!$color_reset" 1>&2
    exit 1
  elif [[ $loop_skip == "tkis_execute_1" ]] ; then
    echo -e "${color_error}ERROR: You passed \"-e\" or \"--execute\" but did not supply a valid instruction name!$color_reset" 1>&2
    exit 1
  elif [[ $loop_skip == "tkis_distribution_1" ]] ; then
    echo -e "${color_error}ERROR: You passed \"-d\" or \"--distribution\" but did not supply a valid distribution name!$color_reset" 1>&2
    exit 1
  elif [[ $loop_skip == "tkis_execute_2" ]] ; then
    tkis_program_rule="main"
  fi

  if [[ $tkis_command == "" ]] ; then
    tkis_command="system"
  fi
}

tkis_main(){
  local tkis_version="1.0.1"
  local tkis_install_program=$(basename $0)
  local tkis_path=$(echo $0 | sed -e "s|scripts/$tkis_install_program$||")

  if [[ $tkis_path == $0 ]] ; then
    tkis_path=$(echo $PWD | sed -e "s|scripts$||")
  elif [[ $(echo $tkis_path | grep -o "^\./") != "" ]] ; then
    if [[ $PWD == "/" ]] ; then
      tkis_path=$PWD$(echo $tkis_path | sed -e 's|^\./||')
    else
      tkis_path=$PWD$(echo $tkis_path | sed -e 's|^\./|/|')
    fi
  fi

  if [[ $(echo $tkis_path | grep -o "^../") != "" ]] ; then
   echo -e "${color_error}ERROR: Unable to identify absolute path of the TKIS directory, please use an absolute path when calling the installation script.$color_reset" 1>&2
   exit 1
  fi

  export PATH=${tkis_path}scripts/bootstrap/bin:${tkis_path}bin:$PATH

  # important information used by the tkis_command_line_function
  local tkis_command=
  local tkis_program_name=
  local tkis_program_version=
  local tkis_program_rule=
  local tkis_process=
  local tkis_print_debug=
  local tkis_force_install=
  local tkis_distribution=
  local tkis_remove_install_file="yes"
  local tkis_start_over=

  # set the default project path variables
  local AR=${tkis_path}archive/
  local PA=${tkis_path}patches/
  local PR=${tkis_path}premade/
  local RU=${tkis_path}rules/
  local SC=${tkis_path}scripts/
  local SE=${tkis_path}settings/
  local SY=${tkis_path}systems/
  local TO=${tkis_path}tools/
  local WO=${tkis_path}work/
  local PO=${tkis_path}scripts/programs/
  local DI=${tkis_path}distributions/
  local TD=${tkis_path}documentation/
  local tkis_global_settings=${tkis_path}settings/global_settings
  local tkis_running_on_host=
  local list_filename=
  local tkis_data=${tkis_path}data/
  local tkis_output=${tkis_path}output/
  local tkis_resume_distribution=${tkis_data}resume.$tkis_distribution.distribution
  local tkis_resume_rule=${tkis_data}resume.rule
  local tkis_resume=${tkis_data}resume.system
  local tkis_local_error=${tkis_output}error
  local turtle_system_type=

  if [[ ! -e ${tkis_path}scripts/bootstrap/bin/fss_basic_read && ! -e ${TO}bin/fss_basic_read ]] ; then
     echo -e "${color_title}PLEASE WAIT: Bootstrapping TKIS dependencies.$color_reset" 1>&2
    ${tkis_path}scripts/bootstrap/compile.sh
  fi

  # process the command line
  tkis_command_line $tkis_version "$*"

  if [[ $tkis_process != "" ]] ; then
    tkis_resume_rule=${tkis_data}resume.$tkis_process.rule
    tkis_resume=${tkis_data}resume.$tkis_process.system
    tkis_local_error=${tkis_output}error.$tkis_process
  fi

  # load global settings
  tkis_source_file $tkis_global_settings
  tkis_source_file ${PO}output
  tkis_source_file ${PO}security
  tkis_source_file ${PO}obtain
  tkis_source_file ${PO}package
  tkis_source_file ${PO}execute
  tkis_source_file ${PO}system
  tkis_source_file ${PO}upgrade
  tkis_source_file ${PO}single_execution
  tkis_source_file ${PO}distribution
  tkis_source_file ${PO}documentation


  if [[ -f ${tkis_data}current.distribution ]] ; then
    turtle_system_type=$(cat ${tkis_data}current.distribution)
  fi

  # these depend on data defined in the global_settings file
  local TC=${target_toolchain}
  local TA=${target_system}
  local PC=${target_package}

  debug "Confirming existance of global environmental variables"
  tkis_confirm_global_settings

  # make sure these have a trailing slash
  if [[ $(echo $TC | grep -s "/$") == "" ]] ; then
    TC=$TC/
  fi
  if [[ $(echo $TA | grep -s "/$") == "" ]] ; then
    TA=$TA/
  fi
  if [[ $(echo $documentation_directory | grep -s "/$") == "" ]] ; then
    documentation_directory=${documentation_directory}/
  fi

  if [[ $tkis_distribution == "" ]] ; then
    debug "Calling tkis_install_system command"
    tkis_install_system
  else
    # make sure start over happens, if requested
    if [[ $tkis_start_over == "yes" ]] ; then rm -vf $tkis_resume_distribution ; fi

    debug "Calling tkis_install_distribution command"
    tkis_install_distribution
  fi
}

# run the program
tkis_main $*
