#!/bin/bash

get_giis_from_section() { # args: file_name, section_name; output: array of URLs
  # sed script to find all the sections in file_name with the given section_name and parse out the URLs
  if [[ -f $1 ]]; then
    sed -n '{
      # if a section start is found, jump to ":section" to process it
      /^\s*\[/ b section
      # if this line is not a section start, append it to the hold buffer
      H
      # if this is the last line, jump to ":section" to process it
      $ b section
      # otherwise skip the ":section" part
      b
      # process a section
      :section
        # put the lines from the hold buffer (containing the whole
        # previous section) the patter space and also put the line
        # from the pattern space (containing the header of the next
        # section) to the hold buffer
        x
        # if it starts with the section name we want, print it
        /^\s*\[\s*'$2'\s*\]/ p
        # otherwise dont do anything: skip this section
    }' $1 | sed -n 's/^\s*giis\s*=\s*"\(.*\)"\s*$/\1/p' # get the URLs from the lines starting with giis=
  fi
}

get_giis_from_file() { # args: file_name; output: array of URLs
  if [[ -f $1 ]]; then
    cat $1
  fi
}

echo ''
echo '=== ARC client configuration conversion ==='
echo ''

# 1. giis options in client section in $HOME/.arc/client.conf
giislist=`get_giis_from_section $HOME/.arc/client.conf client`
if [[ ! $giislist ]]; then
  # 2. giis options in common section in $HOME/.arc/client.conf
  giislist=`get_giis_from_section $HOME/.arc/client.conf common`
  if [[ ! $giislist ]]; then
    # 3. $HOME/.nggiislist (one GIIS per line, ARC <= 0.5.48)
    giislist=`get_giis_from_file $HOME/.nggiislist`
    if [[ ! $giislist ]]; then
      # 4. giis options in client section in $ARC_LOCATION/etc/arc.conf
      giislist=`get_giis_from_section $ARC_LOCATION/etc/arc.conf client`
      if [[ ! $giislist ]]; then
        # 5. giis options in common section in $ARC_LOCATION/etc/arc.conf
        giislist=`get_giis_from_section $ARC_LOCATION/etc/arc.conf common`
        if [[ ! $giislist ]]; then
          # 6. $ARC_LOCATION/etc/giislist (one GIIS per line)
          giislist=`get_giis_from_file $ARC_LOCATION/etc/giislist`
          if [[ ! $giislist ]]; then
            # 7. giis options in client section in /etc/arc.conf
            giislist=`get_giis_from_section /etc/arc.conf client`
            if [[ ! $giislist ]]; then
              # 8. giis options in common section in /etc/arc.conf
              giislist=`get_giis_from_section /etc/arc.conf common`
              if [[ ! $giislist ]]; then
                # 9. /etc/giislist (one GIIS per line)
                giislist=`get_giis_from_file /etc/giislist`
              fi
            fi
          fi
        fi
      fi
    fi
  fi
fi

conffile=$HOME/.arc/client.conf

echo '--- GIIS URLs ---'
echo ''

if [[ $giislist ]]; then

  if [[ -f $conffile ]]; then
    conffile_exists=true
  fi

  if [[ ! $conffile_exists ]]; then
    echo "Please create the file $HOME/.arc/client.conf. Edit the following as needed and put it into that file!"
  else
    echo "Please edit the following if needed and put it into your $HOME/.arc/client.conf!"
  fi
  echo ''

  for giis in ${giislist[@]}; do
    # use hostname as alias
    hostname=`echo $giis | sed 's#^ldap://\(.*\):.*#\1#'`
    # check if index or local ARIS
    islocal=`echo $giis | grep -i "mds-vo-name=local"`
    
    if [[ $islocal ]]; then
      echo "[computing/$hostname]"
      echo "url=$hostname"
      echo "infointerface=org.nordugrid.ldapng"
      echo "jobinterface=org.nordugrid.gridftpjob"
    else
      echo "[registry/$hostname]"
      echo "url=$giis"
      echo "registryinterface=org.nordugrid.ldapegiis"
    fi
    # In ARC 0.x all giises were used by default
    echo "default=yes"
    echo ''
  done

else
  
  echo 'No GIIS URLs found in the old configs.'

fi

echo ''
echo '--- ALIASes ---'
echo ''

tempfile=`mktemp /tmp/ngclient2arc.XXXXXXXXXX`

if [[ $? = 0 ]]; then
  if [[ -f $HOME/.arc/client.conf ]]; then
    sed -n 's/^\s*alias\s*=\s*\"\s*\(.*\)\s*\"\s*$/\1/p' $HOME/.arc/client.conf > $tempfile
  fi
  if [[ -f $HOME/.ngalias ]]; then
    cat $HOME/.ngalias >> $tempfile
  fi

  if [[ -s $tempfile ]]; then

    if [[ -f $conffile ]]; then
      conffile_exists=true
    else
      conffile_exists=
    fi

    if [[ ! $conffile_exists ]]; then
      echo "Please create the file $HOME/.arc/client.conf. Edit the following as needed and put it into that file!"
    else
      echo "Please edit the following if needed and put it into your $HOME/.arc/client.conf!"
    fi

    echo ''

    # channel the file into stdin
    exec 9<&0
    exec < $tempfile

    # for each alias line
    while read line; do
      # get the alias name (before the '=')
      newalias=`echo $line | sed -n 's/^\s*\([^=]*\)\s*=.*$/\1/p'`
      # get the URLs (after the '=')
      urls=`echo $line | sed -n 's/^[^=]*=\(.*\)$/\1/p'`
      # for each URL
      for url in $urls; do
        # if the URL can be found among the aliases (before an '=')
        if [[ `sed -n '/^\s*'$url'\s*=.*$/ p' $tempfile` ]]; then
          # then this is an alias and not a URL
          # TODO: automatically add group= to each section
          echo "To use $newalias as a group alias put group=$newalias in the [computing/$url] block"
        else
          # else this is a real URL
          echo "[computing/$newalias]"
          echo "url=$url"
          echo "infointerface=org.nordugrid.ldapng"
          echo "jobinterface=org.nordugrid.gridftpjob"
        fi
      done
      echo ''
    done

    # restore stdin
    exec 0<&9 9<&-

  else
    
    echo 'No aliases found in the old config files.'

  fi

  rm $tempfile
  
  echo ''

else
  
  echo '  Error creating tempfile: alias conversion failed.'
  echo ''

fi

echo '--- Active jobs ---'
echo ''
echo 'In order to use arc* commands to manipulate the jobs you submitted with ngsub,'
echo 'you have to run the arcsync command with specifying clusters and/or GIISes:'
echo '  arcsync [-c cluster1 -c cluster2 ...] [-g giis1 -g giis2 ...]'
echo ''