#! /bin/bash 

#
# install BGP LEG into a designated directory.
#
#  the directory will become the home dir of new user 'pcube'.
#  default for directory is /opt/pcube
#
#

# import common functions
if ! . sm-common.sh; then
    echo "Cannot find SM common definitions - exiting"
	echo "run install-bgp-leg from distribution root"
    exit 1
fi

# import platform specific definitions
if is_linux; then
	. linux-def.sh
elif is_solaris; then
	. solaris-def.sh
	# temporary till support of solaris
    echo "Unsupported platform: $platform - exiting"
    exit 1	
else 
    echo "Unsupported platform: $platform - exiting"
	exit 1
fi

echo_log "Starting BGP LEG installation sequence"

# this tag will be replaced by ~pcube when constructing the
# /etc/init.d/p3sm boot script.
PQBHOMETAG="__PQBDIR__"

tarfile=bgp_leg.tar.gz

deploy_leg () {
    echo_log "Adding BGP LEG into pcube home ..."
    cat $tarfile | gzip -dc | ( cd "$PQBHOME" ; tar xpf - )
}
    
chown_pcube_home () {
    echo_log "Changing ownership of SM environment..."
    chown -fhR pcube:pcube "$PQBHOME/sm/server"
}
  

    
make_boot_symlinks_solaris () {
    [ -r "$BGP_LEG_INIT_TMPL" ] ||
        return 1
    
    echo_log "Making symlinks in /etc/init.d and rc?.d  ..."
    rm -f /etc/init.d/p3bgpleg 2>/dev/null
    rm -f /etc/rc[012S].d/[SK]*p3bgpleg 2>/dev/null

    sed "s;$PQBHOMETAG;$PQBHOME;g" <\ >/etc/init.d/p3bgpleg
    chmod 744 /etc/init.d/p3bgpleg
    ln -sf /etc/init.d/p3bgpleg /etc/rc2.d/S92p3bgpleg
    for level in 0 1 S; do
        ln -sf /etc/init.d/p3bgpleg /etc/rc$level.d/K44p3bgpleg
    done
}

make_boot_symlinks_linux () {
    [ -r "$BGP_LEG_INIT_TMPL" ] ||
        return 1
    
    echo_log "Making symlinks in /etc/rc.d/init.d and rc?.d  ..."
    rm -f /etc/rc.d/init.d/p3bgpleg 2>/dev/null
    rm -f /etc/rc.d/rc[012356].d/[SK]*p3bgpleg 2>/dev/null

    sed "s;$PQBHOMETAG;$PQBHOME;g" <$BGP_LEG_INIT_TMPL >/etc/rc.d/init.d/p3bgpleg
    chmod 744 /etc/rc.d/init.d/p3bgpleg
    for level in 2 3 5; do
	    ln -sf /etc/rc.d/init.d/p3bgpleg /etc/rc.d/rc$level.d/S92p3bgpleg
    done    	
    for level in 0 1 6; do
        ln -sf /etc/rc.d/init.d/p3bgpleg /etc/rc.d/rc$level.d/K44p3bgpleg
    done
}

copy_images () {
    cp ${BGP_LEG_EXE_SRC} ${BGP_LEG_EXE_DST}
}
##
## main
##

amiroot || 
    err_and_exit "this script must be run by root" 2


# check for existing pcube user home
curhome=$(grep ^pcube: /etc/passwd | awk -F: '{print $6}')

[ "$curhome" ] ||
        err_and_exit "Cannot find current home of user pcube" 4
    
export PQBHOME="$curhome"

# check there is no instance of the bgpleg running
dump=$(ps -aef)
[ $(echo $dump | grep bgpleg | wc -l) -eq 1 ] &&
    err_and_exit "another instance of the BGP LEG is running - stop it before installing a new version" 5

# backup cnofiguration file
cp "$PQBHOME/sm/server/root/config/p3bgpleg.cfg" "$PQBHOME/sm/server/root/config/p3bgpleg.cfg.bak" >& /dev/null

# Begin actual installation
deploy_leg ||
    err_and_exit "problem deploying BGP LEG files in pcube home" 75

# images to copy
BGP_LEG_EXE_DST="$PQBHOME/sm/server/addons/bgpleg/"

if is_linux; then
    BGP_LEG_EXE_SRC="$PQBHOME/sm/server/addons/bgpleg/linux/*"
elif is_solaris; then
    BGP_LEG_EXE_SRC="$PQBHOME/sm/server/addons/bgpleg/solaris/*"
fi

copy_images ||
    err_and_exit "problem copying images" 78

chown_pcube_home ||
    err_and_exit "problem chowning pcube home" 79

# template file for the boot script
BGP_LEG_INIT_TMPL="$PQBHOME/sm/server/addons/bgpleg/p3bgpleg.sh"

[ $platform != "Linux" ] ||  make_boot_symlinks_linux ||
    err_and_exit "problem installing boot symbolic links" 10

[ $platform != "SunOS" ] || make_boot_symlinks_solaris ||
    err_and_exit "problem installing boot symbolic links" 10

echo_log "Finished BGP LEG installation sequence"
