# vyatta bash operational mode image name completions
# **** License ****
# 
# Copyright (c) 2019, AT&T Intellectual Property. All rights reserved.
#
# Copyright (c) 2014 by Brocade Communications Systems, Inc.
# All rights reserved.
# 
# This code was originally developed by Vyatta, Inc.
# Portions created by Vyatta are Copyright (C) 2011-2013 Vyatta, Inc.
# All Rights Reserved.
#
# SPDX-License-Identifier: LGPL-2.1-only
# 
# Author: John Southworth
# Date: 2011
# Description: setup bash completion for image file names
# 
# **** End License ****
_vyatta_image_is_file()
{
  local cur=$1
  if ! [[ ${cur:0:1} =~ "/" ]]; then
    cur=${cur/:/}
    topdir=${cur%%/*}
    cur=${cur#$topdir/}
  fi
  if [[ $topdir == "running" ]]; then
    cur="/${cur}"
  elif [[ $topdir == "disk-install" ]]; then
    cur="/live/image/${cur}"
  elif [[ ${cur:0:1} =~ "/" ]]; then
    cur=${cur}
  else
    cur="/live/image/boot/${topdir}/live-rw/${cur}"
  fi
  if [[ -f ${cur} ]]; then
    return 0;
  else
    return 1;
  fi
}

_vyatta_image_file_complete()
{
  if _vyatta_image_is_file ${COMP_WORDS[(( ${#COMP_WORDS[@]}-2 ))]}; then
    _vyatta_op_completions=( "" " " )
    return 0;
  fi
  _vyatta_image_path_complete
}

declare -a non_comps=( "http://<user>:<passwd>@<host>/<file>" \
 "scp://<user>:<passwd>@<host>/<file>" \
 "ftp://<user>:<passwd>@<host>/<file>" \
 "tftp://<host>/<file>" )

_vyatta_image_path_complete()
{
  compopt -o nospace
  local -a reply
  if _vyatta_image_is_file $cur ; then
    foundfile=1
    _vyatta_op_completions=( "${cur} " )
    _vyatta_op_noncompletions=( )
    return 0;
  fi
  if [[ ${cur:0:1} =~ "/" ]]; then
    reply=( $(compgen -f ${cur}) )
    for ((i=0; i < ${#reply[@]}; i++)); do
      [[ -d ${reply[i]} ]] && reply[i]="${reply[i]}"/
    done
    _vyatta_op_completions=( "${reply[@]}" )
    return
  fi
  if [[ ${cur} == "" ]]; then
    reply=( $(compgen -d /live/image/boot/ | grep -v grub) )
    for i in `seq 0 $[${#reply[@]}-1]`; do
      file=${reply[$i]}
      reply[$i]=${file/#\/live\/image\/boot\//}
      reply[$i]="${reply[$i]}://config/"
    done
    reply+=( "running://config/" )
    if [[ -d /live/image/opt/vyatta/etc/config || -d /live/image/config ]]; then
      reply+=( "disk-install://" )
    fi
    _vyatta_op_noncompletions=( "${non_comps[@]}" )
  else 
     _vyatta_op_noncompletions=( )
    if ! [[ $cur =~ .*:\/\/ ]]; then
      if [[ $cur =~ .*:\/ ]]; then
        cur=${cur/:\//}
      fi
      if [[ $cur =~ .*: ]]; then
        cur=${cur/:/}
      fi
      local isrunningimg=$(compgen -W "running" -- ${cur})
      local isdiskinstall=$(compgen -W "disk-install" -- ${cur})
      if [[ $isrunningimg == "running" ]];then
        cur="/"
      elif [[ $isdiskinstall == "disk-install" ]]; then
        cur="/live/image/"
      else
        cur="/live/image/boot/${cur}"
      fi
      reply=( $(compgen -f ${cur} | grep -v grub) )
      for i in `seq 0 $[${#reply[@]}-1]`; do
        file=${reply[$i]}
        if [[ $isrunningimg == "running" ]];then
          reply[$i]="running://config/"
        elif [[ $isdiskinstall == "disk-install" ]]; then
          reply[$i]="disk-install://" 
        else
          reply[$i]=${file/#\/live\/image\/boot\//}
          if [[ -d /live/image/boot/${reply[$i]} ]]; then
            reply[$i]="${reply[$i]/#\//}://config/"
          fi
        fi
      done
    else
      cur=${cur/:/}
      topdir=${cur%%/*}
      cur=${cur#$topdir//}
      if [[ $topdir == "running" ]]; then
        cur="/${cur}"
      elif [[ $topdir == "disk-install" ]]; then
        cur="/live/image/${cur}"
      else
        cur="/live/image/boot/${topdir}/live-rw/${cur}"
      fi
      reply=( $(compgen -f ${cur}) )
      # for loop from _filedirs() in /etc/bash_completion
      for ((i=0; i < ${#reply[@]}; i++)); do
        if [[ ${cur:0:1} != "'" ]]; then 
           [[ -d ${reply[i]} ]] && reply[i]="${reply[i]}"/
           if [[ ${cur:0:1} == '"' ]]; then 
             reply[i]=${reply[i]//\\/\\\\}
             reply[i]=${reply[i]//\"/\\\"}
             reply[i]=${reply[i]//\$/\\\$}
           else
             reply[i]=$(printf %q ${reply[i]})
           fi
        fi
      done
      for i in `seq 0 $[${#reply[@]}-1]`; do
        file=${reply[$i]}
        if [[ $topdir == "running" ]]; then
          reply[$i]=${file/#\//"$topdir://"}
        elif [[ $topdir == "disk-install" ]]; then
          reply[$i]=${file/#\/live\/image\//"$topdir://"} 
        else
          reply[$i]=${file/#\/live\/image\/boot\/$topdir/"$topdir://"}
          reply[$i]=${reply[$i]/\/live-rw\/}
        fi
      done
    fi 
  fi
  _vyatta_op_completions=( "${reply[@]}" )
  return 0
}
