#!/usr/bin/env bash

# Function executes a given command, detects the execution result and stops installation if error is occurred.
# param $1 - command description label
# param $2 - command with arguments
function run_command () {
	# Display a command description
	echo -n "$1... "
	# Execute a command
	$2 >/dev/null 2>/dev/null
	# Check a command execution result
	if [ $? = 0 ]; then
		# Command execution is successfully
		echo "OK"
	else
		# Error detected, repeat command for display an error message
		echo "FAILED"
		$2
		echo ""
		# Stop a script execution
		exit 1
	fi
}

clear

function pkg_install () {
	run_command "Install package $1" "$PACKAGE_INSTALL $1"
}

function pkg_download () {
	echo -n "Install package $1... "
	$WGET $3 >/dev/null 2>/dev/null
	if [ $? = 0 ]; then
		$DPKG $2 >/dev/null 2>/dev/null
		if [ $? = 0 ]; then
			rm -f $DOWNLOAD_DIR/$2
			echo "OK"
		else
			$DPKG $2
			exit 1
		fi
	else
		$WGET $3
		exit 1
	fi
}

APT="sudo apt"
PACKAGE_INSTALL="$APT -y install"
WGET=wget
BUILD_DIR=/tmp/GNUstep.build
DOWNLOAD_DIR=$BUILD_DIR/download
ARCH=amd64
DPKG="sudo dpkg -i"

mkdir -p $DOWNLOAD_DIR
cd $DOWNLOAD_DIR

run_command "Update packages repositories"	"$APT update"
pkg_install make
pkg_install cmake
pkg_install subversion
pkg_install clang
pkg_install ninja-build
pkg_install libffi-dev
pkg_install libxml2-dev
pkg_install libgnutls28-dev
pkg_install libicu-dev
pkg_install libblocksruntime-dev
pkg_download libkqueue0		libkqueue0_2.0.3-1.1_$ARCH.deb		http://ftp.br.debian.org/debian/pool/main/libk/libkqueue/libkqueue0_2.0.3-1.1_$ARCH.deb
pkg_download libkqueue-dev	libkqueue-dev_2.0.3-1.1_$ARCH.deb	http://ftp.br.debian.org/debian/pool/main/libk/libkqueue/libkqueue-dev_2.0.3-1.1_$ARCH.deb
pkg_install libpthread-workqueue-dev
pkg_install autoconf
pkg_install libtool
pkg_install libjpeg-dev
pkg_install libtiff5-dev
pkg_install libcairo2-dev
pkg_install libx11-dev
pkg_install libxt-dev
pkg_install libxft-dev
pkg_install llvm-dev
pkg_download libdispatch0	libdispatch0_0~svn197-3.2_$ARCH.deb	http://ftp.us.debian.org/debian/pool/main/libd/libdispatch/libdispatch0_0~svn197-3.2_$ARCH.deb
pkg_download libdispatch-dev	libdispatch-dev_0~svn197-3.2_$ARCH.deb	http://ftp.us.debian.org/debian/pool/main/libd/libdispatch/libdispatch-dev_0~svn197-3.2_$ARCH.deb
pkg_install patch

cd $BUILD_DIR
export CC=clang
export CXX=clang++

