#!/bin/sh
#
# cgal.SlackBuild
# 2024-05-20
#
# CGAL - Computational Geometry Algorithms Library
# The goal of the CGAL Open Source Project is to provide easy access to
# efficient and reliable geometric algorithms in the form of a C++ library.

PRGNAM=cgal
VERSION=5.6.1

# automatically determine the architecture
if [ -z "$ARCH" ]; then
  case "$(uname -m)" in
    arm*) ARCH=arm ;;
       *) ARCH=$(uname -m) ;;
  esac
fi

BUILD=${BUILD:-1}
TAG=${TAG:-scr}
PKGTYPE=${PKGTYPE:-txz}

HOMEPAGE="https://www.cgal.org/"

SOURCE="CGAL-${VERSION}.tar.xz"
DOWNLOAD="https://github.com/CGAL/cgal/releases/download/v${VERSION}/${SOURCE}"
MD5SUM="90ccd8f68894ab2f89933163afd66535"

DOC="CGAL-${VERSION}-doc_html.tar.xz"
DOC_DOWNLOAD="https://github.com/CGAL/cgal/releases/download/v${VERSION}/${DOC}"
DOC_MD5SUM="c09b04d8de6c738b749939a817bf3a43"

REQUIRES=""

MAINTAINER="Gernot WALZL"
EMAIL="gernot.walzl@gmx.at"

DESC="CGAL - Computational Geometry Algorithms Library

The goal of the CGAL Open Source Project is to provide easy access
to efficient and reliable geometric algorithms in the form of a
C++ library.

${HOMEPAGE}"

DOCS="$(pwd)/$0
AUTHORS CHANGES.md INSTALL.md LICENSE* README VERSION doc_html demo examples"

# EOF info


# set initial variables
CWD=$(pwd)
TMP=${TMP:-/tmp}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

# exit on error
set -e

# download the source if needed
if [ ! -f $CWD/$SOURCE ]; then
  wget -O $CWD/$SOURCE $DOWNLOAD || exit 1
fi
if [ ! -f $CWD/$DOC ]; then
  wget -O $CWD/$DOC $DOC_DOWNLOAD || exit 1
fi

# check for correct source file
if [ "$(md5sum $CWD/$SOURCE | cut -f 1 -d ' ')" != "$MD5SUM" ]; then
  exit 1
fi
if [ "$(md5sum $CWD/$DOC | cut -f 1 -d ' ')" != "$DOC_MD5SUM" ]; then
  exit 1
fi

# extract the application source
cd $TMP || exit 1
tar xvf $CWD/$SOURCE || exit 1

# change to the application source directory
cd CGAL-${VERSION} || exit 1

# fix permissions
chown -R root:root .
chmod -R u+w,go+r-w,a-s .

# set configure options
if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
 elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
 elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
 else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

# configure and build
rm -rf $PKG
mkdir -p $PKG/install
mkdir -p $PKG/usr
DESTDIR=$PKG/usr
# CGAL_DISABLE_ROUNDING_MATH_CHECK enables the use of valgrind
# (include/CGAL/Interval_nt.h)
cmake -D CMAKE_INSTALL_PREFIX="$DESTDIR" \
      -D CMAKE_CXX_FLAGS:STRING="$SLKCFLAGS" \
      -D CMAKE_C_FLAGS:STRING="$SLKCFLAGS" \
      -D CMAKE_BUILD_TYPE="Release" \
      -D CGAL_INSTALL_DOC_DIR="doc/${PRGNAM}-${VERSION}" \
      -D CGAL_INSTALL_LIB_DIR="lib${LIBDIRSUFFIX}" \
      -D CGAL_INSTALL_MAN_DIR="man/man1" \
      -D CGAL_DISABLE_ROUNDING_MATH_CHECK:BOOL="ON" \
      . || exit 1
make || exit 1
make install || exit 1

# add documentation
mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a $DOCS $PKG/usr/doc/$PRGNAM-$VERSION
cd $PKG/usr/doc/$PRGNAM-$VERSION
tar xvf $CWD/$DOC
find $PKG/usr/doc/$PRGNAM-$VERSION -type f -exec chmod 644 {} \;
cd $TMP/CGAL-${VERSION} || exit 1

# compress man pages
if [ -d $PKG/usr/man ]; then
  find $PKG/usr/man -type f -name "*.?" -exec gzip -9 {} \;
  for i in $(find $PKG/usr/man -type l -name "*.?") ; do
    ln -s $(readlink $i).gz $i.gz
    rm $i
  done
fi

# add some description to the package
if [ "$DESC" != "" ]; then
  echo "$DESC" | sed 's/^/'$PRGNAM': /g' > $PKG/install/slack-desc
fi

# append dependency information to the package
if [ "$REQUIRES" != "" ]; then
  for REQ in $REQUIRES; do
    echo "$REQ" >> $PKG/install/slack-required
  done
fi

# build the package
cd $PKG
PKGVERSION=$(echo $VERSION | sed 's/-/./g')
makepkg -l y -c n \
  $OUTPUT/$PRGNAM-$PKGVERSION-$ARCH-$BUILD$TAG.$PKGTYPE \
  || exit 1

# make everything clean
cd $CWD
rm -rf $PKG
rm -rf $TMP/CGAL-${VERSION}
