#!/bin/bash

# distributions are specific to their file and can be directly called without searching
tkis_load_distribution_list(){
  debug "Searching For List: $1 inside of ${DI}$tkis_distribution"
  local found_list=$(grep -s "^[[:space:]]*$1:[[:space:]]*$" ${DI}$tkis_distribution)

  if [[ $found_list != "" ]] ; then
    debug "Found List: $1 inside of ${DI}$tkis_distribution"
  else
    error "tkis_load_distribution_list(): The distribution list $color_reset$color_notice$found_list$color_error was not found inside $color_reset$color_notice$DI$tkis_distribution"
  fi

  debug "Loading List: $1 from ${DI}$tkis_distribution"
  LIST_SIZE=$(fss_basic_list_read -c 0 -s -n $1 ${DI}$tkis_distribution)

  if [[ $LIST_SIZE -eq 0 ]] ; then
    error "tkis_load_distribution_list(): The build/list $color_reset$color_notice$1$color_error inside $color_reset$color_notice$DI$tkis_distribution$color_reset${color_error} is empty"
  fi

  unset LIST_NAME LIST_TOTAL LIST_DATA
}

tkis_install_distribution(){
  echo
  echo -e "${color_title}Turtle Kevux Installation Scripts${color_reset}"
  echo -e "  ${color_notice}Installing Distribution:${color_reset} ${color_notice}$tkis_distribution${color_reset}"
  echo

  tkis_load_distribution_list main

  local tkis_process=
  local turtle_system_type=$tkis_distribution
  local tkis_command_original=
  local tkis_command=$tkis_command
  local tkis_remove_install_file="no"
  local PR_original=$PR
  local dist_current=0
  local list_size=$LIST_SIZE
  unset LIST_SIZE

  # under distribution, a tkis command does not have to be specified, therefore make sure one is!
  if [[ $tkis_command == "" ]] ; then
    tkis_command=system
    tkis_command_original=system
  else
    tkis_command_original=$tkis_command
  fi

  # save the name of the current distribution so that scripts later can just pull this name
  echo $tkis_distribution > ${tkis_data}current.distribution

  # resume the distribution
  if [[ -f $tkis_resume_distribution ]] ; then
    dist_current=$(cat $tkis_resume_distribution)

    # attempt to identify a corrupt/invalid resume file
    local weird=$(echo $(cat $tkis_resume_distribution) | sed -e 's|0||g' -e 's|1||g' -e 's|2||g' -e 's|3||g' -e 's|4||g' -e 's|5||g' -e 's|6||g' -e 's|7||g' -e 's|8||g' -e 's|9||g')

    # on invalid/corrupt data start from the top
    if [[ $dist_current -lt 0 || $dist_current -ge $list_size ]] ; then
      warning "$tkis_resume_distribution is $color_reset$color_notice$dist_current$color_reset$color_warning and should not be neither < 0 nor >= $list_size. execution will start from the top instead of resuming"
      dist_current=0
    elif  [[ $weird != "" ]] ; then
      warning "$tkis_resume_distribution is $color_reset$color_notice$dist_current$color_reset$color_warning and is corrupt or otherwise invalid. execution will start from the top instead of resuming"
      dist_current=0
    elif [[ $dist_current == "" ]] ; then
      warning "$tkis_resume_distribution was is empty; execution will start from the top instead of resuming"
      dist_current=0
    fi
  fi

  # execute system instalation for each process listed
  while [[ $dist_current -lt $list_size ]] ; do
    debug "obtaining list line #$dist_current, \"${DI}$tkis_distribution\""
    LIST_DATA=$(fss_basic_list_read -c 0 -l $dist_current -n main ${DI}$tkis_distribution)
    tkis_process=$(echo $LIST_DATA | sed -e 's|#.*$||' | sed -e 's|[[:space:]]*$||g')

    debug "setting tkis_command from '$tkis_command' to '$tkis_command_original'"
    tkis_command=$tkis_command_original

    # for security purposes, remove these variables before executing bash code
    unset LIST_DATA LIST_NAME LIST_SIZE LIST_TOTAL

    debug "Now attempting to process the system: '$tkis_process'"
    if [[ $(echo $tkis_process | sed -e 's|[[:space:]]||g') != "" ]] ; then
      if [[ $tkis_process != "" ]] ; then
        tkis_resume=${tkis_data}resume.$tkis_process.system
        tkis_resume_rule=${tkis_data}resume.$tkis_process.rule
        tkis_local_error=${tkis_output}error.$tkis_process
      else
        tkis_resume=${tkis_data}resume.system
        tkis_resume_rule=${tkis_data}resume.rule
        tkis_local_error=${tkis_output}error
      fi
      PR=$PR_original

      debug "Calling tkis_install_system command: $tkis_process, tkis_resume: $tkis_resume, tkis_resume_rule: $tkis_resume_rule, tkis_local_error: $tkis_local_error"
      tkis_install_system

      if [[ $? != 0 ]] ; then
        error "tkis_install_distribution(): tkis_install_systenm failed for some reason"
      fi
    else
      debug "The line: \"$tkis_process\" was skipped because it contains only whitespace"
    fi

    let dist_current=$dist_current+1
 
    if [[ $dist_current -lt $list_size ]] ; then
      # save the next spot so resume can work properly
      echo $dist_current > $tkis_resume_distribution
    fi
  done

  # cleanup process
  rm -f $tkis_resume_distribution

  notice "Distribution Installation Successful"
}
