#!/bin/bash

# 1 = program name, 2 = version number, 3 (optional) = extraction point
tkis_extract(){
  if [[ $1 == "" ]] ; then
    error "tkis_extract(): program name not supplied"
  fi

  if [[ $2 == "" ]] ; then
    error "tkis_extract(): version number for $color_reset$color_notice$1$color_reset$color_error not supplied"
  fi

  if [[ $3 == "" ]] ; then
    local dest=$WO
  else
    local dest=$3
  fi

  # let the users know what is going on, extraction can be slow at times
  notice "Now Extracting $color_reset$color_notice$1-$2$color_highlight to $color_reset$color_notice$dest"

  local archive=
  local format=
  local i=

  debug "Looping through \"$archive_formats\" to find and extract: $1-$2"
  for i in $archive_formats tkis_fail_loop; do
    if [[ $i == "tkis_fail_loop" ]] ; then
      error "tkis_extract(): Failed to find $color_reset$color_notice$1-$2$color_reset$color_error at $color_reset$color_notice$AR$color_reset$color_error in any of these formats: $color_reset$color_notice$archive_formats"
    fi

    debug "Now searching for $1-$2.$i"
    if [[ -d $dest/$1-$2 ]] ; then
      warning "$color_reset$color_notice$dest/$1-$2$color_reset$color_warning already exists, not extracting to $color_reset$color_notice$dest"
      break
    else
      archive=$(find $AR -name $1-$2.$i)
      format=$i
    fi

    if [[ $archive != "" ]] ; then
      archive=$(echo $archive | sed -e "/\n/d")
      debug "Found \"$archive\", now attempting to extract"

      if [[ $(echo $archive | grep -s " ") != "" ]] ; then
        warning "You see to have multiple copies, remove the additional one to continue: $color_reset$color_notice$archive$color_reset${color_warning}, using the first occurance"
        archive=$(echo $archive | awk '{ print $1 }')
      fi 

      tar -xf $archive -C $dest

      if ! [ $? -eq 0 ] ; then
        if [[ $format == "tzo" || $format == "tar.lzo" ]] ; then
          warning "Failed to extract: tar -xf $color_reset$color_notice$archive$color_reset$color_warning -C $color_reset$color_notice$dest$color_reset$color_warning, lzop format detected, trying: lzop -d -c $archive | tar -x -C $dest"
          lzop -d -c $archive | tar -x -C $dest

          if ! [ $? -eq 0 ] ; then
            error "tkis_extract(): Failed to extract: lzop -d $color_reset$color_notice$archive$color_reset$color_error | tar -xf -C $color_reset$color_notice$dest"
          fi
        elif [[ $format == "txz" || $format == "tar.xz" ]] ; then
          warning "Failed to extract: tar -xf $color_reset$color_notice$archive$color_reset$color_warning -C $color_reset$color_notice$dest$color_reset$color_warning, xz format detected, trying: xz -d -c $archive | tar -x -C $dest"
          xz -d -c $archive | tar -x -C $dest

          if ! [ $? -eq 0 ] ; then
            error "tkis_extract(): Failed to extract: xz -d $color_reset$color_notice$archive$color_reset$color_error | tar -xf -C $color_reset$color_notice$dest"
          fi
        elif [[ $format == "tlz" || $format == "tar.lz" ]] ; then
          warning "Failed to extract: tar -xf $color_reset$color_notice$archive$color_reset$color_warning -C $color_reset$color_notice$dest$color_reset$color_warning, lzip format detected, trying: lzip -d -c $archive | tar -x -C $dest"
          lzip -d -c $archive | tar -x -C $dest

          if ! [ $? -eq 0 ] ; then
            error "tkis_extract(): Failed to extract: lzip -d $color_reset$color_notice$archive$color_reset$color_error | tar -xf -C $color_reset$color_notice$dest"
          fi
        else
          error "tkis_extract(): Failed to extract: tar -xf $color_reset$color_notice$archive$color_reset$color_error -C $color_reset$color_notice$dest"
        fi
      fi

      break
    fi
  done
}

