#!/bin/sh

# Slackware build script for pixma

# Copyright 2019 Christoph Willing  Brisbane, Australia
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

PRGNAM=pixma
VERSION=${VERSION:-4.10.1}
BUILD=${BUILD:-1}
TAG=${TAG:-AL}

SRCNAM=cnijfilter-source
SRCVERSION=${SRCVERSION:-4.10-1}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

CWD=$(pwd)
TMP=${TMP:-/tmp/AL}
PKG=$TMP/package-$SRCNAM
OUTPUT=${OUTPUT:-/tmp}

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

PRINTER_PKG_PROGRAM="ppd cnijfilter"
MODELS=${MODELS:-}
if [ -z $MODELS ]; then
  PRINTER_DEPEND_PKG="cnijfilter-ix6700-431 cnijfilter-ix6800-432 cnijfilter-ip2800-433 cnijfilter-mx470-434 cnijfilter-mx530-435 cnijfilter-ip8700-436 cnijfilter-e560-437 cnijfilter-e400-438"
  echo "PRINTER_DEPEND_PKG = $PRINTER_DEPEND_PKG"
else
  PRINTER_DEPEND_PKG=""
  for m in $MODELS ; do
    PRINTER_DEPEND_PKG="$PRINTER_DEPEND_PKG $m"
  done
  echo "PRINTER_DEPEND_PKG = $PRINTER_DEPEND_PKG"
fi

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $SRCNAM-$SRCVERSION
tar xvf $CWD/$SRCNAM-$SRCVERSION.tar.gz
cd $SRCNAM-$SRCVERSION

# Cups >= 1.6
patch -p0 < $CWD/00_IPP_PRIVATE_STRUCTURES.diff
patch -p0 < $CWD/04_ppd_h.diff
patch -p1 < $CWD/mychanges.patch

# Correct cups directory location
sed -i -e "s/lib\/cups/lib$LIBDIRSUFFIX\/cups/" cnijbe/src/Makefile.am
sed -i -e "s/lib\/cups/lib$LIBDIRSUFFIX\/cups/" backend/src/Makefile.am
sed -i -e "s/lib\/cups/lib$LIBDIRSUFFIX\/cups/" backendnet/backend/Makefile.am

chown -R root:root .
find -L . \
 \( -perm 777 -o -perm 775 -o -perm 750 -o -perm 711 -o -perm 555 \
  -o -perm 511 \) -exec chmod 755 {} \; -o \
 \( -perm 666 -o -perm 664 -o -perm 640 -o -perm 600 -o -perm 444 \
  -o -perm 440 -o -perm 400 \) -exec chmod 644 {} \;

(cd ./libs; CFLAGS="$SLKCFLAGS" CXXFLAGS="$SLKCFLAGS" ./autogen.sh --prefix=/usr )
(cd ./bscc2sts; CFLAGS="$SLKCFLAGS" CXXFLAGS="$SLKCFLAGS" ./autogen.sh )
(cd ./cnijnpr; CFLAGS="$SLKCFLAGS" CXXFLAGS="$SLKCFLAGS" ./autogen.sh --prefix=/usr --enable-libpath=/usr/lib$LIBDIRSUFFIX/bjlib )
(cd ./cngpij; CFLAGS="$SLKCFLAGS" CXXFLAGS="$SLKCFLAGS" ./autogen.sh --prefix=/usr --enable-progpath=/usr/bin )
(cd ./cngpijmnt; CFLAGS="$SLKCFLAGS" CXXFLAGS="$SLKCFLAGS" ./autogen.sh --prefix=/usr --enable-progpath=/usr/bin )
(cd ./pstocanonij; CFLAGS="$SLKCFLAGS" CXXFLAGS="$SLKCFLAGS" ./autogen.sh --libdir=/usr/lib$LIBDIRSUFFIX --enable-progpath=/usr/bin )
(cd ./backend; CFLAGS="$SLKCFLAGS" CXXFLAGS="$SLKCFLAGS" ./autogen.sh --prefix=/usr --enable-progpath=/usr/bin )
(cd ./backendnet; CFLAGS="$SLKCFLAGS" CXXFLAGS="$SLKCFLAGS" ./autogen.sh --prefix=/usr --enable-progpath=/usr/bin LDFLAGS="-L../../com/libs_bin$LIBDIRSUFFIX" )
(cd ./cmdtocanonij; CFLAGS="$SLKCFLAGS" CXXFLAGS="$SLKCFLAGS" ./autogen.sh --prefix=/usr --libdir=/usr/lib$LIBDIRSUFFIX )
(cd ./cnijbe; CFLAGS="$SLKCFLAGS" CXXFLAGS="$SLKCFLAGS" ./autogen.sh --prefix=/usr --enable-progpath=/usr/bin )
(cd ./lgmon2; CFLAGS="$SLKCFLAGS" CXXFLAGS="$SLKCFLAGS" ./autogen.sh --prefix=/usr --enable-progpath=/usr/bin LDFLAGS="-L../../com/libs_bin$LIBDIRSUFFIX" )

