#!/bin/bash

# This file is part of Megatest.
# 
#     Megatest is free software: you can redistribute it and/or modify
#     it under the terms of the GNU General Public License as published by
#     the Free Software Foundation, either version 3 of the License, or
#     (at your option) any later version.
# 
#     Megatest is distributed in the hope that it will be useful,
#     but WITHOUT ANY WARRANTY; without even the implied warranty of
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#     GNU General Public License for more details.
# 
#     You should have received a copy of the GNU General Public License
#     along with Megatest.  If not, see <http://www.gnu.org/licenses/>.

set -x

if [[ $DEPLOYTARG == "" ]] ; then
    echo Installing into deploytarg
    export DEPLOYTARG=$PWD/deploytarg
fi

# Make the deploy dir
mkdir -p $DEPLOYTARG

if [[ $proxy == "" ]]; then 
  echo 'Please set the environment variable "proxy" to host.com:port (e.g. foo.com:1234) if you need to use a proxy'
  echo PROX=""
else
  export http_proxy=http://$proxy
  export PROX="-proxy $proxy"
fi

export CHICKENINSTDIR=$(dirname $(dirname $(type -p csi)))

# First copy in the needed iup, sqlite3 and zmq libraries
cp $CHICKENINSTDIR/lib/lib{zmq,uuid}* $DEPLOYTARG
cp $CHICKENINSTDIR/lib/libchicken.* $DEPLOYTARG

# Then run the deploy for all needed # Some eggs are quoted since they are reserved to Bash
for f in matchable readline apropos base64 regex-literals format "regex-case" "test" coops \
         trace csv dot-locking posix-utils posix-extras directory-utils hostinfo tcp rpc \
         csv-xml fmt json md5 iup canvas-draw ; do
  if ! [[ -e $DEPLOYTARG/$f.so ]];then
    chicken-install $PROX -deploy $f -prefix $DEPLOYTARG
    # chicken-install -deploy -prefix $DEPLOYTARG $PROX $f
  else
    echo Skipping install of egg $f as it is already installed
  fi
done

export CSC_OPTIONS="-I$CHICKENINSTDIR/include -L$DEPLOYTARG" 
chicken-install -deploy zmq -prefix $DEPLOYTARG
chicken-install -deploy sqlite3 -prefix $DEPLOYTARG

make $DEPLOYTARG/megatest
make $DEPLOYTARG/dashboard
