#!/bin/bash

## 
##  IMS Open Corpus Workbench (CWB)
##  Copyright (C) 1993-2006 by IMS, University of Stuttgart
##  Copyright (C) 2007-     by the respective contributers (see file AUTHORS)
## 
##  This program 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 2, or (at your option) any later
##  version.
## 
##  This program 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 (in the file "COPYING", or available via
##  WWW at http://www.gnu.org/copyleft/gpl.html).


## check that Xcode is installed
if [[ !( -x /usr/bin/gcc && -x /usr/bin/flex && -x /usr/bin/bison ) ]]
then
    echo "You must install the Xcode suite (available from Apple) in order to compile CWB from source."
    exit
fi

## now try common ways of providing required external libraries
## (1) HomeBrew package manager
if command -v brew >/dev/null 
then
    HOMEBREW=$(brew --prefix)
    OPT="$HOMEBREW/opt"
    echo "Assuming external libraries to be provided by HomeBrew package manager ($HOMEBREW)"
    if [[ !(-f $OPT/glib/lib/libglib-2.0.dylib || -f $OPT/glib/lib/libglib-2.0.a) ]]
    then
        echo " *** cannot find external GLib2 library (libglib-2.0) ***"
        echo "Please install Glib2 with 'brew install glib', then re-run this script."
        exit
    fi
    if [[ !(-f $OPT/pcre/lib/libpcre.dylib || -f $OPT/pcre/lib/libpcre.a) ]]
    then
        echo " *** cannot find external PCRE library (libpcre) ***"
        echo "Please install PCRE with 'brew install pcre', then re-run this script."
        exit
    fi
    if [[ !(-f $OPT/readline/lib/libreadline.dylib || -f $OPT/readline/lib/libreadline.a) || !(-f $OPT/readline/lib/libhistory.dylib || -f $OPT/readline/lib/libhistory.a) ]]
    then
        echo " *** cannot find external GNU Readline library (libreadline + libhistory) ***"
        echo "Please install GNU Readline with 'brew install readline', then re-run this script."
        exit
    fi
    ARCH=$(uname -m)
    if [[ $ARCH == "arm64" ]]
    then
        PLATFORM=darwin-brew-m1
    else
        PLATFORM=darwin-brew
    fi

## (2) MacPorts package manager
elif [[ -x /opt/local/bin/port ]]
then
    echo "Assuming external libraries to be provided by MacPorts (/opt/local)"
    if [[ !(-f /opt/local/lib/libglib-2.0.dylib || -f /opt/local/lib/libglib-2.0.a) ]]
    then
        echo " *** cannot find external GLib2 library (libglib-2.0) ***"
        echo "Please install Glib2 with 'port install glib2 +universal', then re-run this script."
        exit
    fi
    if [[ !(-f /opt/local/lib/libpcre.dylib || -f /opt/local/lib/libpcre.a) ]]
    then
        echo " *** cannot find external PCRE library (libpcre) ***"
        echo "Please install PCRE with 'port install pcre +universal', then re-run this script."
        exit
    fi
    if [[ !(-f /opt/local/lib/libreadline.dylib || -f /opt/local/lib/libreadline.a) || !(-f /opt/local/lib/libhistory.dylib || -f /opt/local/lib/libhistory.a) ]]
    then
        echo " *** cannot find external GNU Readline library (libreadline + libhistory) ***"
        echo "Please install GNU Readline with 'port install readline +universal', then re-run this script."
    fi
    PLATFORM=darwin-port

## (3) manual installation in /usr/local tree
else
    echo "Assuming external libraries to installed manually in /usr/local tree"
    if [[ !(-f /usr/local/lib/libglib-2.0.dylib || -f /usr/local/lib/libglib-2.0.a) ]]
    then
        echo " *** cannot find external GLib2 library (libglib-2.0) ***"
        echo "Please install Glib2, then re-run this script."
        exit
    fi
    if [[ !(-f /usr/local/lib/libpcre.dylib || -f /usr/local/lib/libpcre.a) ]]
    then
        echo " *** cannot find external PCRE library (libpcre) ***"
        echo "Please install PCRE, then re-run this script."
        exit
    fi
    readline_check=$(instutils/find_readline.perl --prefix)
    if [[ ! -d "$readline_check" ]]
    then
        echo " *** cannot find external GNU Readline library (libreadline + libhistory) ***"
        echo "Please install GNU Readline, then re-run this script."
        exit
    fi
    PLATFORM=darwin
fi

## check if we can install to /usr/local without sudo
## (more recent version of MacOS lock down /usr/local, but installation still works without sudi
## if relevant subdirectories exist and are writable by user)
if [[ !(-w /usr/local || -w /usr/local/bin && -w /usr/local/lib && -w /usr/local/share) ]]
then
    ## must run as root! su or sudo.
    who=$(whoami)
    if [[ "$who" != root ]]
    then
        echo " *** script must be run as root ***"
        echo "Please try again: sudo install-scripts/install-mac-osx"
        echo "Thanks!"
        exit
    else
        echo "Error: /usr/local doesn't exist or isn't writable."
        echo "Please install CWB manually, follwing instructions in INSTALL-MACOS!"
        exit
    fi
fi

## configuration has been determined and prerequisites checked => compile and install
CONFIG_FLAGS="PLATFORM=$PLATFORM SITE=standard"
echo "Success, all necessary prerequisites seem to be available."
echo "Building & installing with $CONFIG_FLAGS"

make clean $CONFIG_FLAGS \
&& make depend $CONFIG_FLAGS \
&& make cl $CONFIG_FLAGS \
&& make utils $CONFIG_FLAGS \
&& make cqp $CONFIG_FLAGS \
&& make install $CONFIG_FLAGS \
&& make realclean $CONFIG_FLAGS


