#!/usr/bin/env bash

# GMP - a free library for arbitrary precision arithmetic
# MPFR - Multiple-precision floating-point computation
# Boost - Portable C++ source libraries
# zlib - Unobtrusive Compression Library
# Eigen - Eigen is a C++ template library for linear algebra
# Chibi-Scheme - 
# CGAL - The Computational Geometry Algorithms Library

sudo apt-get install -y libgmp-dev libmpfr-dev zlib1g-dev libeigen3-dev libcgal-dev
sudo apt-get install -y geomview

DEST=${1:-/etc/skel}


# Checkinstall gamma to produce a deb package
cd /var/tmp || exit

GITHUB_USER=dpapavas
PROJECT=gamma
ARCH=$(dpkg --print-architecture)
# RELEASE=$(lastversion --format="tag" "${GITHUB_USER}/${PROJECT}") #$(lastversion --form "${PROJECT}")
# LATEST_URL="https://api.github.com/repos/${GITHUB_USER}/${PROJECT}/releases/latest"
# RELEASE=$(curl -L -s -H 'Accept: application/json' "${LATEST_URL}" | grep -Po '"tag_name": "v\K[^"]*')
RELEASE=$(lastversion ${GITHUB_USER}/${PROJECT})
# GIT_TAG=$(curl -L -s -H 'Accept: application/json' "${LATEST_URL}" | jq -r .tag_name)
GIT_TAG=$(lastversion --format=tag ${GITHUB_USER}/${PROJECT})
# DOWNLOAD_DIR=/tmp

RSS_FEED="https://github.com/${USER}/${PROJECT}/releases.atom"
# xdg-open ${RSS_FEED}
echo ${RSS_FEED}

# make a containing directory
mkdir -p /var/tmp/${PROJECT}-"${RELEASE}"
cd /var/tmp/${PROJECT}-"${RELEASE}" || exit

cat >./description-pak <<-EOF
	 Gamma transforms code, scheme or lua, into geometry, written with solid modeling for CAD/CAM applications in mind.
EOF

cat >./checkinstall_it.sh <<-EOF

	echo "ENTERING CHECKINSTALL"

	BASE_URL='https://raw.githubusercontent.com/'
	BASE_USER=${GITHUB_USER}
	BASE_REPO=${PROJECT}
	LICENSE_URL="${BASE_URL}/${BASE_USER}/${BASE_REPO}"/master/LICENSE
	# wget -c ${LICENSE_URL}

	# VERSION=$(date +%Y-%m-%d_)git
	VERSION=${RELEASE}
	RELEASE="1"
	LICENSE=MIT

	# make a new temporary directory for this use
	BASE_TMP_DIR=~/tmptmp/checkinstall_tmp
	mkdir -p \${BASE_TMP_DIR}

	# do your work
	checkinstall -y --fstrans \
		--exclude=/root/.sudo_as_admin_successful \
		--pkgname=\${PROJECT} \
		--pkgversion=\${VERSION}\
		--pkgrelease="\${RELEASE}" \
		--pkgarch=${ARCH} \
		--pkggroup=development \
		--pkglicense=MIT \
		--pkgsource=${LATEST_URL} \
		--maintainer=cyteen@ring-zero.co.uk \
		--requires= \
		-D \
		bash ./install.sh
EOF

cat >./install.sh <<-EOF
	#!/bin/bash

	# allow specifying different destination directory
	DIR="${DIR:-"$HOME/.local/bin"}"

	# map different architecture variations to the available binaries
	ARCH=$(uname -m)
	case \$ARCH in
		i386|i686) ARCH=x86 ;;
		armv6*) ARCH=armv6 ;;
		armv7*) ARCH=armv7 ;;
		aarch64*) ARCH=arm64 ;;
	esac

	GITHUB_FILE="${PROJECT}_${RELEASE}_$(uname -s)_\${ARCH}.tar.gz"
	# GITHUB_URL="https://github.com/${GITHUB_USER}/${PROJECT}/releases/download/${GIT_TAG}/\${GITHUB_FILE}"
	GITHUB_URL="https://github.com/${GITHUB_USER}/${PROJECT}"

  # Debian is current with stable release.
  # git clone --depth=1 https://github.com/CGAL/cgal.git

  git clone --depth=1 https://github.com/dpapavas/chibi-scheme.git
  pushd chibi-scheme
    make clibs.c
    make distclean
    make libchibi-scheme.a SEXP_USE_DL=0 "CPPFLAGS=-DSEXP_USE_STATIC_LIBS -DSEXP_USE_STATIC_LIBS_NO_INCLUDE=0"
  popd

  wget -c https://www.lua.org/ftp/lua-5.4.4.tar.gz
  tar -zxf lua-5.4.4.tar.gz
  pushd lua-5.4.4/src/
    make CC=g++ liblua.a
  popd

  # installs to /usr/local by default
	git clone --depth 1 --branch ${GIT_TAG} \$GITHUB_URL ${PROJECT}
  pushd ${PROJECT} || exit
    echo "gamma build dir: \${PWD}"
    cmake -DCMAKE_BUILD_TYPE=Release -DCGAL_ROOT=../cgal -DChibi_ROOT=../chibi-scheme -DLua_ROOT=../lua-5.4.4/src 
    make -j4
    make install
  popd
EOF

bash ./checkinstall_it.sh

