#!/bin/bash
#
# Copyright 2019 Skycloud Software
#
# Install script to set up multiple cluster client environment
#
function valid_ip()
{
    local  ip=$1
    local  stat=1

    if [ -z $1 ]; then
        return
    fi

    if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
        OIFS=$IFS
        IFS='.'
        ip=($ip)
        IFS=$OIFS
        [[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \
            && ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
        stat=$?
    fi
    return $stat
}

n=1
clienttop=`dirname $CB_ENVDIR`
installdir=`pwd`
while true; do
  read -p "Input the name of the cluster $n:" cluster
  if [ -z $cluster ]; then
    break
  fi
  read -p "Cluster $n - input the master hostname:" master
  while true; do
    read -p "Cluster $n - input the master IP address:" masterip
    if [ -z $masterip ]; then
        break
    else
       valid_ip $masterip
       if [ $? == 0 ]; then
           break
       fi
    fi
    echo "Invalid ip: $masterip"
  done
  read -p "Cluster $n - input the 2nd master hostname:" master2
  if [ ! -z $master2 ]; then
     while true; do
        read -p "Cluster $n - input the 2nd master IP address:" master2ip
        if [ -z $master2ip ]; then
            break
        else
            valid_ip $master2ip
            if [ $? == 0 ]; then
                break
            fi
        fi
        echo "Invalid ip: $master2ip"
     done
  fi
  clusterdir=$clienttop/clusters/$cluster
  clusteretc=$clusterdir/etc
  if [ ! -d $clusterdir ]; then
     mkdir -p $clusteretc
     ln -s $clienttop/bin $clusterdir/bin
     ln -s $clienttop/sbin $clusterdir/sbin
     ln -s $clienttop/etc/skyformvnc $clusteretc/skyformvnc
  fi
  if [ ! -z $master ]; then
   if [ ! -z $master2 ]; then
      echo CB_SERVER_HOSTS=\"$master $master2\" > $clusteretc/cb.conf
   else
      echo CB_SERVER_HOSTS=\"$master\" > $clusteretc/cb.conf
   fi
  fi

  if [ ! -z $master ] && [ ! -z $masterip ]; then
   echo $masterip $master > $clusteretc/hosts
   if [ ! -z $master2 ] && [ ! -z $master2ip ]; then
      echo $master2ip $master2 >> $clusteretc/hosts
   fi
  fi
  cp $clienttop/etc/cb.yaml $clusteretc
  n=$((n+1))
  echo ------------------------------------------------------
done
clusterlist=`ls -d $clienttop/clusters/* | grep -v default`
while true; do
  echo Select the default cluster:
  n=0
  for file in $clusterlist; do
    n=$((n+1))
    echo "[$n] `basename $file`"
  done
  read -p "Selection:" un
  if [ $un -ge "1" -a $un -le $n ]; then
    default=`echo $clusterlist | cut -d' ' -f$un`
    default=`basename $default`
    break
  fi
done

cd $clienttop/clusters
ln -sf $default default

#exists="`ls $clienttop/bin/*.orig`"
#if [ ! -z $exists ]; then
#  exit 0
#fi
cd $installdir
cp aipaction aipquery $clienttop/bin
cd $clienttop/bin
querycmds="ccluster chinfo chist chosts cinfo cjdep cjobs cload cmgroup cparams cqueues cread cresources cusers cview"
links="bchkpnt bhosts bjobs bkill bmig bpeek bpost bqueue bread brestart bsub cresume cstop cugroup"
actioncmds="cbot ckill cmig cmod cpost crequeue crestart crun csub cswitch ctop dcvsub vncsub cstop cresume"

# action cmds
for cmd in $actioncmds; do
    link=`readlink $cmd`
    if [ -z $link ]; then
        mv $cmd $cmd.orig
        ln -s aipaction $cmd
    fi
done

# query cmds
for cmd in $querycmds; do
    link=`readlink $cmd`
    if [ -z $link ]; then
        mv $cmd $cmd.orig
        ln -s aipquery $cmd
    fi
done

ln -sf ckill.orig cstop.orig
ln -sf ckill.orig cresume.orig
# ln -sf ckill.orig cchkpnt.orig
