#!/bin/sh
#
# opencv.SlackBuild
# 2024-05-21
#
# OpenCV (Open Source Computer Vision)
# is a library of programming functions for real time computer vision.

PRGNAM=opencv
VERSION=4.9.0

# 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://opencv.org/"

SOURCE="${PRGNAM}-${VERSION}.zip"
DOWNLOAD="https://github.com/opencv/opencv/archive/refs/tags/${VERSION}.zip"
MD5SUM="872cf2ded2c5e79cb5904563c0b35bf4"

DOC="${PRGNAM}-${VERSION}-docs.zip"
DOC_DOWNLOAD="https://github.com/opencv/opencv/releases/download/${VERSION}/${DOC}"
DOC_MD5SUM="90e8150620d15a23a07c6e65608ee3f0"

REQUIRES=""

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

DESC="OpenCV (Open Source Computer Vision)

is a library of programming functions for real time computer vision.

${HOMEPAGE}"

DOCS="$(pwd)/$0 CONTRIBUTING.md COPYRIGHT LICENSE README.md SECURITY.md samples"

# 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
unzip $CWD/$SOURCE || exit 1

# change to the application source directory
cd $PRGNAM-$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

mkdir -p build
cd build
cmake -D CMAKE_BUILD_TYPE="Release" \
      -D CMAKE_INSTALL_PREFIX=/usr \
      -D LIB_SUFFIX=$LIBDIRSUFFIX \
      -D CMAKE_CXX_FLAGS:STRING="$SLKCFLAGS" \
      -D CMAKE_C_FLAGS:STRING="$SLKCFLAGS" \
      -D BUILD_SHARED_LIBS:BOOL=ON \
      .. || exit 1
make || exit 1
make install DESTDIR=$PKG || exit 1
cd ..

# add documentation
mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a $DOCS $PKG/usr/doc/$PRGNAM-$VERSION
cd $PKG/usr/doc/$PRGNAM-$VERSION
unzip $CWD/$DOC
find $PKG/usr/doc/$PRGNAM-$VERSION -type f -exec chmod 644 {} \;
cd $TMP/$PRGNAM-$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

# strip binaries
find $PKG | xargs file | grep -e "executable" -e "shared object" | grep ELF \
  | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null

# 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/$PRGNAM-$VERSION
