#!/bin/bash
# 
# Copyright (c) 2019 Skycloud Software Corporation
#
# This script installs SkyForm AIP Client from the tar file.
# It sets up the environment
#
# By default, the path of the installed directory is /opt/skyformaip-client
#

usage() {
        echo "Usage: $0 [--dir=installed_dir] (default: /opt/skyformaip-client)"
        echo "          [--master=host/host_ip] (default: none, prompt)"
        echo "          [--master2=host2/host2_ip] (default: none)"
        echo "          [--noint]"
        exit 1
}

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
    if [ $stat != 0 ]; then
        echo Invalid ip address $1
        exit 1
    fi
}

i_top=/opt/skyformaip-client

bindirs="bin lib64 share"
non_bindirs="etc"
existing=0
noint=0
IFS=/

while [ $# != '0' ]; do
  case $1 in
  *=*) optarg=`expr "X$1" : '[^=]*=\(.*\)'` ;;
  *)  optarg=no ;;
  esac

  case $1 in
  --dir=*)
	i_top=$optarg
	;;
  --master=*)
	read -ra HOST <<< "$optarg"
        master=${HOST[0]}
        masterip=${HOST[1]}
        valid_ip $masterip
	;;
  --master2=*)
	read -ra HOST <<< "$optarg"
        master2=${HOST[0]}
        master2ip=${HOST[1]}
        valid_ip $master2ip
	;;
  --noint)
    noint=1
    ;;
  --help | -h)
	usage ;;
  *)
	echo "ERROR: Invalid option"
        usage ;; 
  esac
  shift
done

IFS=''

if [ -z $master ]; then
   read -p "Master host name:" master
   read -p "Master host IP address:" masterip
   read -p "2nd master host name:" master2
   if [ ! -z $master2 ] && [ -z $master2ip ]; then
      read -p "2nd master host IP address:" master2ip
   fi
fi

distro=`ls -rC [1-9]*.tar.gz`

if [ "X$distro" == "X" ]; then
    echo "No AIP distribution files are found."
    exit 1
fi

myd=$(echo $distro | awk '{print $1}')

echo Found package: $myd
if [ $noint == '0' ]; then
    read -p "Press enter to install..."
fi

if [ ! -d $i_top ]; then
   mkdir $i_top
fi

if [ -f $i_top/etc/cb.yaml ]; then
   existing=1
fi

package=$myd
tar xfz $package -C $i_top

packagedir=$(basename $package .tar.gz)

if [ $existing != 1 ]; then
   mv $i_top/$packagedir/etc $i_top
   for file in $i_top/etc/aip*; do
      sed -i "s|/cubetop|$i_top|g" $file
   done
   ln -s $i_top/$packagedir/bin $i_top
   mkdir $i_top/sbin
   ln -s $i_top/$packagedir/sbin/cbauth $i_top/sbin
   ln -s $i_top/$packagedir/sbin/cbtt $i_top/sbin
   ln -s $i_top/$packagedir/share $i_top
   ln -s $i_top/$packagedir/work $i_top
else
   ln -sf $i_top/$packagedir/bin $i_top
   ln -sf $i_top/$packagedir/sbin/cbauth $i_top/sbin
   ln -sf $i_top/$packagedir/sbin/cbtt $i_top/sbin
   ln -sf $i_top/$packagedir/share $i_top
   ln -sf $i_top/$packagedir/work $i_top
fi

if [ ! -z $master ]; then
   n=37
   line="- name: $master"
   sed -i "${n}i\ \ \ \ $line" $i_top/etc/cb.yaml
   if [ ! -z $master2 ]; then
      echo CB_SERVER_HOSTS=\"$master $master2\" > $i_top/etc/cb.conf
      n=$((n+1))
      line="- name: $master2"
      sed -i "${n}i\ \ \ \ $line" $i_top/etc/cb.yaml
   else
      echo CB_SERVER_HOSTS=\"$master\" > $i_top/etc/cb.conf
   fi
   n=$((n+1))
   line="- name: `hostname`"
   sed -i "${n}i\ \ \ \ $line" $i_top/etc/cb.yaml
   n=$((n+1))
   line="  attr: client"
   sed -i "${n}i\ \ \ \ $line" $i_top/etc/cb.yaml
fi

if [ ! -z $master ] && [ ! -z $masterip ]; then
   echo $masterip $master > $i_top/etc/hosts
   if [ ! -z $master2 ] && [ ! -z $master2ip ]; then
      echo $master2ip $master2 >> $i_top/etc/hosts
   fi
fi

cp $i_top/etc/aip.sh $i_top/etc/aip.csh /etc/profile.d

echo SkyForm AIP Client is installed/updated.