(cd ./ppd; CFLAGS="$SLKCFLAGS" CXXFLAGS="$SLKCFLAGS" ./autogen.sh --prefix=/usr --enable-ppdpath=/usr/share/ppd --program-suffix=CN_IJ_MODEL )
(cd ./cnijfilter; CFLAGS="$SLKCFLAGS" CXXFLAGS="$SLKCFLAGS" ./autogen.sh --prefix=/usr --enable-libpath=/usr/lib$LIBDIRSUFFIX/bjlib --enable-binpath=/usr/bin --program-suffix=CN_IJ_MODEL )

for pkg in $PRINTER_DEPEND_PKG ; do
  model_name=`echo ${pkg} | cut -d- -f2` && \
  comm_name=`echo ${pkg} | cut -d- -f1` && \
  model_num=`echo ${pkg} | cut -d- -f3` && \
  model_dir=${comm_name}-${model_name}series && \
  for pg_name in $PRINTER_PKG_PROGRAM ; do
    pushd ./$pg_name
    find . -name Makefile -print > file_lists; \
    find . -name config.h -print >> file_lists; \
    for fn in $(cat file_lists) ; do
      if [ ! -f ${fn}.org ]; then
        cp ${fn} ${fn}.org
      fi
      sed -e s/CN_IJ_MODEL_NUM/${model_num}/g ${fn}.org | sed -e s/CN_IJ_MODEL/${model_name}/ > cn_ij_tmp; mv cn_ij_tmp ${fn}
    done
    make clean; make; make install DESTDIR=$PKG
    popd
  done
  mkdir -p ${PKG}/usr/lib${LIBDIRSUFFIX}
  install -m 755 ${model_num}/libs_bin${LIBDIRSUFFIX}/libcnbpcmcm${model_num}.so.* ${PKG}/usr/lib${LIBDIRSUFFIX}/
  install -m 755 ${model_num}/libs_bin${LIBDIRSUFFIX}/libcnbpcnclapi${model_num}.so.* ${PKG}/usr/lib${LIBDIRSUFFIX}/
  install -m 755 ${model_num}/libs_bin${LIBDIRSUFFIX}/libcnbpcnclbjcmd${model_num}.so.* ${PKG}/usr/lib${LIBDIRSUFFIX}/
  install -m 755 ${model_num}/libs_bin${LIBDIRSUFFIX}/libcnbpcnclui${model_num}.so.* ${PKG}/usr/lib${LIBDIRSUFFIX}/
  install -m 755 ${model_num}/libs_bin${LIBDIRSUFFIX}/libcnbpess${model_num}.so.* ${PKG}/usr/lib${LIBDIRSUFFIX}/
  install -m 755 ${model_num}/libs_bin${LIBDIRSUFFIX}/libcnbpo${model_num}.so.* ${PKG}/usr/lib${LIBDIRSUFFIX}/
  mkdir -p ${PKG}/usr/lib${LIBDIRSUFFIX}/bjlib
  install -m 644 ${model_num}/database/cif${model_name}.conf ${PKG}/usr/lib${LIBDIRSUFFIX}/bjlib
  install -m 644 ${model_num}/database/cnb_${model_num}0.tbl ${PKG}/usr/lib${LIBDIRSUFFIX}/bjlib
  install -m 644 ${model_num}/database/cnbpname${model_num}.tbl ${PKG}/usr/lib${LIBDIRSUFFIX}/bjlib
  if [ -f ${model_num}/database/cif${model_name}.bscc ]; then
    install -m 644 -o lp -g lp ${model_num}/database/cif${model_name}.bscc ${PKG}/usr/lib${LIBDIRSUFFIX}/bjlib || true
  fi
done

( cd ./libs; make clean; make )
( cd ./bscc2sts; make clean; make )
( cd ./cnijnpr; make clean; make; make install DESTDIR=$PKG )
( cd ./cngpij; make clean; make; make install DESTDIR=$PKG)
( cd ./cngpijmnt; make clean; make; make install DESTDIR=$PKG)
( cd ./pstocanonij; make clean; make; make install DESTDIR=$PKG )
( cd ./backend; make clean; make; make install DESTDIR=$PKG )
( cd ./backendnet; make clean; make; make install  DESTDIR=$PKG )
( cd ./cmdtocanonij; make clean; make; make install DESTDIR=$PKG )
( cd ./cnijbe; make clean; make; make install DESTDIR=$PKG )
( cd ./lgmon2; make clean; make; make install DESTDIR=$PKG )

install -m 755 com/libs_bin${LIBDIRSUFFIX}/libcnnet.so.* ${PKG}/usr/lib${LIBDIRSUFFIX}/
install -m 755 com/libs_bin${LIBDIRSUFFIX}/libcnbpcnclapicom.so.* ${PKG}/usr/lib${LIBDIRSUFFIX}/
install -m 644 -o lp -g lp com/ini/cnnet.ini ${PKG}/usr/lib${LIBDIRSUFFIX}/bjlib/ || true

mkdir -p $PKG/etc/udev/rules.d
install -m 644 etc/*.rules ${PKG}/etc/udev/rules.d/

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

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a LICENSE-cnijfilter-4*.txt $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.${PKGTYPE:-txz}
cat $PKG/install/slack-desc | grep "^${PRGNAM}" > \
$OUTPUT/$PRGNAM-${VERSION}-${ARCH}-${BUILD}${TAG}.txt