# will store the version number inside of VERSION if one is found
# 1 = program name
tkis_get_version(){
  if [[ $1 == "" ]] ; then
    error "tkis_get_version(): program name not supplied"
  fi

  local version=
  local number_found=

  if ! [ -f ${SE}versions-$list_filename ] ; then
    debug "${SE}versions-$list_filename does not exist or cannot be accessed"
  else
    debug "running 'fss_extended_read -c 0 -n $1 ${SE}versions-$list_filename' to obtain a single version number for $1"
    version=$(fss_extended_read -c 0 -n $1 ${SE}versions-$list_filename)
  fi

  if [[ $version == "" ]] ; then
    debug "No version found in ${SE}versions-$list_filename, now trying ${SE}global_versions"
    if ! [ -f ${SE}global_versions ] ; then
      error "tkis_get_version(): $color_reset$color_notice${SE}global_versions$color_reset$color_error does not exist or cannot be accessed"
    else
      debug "running 'fss_extended_read -c 0 -n $1 ${SE}global_versions' to obtain a single version number for \"$1\""
      version=$(fss_extended_read -c 0 -n $1 ${SE}global_versions)
    fi
  fi

  if [[ $version == "" ]] ; then
    error "tkis_get_version(): Failed to find a version number for $color_reset$color_notice$1$color_reset$color_error in either $color_reset$color_notice${SE}versions-$list_filename$color_reset$color_error or $color_reset$color_notice${SE}global_versions"
  fi

  # remove trailing and leading whitespace from version number
  version=$(echo $version | sed -e 's|^[[:space:]]*||' -e 's|[[:space:]]*$||')

  tkis_process_version
}

tkis_process_version(){
  VERSION=$version
  SUBVERSION=$(echo $version | grep -o "^[[:alnum:]_-]*\.[[:alnum:]_-]*\.[[:alnum:]_-]*")
  MICVERSION=$(echo $version | grep -o "^[[:alnum:]_-]*\.[[:alnum:]_-]*")
  MAJVERSION=$(echo $version | grep -o "^[[:alnum:]_-]*")

  if [[ $SUBVERSION == "" ]] ; then
    if [[ $MICVERSION == "" ]] ; then
      SUBVERSION=$MAJVERSION
    else
      SUBVERSION=$MICVERSION
    fi
  fi
}