# Download sources
cd $DOWNLOAD_DIR
run_command "Download libobjc2"		"$WGET https://github.com/gnustep/libobjc2/archive/v1.8.1.tar.gz"
run_command "Download gnustep-make"	"$WGET ftp://ftp.gnustep.org/pub/gnustep/core/gnustep-make-2.6.8.tar.gz"
run_command "Download gnustep-base"	"$WGET ftp://ftp.gnustep.org/pub/gnustep/core/gnustep-base-1.24.9.tar.gz"
run_command "Download gnustep-gui"	"$WGET ftp://ftp.gnustep.org/pub/gnustep/core/gnustep-gui-0.25.0.tar.gz"
run_command "Download gnustep-back"	"$WGET ftp://ftp.gnustep.org/pub/gnustep/core/gnustep-back-0.25.0.tar.gz"
cd $BUILD_DIR/
run_command "Unpack libobjc2"		"tar -xvzf $DOWNLOAD_DIR/v1.8.1.tar.gz"
run_command "Unpack gnustep-make"	"tar -xvzf $DOWNLOAD_DIR/gnustep-make-2.6.8.tar.gz"
run_command "Unpack gnustep-base"	"tar -xvzf $DOWNLOAD_DIR/gnustep-base-1.24.9.tar.gz"
run_command "Unpack gnustep-gui"	"tar -xvzf $DOWNLOAD_DIR/gnustep-gui-0.25.0.tar.gz"
run_command "Unpack gnustep-back"	"tar -xvzf $DOWNLOAD_DIR/gnustep-back-0.25.0.tar.gz"
rm $DOWNLOAD_DIR/v1.8.1.tar.gz
rm $DOWNLOAD_DIR/gnustep-make-2.6.8.tar.gz
rm $DOWNLOAD_DIR/gnustep-base-1.24.9.tar.gz
rm $DOWNLOAD_DIR/gnustep-gui-0.25.0.tar.gz
rm $DOWNLOAD_DIR/gnustep-back-0.25.0.tar.gz

cd $BUILD_DIR/gnustep-make-2.6.8
run_command "Configure gnustep-make"	"./configure --enable-debug-by-default --with-layout=gnustep --enable-objc-nonfragile-abi --enable-objc-arc"
run_command "Build gnustep-make"	"make -j8"
run_command "Install gnustep-make"	"sudo -E make install"
. /usr/GNUstep/System/Library/Makefiles/GNUstep.sh

cd $BUILD_DIR/libobjc2-1.8.1
rm -Rf build
mkdir build && cd build
run_command "Make libobjc2"	"cmake ../ -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang -DCMAKE_ASM_COMPILER=clang -DTESTS=OFF"
run_command "Build libobjc2"	"cmake --build ."
run_command "Install libobjc2"	"sudo -E make install"
sudo ldconfig
export LDFLAGS=-ldispatch
OBJCFLAGS="-fblocks -fobjc-runtime=gnustep-1.8.1"

cd $BUILD_DIR/gnustep-make-2.6.8
run_command "Reconfigure gnustep-make"	"./configure --enable-debug-by-default --with-layout=gnustep --enable-objc-nonfragile-abi --enable-objc-arc"
run_command "Rebuild gnustep-make"	"make -j8"
run_command "Reinstall gnustep-make"	"sudo -E make install"

cd $BUILD_DIR/gnustep-base-1.24.9
run_command "Configure gnustep-base"	"./configure"
run_command "Build gnustep-base"	"make -j8"
run_command "Iinstall gnustep-base"	"sudo -E make install"

cd $BUILD_DIR/gnustep-gui-0.25.0
run_command "Configure gnustep-gui"	"./configure"
run_command "Build gnustep-gui"		"make -j8"
run_command "Iinstall gnustep-gui"	"sudo -E make install"

cd $BUILD_DIR/gnustep-back-0.25.0
run_command "Configure gnustep-back"	"./configure"
run_command "Build gnustep-back"	"make -j8"
run_command "Iinstall gnustep-back"	"sudo -E make install"

rm -rf $BUILD_DIR

# GNUstep was successfully installed
echo "GNUstep was successfully installed"
echo ""
echo "For sh or bash add into the /etc/profile or ~/.profile the following strings:"
echo "export CC=clang"
echo "export CXX=clang++"
echo ". /usr/GNUstep/System/Library/Makefiles/GNUstep.sh"
echo ""
echo "For csh add into the /etc/csh.login or ~/.login the following strings:"
echo "setenv CC clang"
echo "setenv CXX=clang++"
echo "source /usr/GNUstep/System/Library/Makefiles/GNUstep.csh"
echo ""
