#!/bin/bash

#   VERSIONS OF MAJOR DEPENDENCIES:
#     OPENSUSE LEAP 15
#     LLVM: 5.0.1
#     CLANG: 5.0.1
#     GCC: 7.3.1

SCRIPT=`realpath $0`
XREATE_PATH=`dirname $SCRIPT`/..

#Provide ABSOLUTE path
CLINGO_PATH=${XREATE_PATH}/build/vendors/clingo
COCO_PATH=${XREATE_PATH}/build/vendors/coco

CLASP_VERSION=95cc1182f

pause(){
 read -n1 -rsp "$1 "$'Press any key to continue or Ctrl+C to exit...\n'
}

#   PREREQUISITES
#
sudo zypper in \
  git wget unzip make \
  gcc7-c++ scons bison re2c\
  llvm5-devel libboost_headers1_66_0-devel \
  cmake gtest tbb-devel clang5-devel \
  libxml2-devel  libbsd-devel\
  libboost_system1_66_0-devel libboost_filesystem1_66_0-devel
  
  
#   COCO CPP
#   The Compiler Generator Coco/R 
#   http://www.ssw.uni-linz.ac.at/coco/#CPP
#
pause "Preparing Coco.."
mkdir -p $COCO_PATH
cd $COCO_PATH
wget http://www.ssw.uni-linz.ac.at/coco/CPP/CocoSourcesCPP.zip
unzip ./CocoSourcesCPP.zip
make


#   CLINGO
#   A grounder and solver for logic programs.
#   https://github.com/potassco/clingo
#
pause "Preparing Clingo.."
mkdir -p $CLINGO_PATH
cd $CLINGO_PATH
git clone https://github.com/potassco/clingo.git ./ &&\
git reset --hard $CLASP_VERSION
git apply $XREATE_PATH/installation/docker/patches/potassco-patch-95cc11
scons configure --build-dir=debug &&\
  sed -i "s/, '-fvisibility=hidden'//" build/debug.py &&\
  sed -i "s/CXXFLAGS = \[\(.*\)\]/CXXFLAGS = \['-fPIC', \1\]/" build/debug.py &&\
  sed -i "s/WITH_LUA = 'auto'/WITH_LUA = None/" build/debug.py &&\
  sed -i "s/WITH_PYTHON = 'auto'/WITH_PYTHON = None/" build/debug.py &&\
  sed -i "s/'-std=c++11'/'-std=c++14'/" build/debug.py &&\
  cat build/debug.py &&\
  scons --build-dir=debug
    
    
#   XREATE    
#
pause "Preparing Xreate.."
cd $XREATE_PATH/vendors/jeayeson
git pull
./configure
mkdir ../../build
cd ../../build
ln -s $COCO_PATH/Copyright.frame $XREATE_PATH/vendors/coco/generator/Copyright.frame
ln -s $COCO_PATH/Scanner.frame $XREATE_PATH/vendors/coco/generator/Scanner.frame
mkdir $XREATE_PATH/grammar/main $XREATE_PATH/grammar/modules
cmake -DCMAKE_BUILD_TYPE=Debug -DBUILD_XREATE_TESTS=1 -DCOCO_EXECUTABLE=$COCO_PATH/Coco -DCLINGO_PATH=$CLINGO_PATH -DCOCO_FRAMES_PATH=$XREATE_PATH/vendors/coco/generator/ -DCMAKE_CXX_COMPILER=g++ ../cpp
    