# will store the version number inside of PATCHES for all patches found
# 1 = program name, 2 = version number 3 = subversion number
tkis_get_patches(){
  if [[ $1 == "" ]] ; then
    error "tkis_get_patches(): program name not supplied"
  fi

  local patches=
  local number_found=

  if ! [ -f ${SE}patches-$list_filename ] ; then
    debug "${SE}patches-$list_filename does not exist or cannot be accessed"
  else
    if [[ $2 != "" ]] ; then
      debug "running 'grep -s -c \"^$2-$1 \" ${SE}patches-$list_filename' to see if there are multiple copies of \"$2-$1\""
      number_found=$(grep -s -c "^$2-$1 " ${SE}patches-$list_filename)

      if [ $number_found -gt 1 ] ; then
        warning "Found multiple names for $color_reset$color_notice$2-$1$color_reset$color_warning inside of $color_reset$color_notice${SE}patches-$list_filename$color_reset$color_warning, will use the first occurrence"
      fi

      debug "running 'fss_basic_read -c 0 -n $2-$1 ${SE}patches-$list_filename | sed -e \"s|\\n||g\" -e \"s|\\r||g\"' to obtain a single version number for \"$2-$1\""
      patches=$(fss_basic_read -c 0 -n $2-$1 ${SE}patches-$list_filename | sed -e "s|\n||g" -e "s|\r||g")

      if [[ $3 != "" && $patches == "" ]] ; then
        debug "running 'grep -s -c \"^$3-$1 \" ${SE}patches-$list_filename' to see if there are multiple copies of \"$3-$1\""
        number_found=$(grep -s -c "^$3-$1 " ${SE}patches-$list_filename)

        if [ $number_found -gt 1 ] ; then
          warning "Found multiple names for $color_reset$color_notice$3-$1$color_reset$color_warning inside of $color_reset$color_notice${SE}patches-$list_filename$color_reset$color_warning, will use the first occurrence"
        fi

        debug "running 'fss_basic_read -c 0 -n $3-$1 ${SE}patches-$list_filename | sed -e \"s|\\n||g\" -e \"s|\\r||g\"' to obtain a single version number for \"$3-$1\""
        patches=$(fss_basic_read -c 0 -n $3-$1 ${SE}patches-$list_filename | sed -e "s|\n||g" -e "s|\r||g")
      fi
    fi

    if [[ $patches == "" ]] ; then
      debug "running 'grep -s -c \"^$1 \" ${SE}patches-$list_filename' to see if there are multiple copies of \"$1\""
      number_found=$(grep -s -c "^$1 " ${SE}patches-$list_filename)

      if [ $number_found -gt 1 ] ; then
        warning "Found multiple names for $color_reset$color_notice$1$color_reset$color_warning inside of $color_reset$color_notice${SE}patches-$list_filename$color_reset$color_warning, will use the first occurrence"
      fi

      debug "running 'grep -s -m 1 \"^$1 \" ${SE}patches-$list_filename | sed -e \"s|^$1 ||\" -e \"s|\n||g\" -e \"s|\r||g\"' to obtain a single version number for \"$1\""
      patches=$(grep -s -m 1 "^$1 " ${SE}patches-$list_filename | sed -e "s|^$1 ||" -e "s|\\n||g" -e "s|\\r||g")
    fi
  fi

  if [[ $patches == "" ]] ; then
    debug "No patches found in ${SE}patches-$list_filename, now trying ${SE}global_patches"
    if ! [ -f ${SE}global_patches ] ; then
      error "tkis_get_patches(): $color_reset$color_notice${SE}global_patches$color_reset$color_error does not exist or cannot be accessed"
    else
      if [[ $2 != "" ]] ; then
        debug "running 'grep -s -c \"^$2-$1 \" ${SE}global_patches' to see if there are multiple copies of \"$2-$1\""
        number_found=$(grep -s -c "^$2-$1 " ${SE}global_patches)

        if [ $number_found -gt 1 ] ; then
          warning "Found multiple names for $color_reset$color_notice$2-$1$color_reset$color_warning inside of $color_reset$color_notice${SE}global_patches$color_reset$color_warning, will use the first occurrence"
        fi

        debug "running 'fss_basic_read -c 0 -n $2-$1 ${SE}global_patches | sed -e \"s|\\n||g\" -e \"s|\\r||g\"' to obtain a single version number for \"$2-$1\""
        patches=$(fss_basic_read -c 0 -n $2-$1 ${SE}global_patches | sed -e "s|\\n||g" -e "s|\\r||g")

        if [[ $3 != "" && $patches == "" ]] ; then
          debug "running 'grep -s -c \"^$3-$1 \" ${SE}global_patches' to see if there are multiple copies of \"$3-$1\""
          number_found=$(grep -s -c "^$3-$1 " ${SE}global_patches)

          if [ $number_found -gt 1 ] ; then
            warning "Found multiple names for $color_reset$color_notice$3-$1$color_reset$color_warning inside of $color_reset$color_notice${SE}global_patches$color_reset$color_warning, will use the first occurrence"
          fi

          debug "running 'fss_basic_read -c 0 -n $2-$1 ${SE}global_patches | sed -e \"s|\\n||g\" -e \"s|\\r||g\"' to obtain a single version number for \"$3-$1\""
          patches=$(fss_basic_read -c 0 -n $3-$1 ${SE}global_patches | sed -e "s|\\n||g" -e "s|\\r||g")
        fi
      fi

      if [[ $patches == "" ]] ; then
        debug "running 'grep -s -c \"^$1 \" ${SE}global_patches' to see if there are multiple copies of \"$1\""
        number_found=$(grep -s -c "^$1 " ${SE}global_patches)

        if [ $number_found -gt 1 ] ; then
          warning "Found multiple names for $color_reset$color_notice$1$color_reset$color_warning inside of $color_reset$color_notice${SE}global_patches$color_reset$color_warning, will use the first occurrence"
        fi

        debug "running 'fss_basic_read -c 0 -n $1 ${SE}global_patches | sed -e \"s|\\n||g\" -e \"s|\\r||g\"' to obtain a single version number for \"$1\""
        patches=$(fss_basic_read -c 0 -n $1 ${SE}global_patches | sed -e "s|\n||g" -e "s|\r||g")
      fi
    fi
  fi

  if [[ $patches == "" ]] ; then
    debug "Did not find any patches for \"$1\" in either ${SE}patches-$list_filename or ${SE}global_patches"
  fi

  # remove trailing and leading whitespace from patches
  patches=$(echo $patches | sed -e 's|^[[:space:]]*||' -e 's|[[:space:]]*$||')

  PATCHES=$patches
}

# this is a get_version that rules are allowed to directly call
tkis_local_get_version(){
  local old_version=$VERSION
  local old_subversion=$SUBVERSION
  LOCAL_VERSION=
  LOCAL_SUBVERSION=

  debug "tkis_local_get_version: calling the function: tkis_get_version $1"
  tkis_get_version $1

  LOCAL_VERSION=$VERSION
  LOCAL_SUBVERSION=$SUBVERSION
  VERSION=$old_version
  SUBVERSION=$old_subversion
}

# If a script requires the use of certain non-standard parameters that may or may not be defined, make sure they get defined
tkis_local_require(){
  for i in $* ; do
    if [[ $(eval "echo \$$i") == "" ]] ; then
      echo -e "${color_warning}The variable $color_reset$color_notice$i$color_reset$color_warning is not defined, define it now:$color_reset"
      echo -e -n "${color_notice}$i =${color_reset} "
      read $i
      eval "export $i"
    fi
  done
}
