#!/bin/bash
#
# Slackware build script for google-webdesigner
#
# Copyright 2020-2021 Corrado Franco (https://corradofranco.it)
# All rights reserved.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version, with the following exception:
# the text of the GPL license may be omitted.

# This program is distributed in the hope that it will be useful, but
# without any warranty; without even the implied warranty of
# merchantability or fitness for a particular purpose. Compiling,
# interpreting, executing or merely reading the text of the program
# may result in lapses of consciousness and/or very being, up to and
# including the end of all existence and the Universe as we know it.
# See the GNU General Public License for more details.

# You may have received a copy of the GNU General Public License along
# with this program (most likely, a file named COPYING).  If not, see
# <http://www.gnu.org/licenses/>.
#
# Latest version of this SlackBuild at https://github.com/conraid/SlackBuilds

set -eu
cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=google-webdesigner
PKGNAM=$(echo $PRGNAM | tr A-Z a-z)-bin
SRCVERSION=12.0.0.0719-1
VERSION=$(echo $SRCVERSION | tr - _)
BUILD=${BUILD:-2}
TAG=${TAG:-cf}
TMP=${TMP:-/tmp/pkg}
PKG=$TMP/package-$PKGNAM
OUTPUT=${OUTPUT:-/tmp}

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

# If the variable PRINT_PACKAGE_NAME is set, then this script will report what
# the name of the created package would be, and then exit. This information
# could be useful to other scripts.
if [ ! -z "${PRINT_PACKAGE_NAME:-""}" ]; then
  echo "$PKGNAM-$VERSION-$ARCH-$BUILD.txz "
  exit 0
fi

if [ "$ARCH" = "x86_64" ]; then
  DEBARCH="amd64"
else
  echo "This platform is not supported"
  exit 1
fi

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $PKG
ar p $CWD/${PRGNAM}_current_${DEBARCH}.deb data.tar.xz | tar xJv

chown -R root:root .
chmod -R u+w,go+r-w,a+X-s .

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

mkdir -p $PKG/usr/doc/
mv $PKG/usr/share/doc/$PRGNAM $PKG/usr/doc/$PKGNAM-$VERSION
cat $CWD/$PKGNAM.SlackBuild > $PKG/usr/doc/$PKGNAM-$VERSION/$PKGNAM.SlackBuild
rmdir $PKG/usr/share/doc

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

# Change desktop file permissions
chmod 644 $PKG/usr/share/applications/$PRGNAM.desktop

# Add icons
for SIZE in 16 32 48 64 128; do
  install -Dm644 $PKG/opt/google/webdesigner/resources/product_logo_${SIZE}.png \
                 $PKG/usr/share/icons/hicolor/${SIZE}x${SIZE}/apps/google-webdesigner.png
done

# Clean up cruft/unneeded directories.The cron job in /etc is for Debian/Ubuntu only:
# (Thanks to Sbo)
rm -rf $PKG/etc

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PKGNAM-$VERSION-$ARCH-$BUILD$TAG.${PKGTYPE:-txz}
