#!/bin/bash

PREFIX="/usr/local"
DEST="chart_director"
RUNDIR=`pwd`

####
#    A little setup work
####
if uname -a | grep x86_64 >/dev/null; then
    X86_64=1
else
    X86_64=0
fi

if [ $X86_64 = 1 ]; then
    SRC="$RUNDIR/src/chartdir_perl_linux_64.tar.gz"
else
    SRC="$RUNDIR/src/chartdir_perl_linux.tar.gz"
fi
    
if [ -d "$PREFIX/$DEST" ]; then
    echo "$PREFIX/$DEST exists, not going to overwrite it" >/dev/stderr
    exit 1;
fi

if [ ! -w "$PREFIX" ]; then
    echo "Can not write to $PREFIX" >/dev/stderr
    exit 2;
fi

####
#    Install the code
####
if cd $PREFIX && tar xfz $SRC && mv ChartDirector $DEST; then
    echo "Installed libraries"
else 
    exit 3;
fi


####
#     INstall the license
####

HOST=`hostname`

if [ $HOST = 'rpsweb01.royaltyshare.com' ]; then
    LICENSE="$RUNDIR/license/ChartDirector-5.0_rpsweb01"
fi

if [ $HOST = 'rpsweb01.royaltyshare.com' ]; then
    LICENSE="$RUNDIR/license/ChartDirector-5.0_rpsweb02"
fi

if [ -z "$LICENSE" ]; then
    LICENSE="$RUNDIR/license/ChartDirector-5.0_rpsweb01"
fi

cp $LICENSE "$PREFIX/$DEST/lib/chartdir.lic"
