#!/bin/bash

GIT_URL="https://github.com/dino/dino.git"
MASTER_DIR="dino"
BUILD_DIR="build"


if [[ -z "${1}" ]];then
	echo "Please specify how many threads you want the make command to use (starting from 0)"
	echo "Example: ${0} 5"
	echo "The above example will use 4 threads"
	exit 0
fi
echo "getting dependencies..."

if [[ -f /usr/bin/apt ]];then
	sudo apt install cmake valac libgee-0.8-dev libsqlite3-dev libgtk-3-dev libnotify-dev libgpgme-dev libsoup2.4-dev libgcrypt20-dev libqrencode-dev gettext libsignal-protocol-c-dev
elif [[ -f /usr/bin/dnf ]];then
	sudo dnf install cmake vala libgee-devel gtk3-devel libgcrypt-devel qrencode-devel gdk-pixbuf2-devel libsoup-devel gpgme-devel libsignal-protocol-c-devel
else
	echo "Package manager not detected"
fi

if [[ ! -d "${MASTER_DIR}" ]];then
		git clone ${GIT_URL}
		cd ${MASTER_DIR}
		./configure
		make -j${1}

		if [[ ! -f "${BUILD_DIR}/dino" ]];then
			echo "The build failed, since the ${BUILD_DIR} is missing"
			exit 1
		else
			echo "You can now run the binary located in $(pwd)/${BUILD_DIR}"
		fi
else echo "${MASTER_DIR} directory exists already"
fi
