#!/bin/sh # $Id: domoticz.SlackBuild,v 1.2 2021/11/12 16:35:08 root Exp root $ # Copyright 2021 Eric Hameleers, Eindhoven, NL # All rights reserved. # # Permission to use, copy, modify, and distribute this software for # any purpose with or without fee is hereby granted, provided that # the above copyright notice and this permission notice appear in all # copies. # # THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED 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 AUTHORS AND COPYRIGHT HOLDERS AND THEIR # CONTRIBUTORS 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. # ----------------------------------------------------------------------------- # # Slackware SlackBuild script # =========================== # By: Eric Hameleers # For: domoticz # Descr: home automation system # URL: https://www.domoticz.com/ # Build needs: (Slackware 14.2: cmake > 3.16, gcc > 7) # Needs: cereal, jsoncpp, lua53 # Changelog: # 2021.1-1: 10/nov/2021 by Eric Hameleers # * Initial build. # # Run 'sh domoticz.SlackBuild' to build a Slackware package. # The package (.t?z) and .txt file as well as build logs are created in /tmp . # Install the package using 'installpkg' or 'upgradepkg --install-new'. # # ----------------------------------------------------------------------------- PRGNAM=domoticz VERSION=${VERSION:-2021.1} ZWAVEVER=${ZWAVEVER:-20211110} BOOSTVER=${BOOSTVER:-1_77_0} OSSLVER=${OSSLVER:-1.1.1l} BUILD=${BUILD:-1} NUMJOBS=${NUMJOBS:-" -j$(nproc) "} TAG=${TAG:-alien} DOCS="History.txt INSTALL.md License.txt README.md" # User 'domoticz' (uidNr=371) and group 'domoticz' (grpId=371) # are also used by SBo. DOMO_UID=${DOMO_UID:-371} DOMO_GID=${DOMO_GID:-371} DOMO_HOME="/opt/domoticz" # Slackware older than 15 has a too old boost, openssl and gcc, # so we need to use some static/internal libraries: if [ $(cat /etc/slackware-version |cut -d' ' -f2 |cut -d. -f1) -ge 15 ] ; then # Slackware 15 or newer: DO_OLDSLACK='NO' else DO_OLDSLACK='YES' fi if [ "$P1" != "--download" ]; then # User and group must be present on build system: if ! getent passwd domoticz 1>/dev/null 2>/dev/null; then printf "\n\tYou must have a 'domoticz' user and group to run this script:\n" printf "\t# groupadd -g $DOMO_GID domoticz\n" printf "\t# useradd -g $DOMO_GID -u $DOMO_UID -M -d ${DOMO_HOME} -c 'Domoticz home automation' -s /bin/false domoticz\n\n" exit 1 fi fi # Where do we look for sources? SRCDIR=$(cd $(dirname $0); pwd) # Place to build (TMP) package (PKG) and output (OUTPUT) the program: TMP=${TMP:-/tmp/build} PKG=$TMP/package-$PRGNAM OUTPUT=${OUTPUT:-/tmp} # We will install a static boost here: BOOSTINSTDIR="$TMP/tmp-$PRGNAM/boostinst" # We will install a static openssl here: OSSLINSTDIR="$TMP/tmp-$PRGNAM/osslinst" SOURCE[0]="$SRCDIR/${PRGNAM}-${VERSION}.tar.gz" SRCURL[0]="" SOURCE[1]="$SRCDIR/open-zwave-${ZWAVEVER}.tar.gz" SRCURL[1]="" SOURCE[2]="$SRCDIR/utlist.h" SRCURL[2]="https://raw.githubusercontent.com/troydhanson/uthash/master/src/utlist.h" SOURCE[3]="$SRCDIR/boost_${BOOSTVER}.tar.gz" SRCURL[3]="https://boostorg.jfrog.io/artifactory/main/release/$(echo $BOOSTVER |tr _ .)/source/boost_${BOOSTVER}.tar.gz" SOURCE[4]="$SRCDIR/openssl-${OSSLVER}.tar.gz" SRCURL[4]="https://www.openssl.org/source/openssl-${OSSLVER}.tar.gz" # We checkout some of the sources from git instead: GITURI[0]="https://github.com/${PRGNAM}/${PRGNAM}.git" BRANCH[0]="development" GITTAG[0]="$VERSION" GITURI[1]="https://github.com/domoticz/open-zwave.git" BRANCH[1]="master" # Use the src_checkout() function if no downloadable tarball exists. # Check out sources from SVN/GIT and create a tarball of them. # Examples for GIT and SVN repositories are given below: src_checkout() { # Param #1 : index in the SOURCE[] array. # Param #2 : full path to where SOURCE[$1] tarball should be created. # Determine the tarball extension: PEXT=$(echo "${2}" | sed -r -e 's/.*[^.].(tar.xz|tar.gz|tar.bz2|tgz).*/\1/') case "$PEXT" in "tar.xz") TARCOMP="J" ;; "tar.gz") TARCOMP="z" ;; "tgz") TARCOMP="z" ;; "tar.bz2") TARCOMP="j" ;; *) echo "Archive can only have extension 'tar.xz', '.tar.gz' '.tar.bz2' or '.tgz'" ; exit 1 ;; esac # Determine the directory name to create for the archive root: PBASE=$(basename ${2} .${PEXT}) # Determine the directory where we create our checkout: CODIR=$(dirname ${2}) case ${1} in 0) RETDIR=$(pwd) mkdir -p $CODIR/${PRGNAM}_temp_checkout_$$ \ && cd $CODIR/${PRGNAM}_temp_checkout_$$ mkdir ${PBASE} \ && cd ${PBASE} echo "Checking out tag '${GITTAG[0]}' from '${GITURI[0]}':" git clone ${GITURI[0]} . \ && git checkout ${BRANCH[0]} \ && git checkout tags/${GITTAG[0]} \ && git submodule update --init --recursive \ && chown -R root:root . \ && cd .. \ && tar -${TARCOMP}cf ${2} ${PBASE} cd $RETDIR rm -rf $CODIR/${PRGNAM}_temp_checkout_$$ || true ;; 1) RETDIR=$(pwd) mkdir -p $CODIR/${PRGNAM}_temp_checkout_$$ \ && cd $CODIR/${PRGNAM}_temp_checkout_$$ mkdir ${PBASE} \ && cd ${PBASE} echo "Checking out '${BRANCH[1]}' at date $ZWAVEVER from '${GITURI[1]}':" git clone ${GITURI[1]} . \ && git checkout ${BRANCH[1]} \ && git checkout $(git rev-list -n 1 --before="$(date -d $ZWAVEVER)" ${BRANCH[1]}) \ && chown -R root:root . \ && cd .. \ && tar -${TARCOMP}cf ${2} ${PBASE} cd $RETDIR rm -rf $CODIR/${PRGNAM}_temp_checkout_$$ || true ;; *) # Do nothing ;; esac } ## ## --- with a little luck, you won't have to edit below this point --- ## ## # Automatically determine the architecture we're building on: if [ -z "$ARCH" ]; then case "$(uname -m)" in i?86) ARCH=i586 ;; arm*) readelf /usr/bin/file -A | egrep -q "Tag_CPU.*[4,5]" && ARCH=arm || ARCH=armv7hl ;; # Unless $ARCH is already set, use uname -m for all other archs: *) ARCH=$(uname -m) ;; esac export ARCH fi # Set CFLAGS/CXXFLAGS and LIBDIRSUFFIX: case "$ARCH" in i?86) SLKCFLAGS="-O2 -march=${ARCH} -mtune=i686" SLKLDFLAGS=""; LIBDIRSUFFIX="" ;; x86_64) SLKCFLAGS="-O2 -fPIC" SLKLDFLAGS="-L/usr/lib64"; LIBDIRSUFFIX="64" ;; armv7hl) SLKCFLAGS="-O2 -march=armv7-a -mfpu=vfpv3-d16" SLKLDFLAGS=""; LIBDIRSUFFIX="" ;; *) SLKCFLAGS=${SLKCFLAGS:-"-O2"} SLKLDFLAGS=${SLKLDFLAGS:-""}; LIBDIRSUFFIX=${LIBDIRSUFFIX:-""} ;; esac case "$ARCH" in arm*) TARGET=$ARCH-slackware-linux-gnueabi ;; *) TARGET=$ARCH-slackware-linux ;; esac # Exit the script on errors: set -e trap 'echo "$0 FAILED at line ${LINENO}" | tee $OUTPUT/error-${PRGNAM}.log' ERR # Catch unitialized variables: set -u P1=${1:-1} # Save old umask and set to 0022: _UMASK_=$(umask) umask 0022 # Create working directories: mkdir -p $OUTPUT # place for the package to be saved mkdir -p $TMP/tmp-$PRGNAM # location to build the source mkdir -p $PKG # place for the package to be built mkdir -p $BOOSTINSTDIR # where boost gets installed mkdir -p $OSSLINSTDIR # where openssl gets installed rm -rf $PKG/* # always erase old package's contents rm -rf $TMP/tmp-$PRGNAM/* # remove the remnants of previous build rm -rf $OUTPUT/{checkout,configure,make,install,error,makepkg,patch}-$PRGNAM.log # remove old log files # Source file availability: for (( i = 0; i < ${#SOURCE[*]}; i++ )) ; do if ! [ -f ${SOURCE[$i]} ]; then echo "Source '$(basename ${SOURCE[$i]})' not available yet..." # Check if the $SRCDIR is writable at all - if not, download to $OUTPUT [ -w "$SRCDIR" ] || SOURCE[$i]="$OUTPUT/$(basename ${SOURCE[$i]})" if [ -f ${SOURCE[$i]} ]; then echo "Ah, found it!"; continue; fi if ! [ "x${SRCURL[$i]}" == "x" ]; then echo "Will download file to $(dirname $SOURCE[$i])" wget --no-check-certificate -nv -T 20 -O "${SOURCE[$i]}" "${SRCURL[$i]}" || true if [ $? -ne 0 -o ! -s "${SOURCE[$i]}" ]; then echo "Fail to download '$(basename ${SOURCE[$i]})'. Aborting the build." mv -f "${SOURCE[$i]}" "${SOURCE[$i]}".FAIL exit 1 fi else # Try if we have a SVN/CVS download routine for ${SOURCE[$i]} echo "Will checkout sources to $(dirname $SOURCE[$i])" src_checkout $i "${SOURCE[$i]}" \ 2>&1 > $OUTPUT/checkout-$(basename ${SOURCE[$i]}).log fi if [ ! -f "${SOURCE[$i]}" -o ! -s "${SOURCE[$i]}" ]; then echo "File '$(basename ${SOURCE[$i]})' not available. Aborting the build." exit 1 fi fi done if [ "$P1" == "--download" ]; then echo "Download complete." exit 0 fi # --- PACKAGE BUILDING --- echo "++" echo "|| $PRGNAM-$VERSION" echo "++" cd $TMP/tmp-$PRGNAM echo -n "Extracting the source archive(s) for $PRGNAM :." tar -xf ${SOURCE[0]} echo -n ".. $(basename ${SOURCE[0]}) done." tar -xf ${SOURCE[1]} echo -n ".. $(basename ${SOURCE[1]}) done." tar -xf ${SOURCE[3]} echo -n ".. $(basename ${SOURCE[3]}) done." tar -xf ${SOURCE[4]} echo -n ".. $(basename ${SOURCE[4]}) done." echo chown -R root:root * chmod -R u+w,go+r-w,a+rX-st * # Prevent mosquitto compilation error: install -m0664 ${SOURCE[2]} ${PRGNAM}-${VERSION}/extern/mosquitto/lib/ # First, do a static compilation of open-zwave, libopenzwave.a will be created # in the root after successful build but never gets actually installed. # And domoticz expects this directory: mv open-zwave-${ZWAVEVER} open-zwave-read-only cd open-zwave-read-only CXXFLAGS="$SLKCFLAGS -Wno-stringop-truncation -Wno-maybe-uninitialized -Wno-nonnull" \ CFLAGS="$SLKCFLAGS -Wno-stringop-truncation -Wno-maybe-uninitialized -Wno-nonnull" \ BUILD=release \ make \ 2>&1 | tee $OUTPUT/install-${PRGNAM}_openzwave.log cd - 1>/dev/null # Statically compile boost, for two reasons: # 1- the version of boost in Slackware 14.2 is too old anyway, and # 2- boost upgrades in Slackware usually break stuff. PYTHON_VERSION=$(python2 -c 'import sys; print("{}.{}".format(sys.version_info.major, sys.version_info.minor))') cd boost_${BOOSTVER} # Build bjam, the boost build system: ./bootstrap.sh \ --with-toolset=gcc \ --with-icu \ --with-python=/usr/bin/python2 \ 2>&1 | tee $OUTPUT/configure-${PRGNAM}_boost.log # Build boost using bjam: ./b2 \ $NUMJOBS \ --layout=system \ --build-dir=build-python2 \ --prefix=/usr \ --libdir=/usr/lib$LIBDIRSUFFIX \ --build-type=minimal \ toolset=gcc \ variant=release \ debug-symbols=off \ link=static \ threading=multi \ python=$PYTHON_VERSION \ cflags="$SLKCFLAGS" \ cxxflags="$SLKCFLAGS -fpermissive" \ stage \ 2>&1 | tee $OUTPUT/make-${PRGNAM}_boost.log #--with-system --with-thread \ # And then install boost.. ./b2 \ $NUMJOBS \ --layout=system \ --build-dir=build-python2 \ --prefix=$BOOSTINSTDIR/usr \ --libdir=$BOOSTINSTDIR/usr/lib$LIBDIRSUFFIX \ --build-type=minimal \ toolset=gcc \ variant=release \ debug-symbols=off \ link=static \ threading=multi \ python=$PYTHON_VERSION \ cflags="$SLKCFLAGS" \ cxxflags="$SLKCFLAGS -fpermissive" \ install \ 2>&1 | tee $OUTPUT/install-${PRGNAM}_boost.log #--with-system --with-thread \ cd - 1>/dev/null # Also statically compile openssl. The 1.1.1l release is the minimum, # so the version of boost in Slackware 14.2 is too old. cd openssl-${OSSLVER} CFLAGS="$SLKCFLAGS -static-libgcc" \ ./config \ --prefix=/usr \ zlib \ enable-camellia \ enable-seed \ enable-rfc3779 \ enable-cms \ enable-md2 \ enable-rc5 \ enable-ssl3 \ enable-ssl3-method \ no-weak-ssl-ciphers \ no-mdc2 \ no-ec2m \ no-idea \ no-sse2 \ no-shared \ 2>&1 | tee $OUTPUT/configure-${PRGNAM}_openssl.log make depend \ 2>&1 | tee $OUTPUT/make-${PRGNAM}_openssl.log make \ 2>&1 | tee -a $OUTPUT/make-${PRGNAM}_openssl.log make install DESTDIR=$OSSLINSTDIR \ 2>&1 | tee $OUTPUT/install-${PRGNAM}_openssl.log cd - 1>/dev/null # Finally, we can compile domoticz using the static libraries we just built. cd ${PRGNAM}-${VERSION} echo Building ... #export Boost_INCLUDE_DIR="$BOOSTINSTDIR/usr/include" #export Boost_LIBRARY_DIR_DEBUG="$BOOSTINSTDIR/usr/lib${LIBDIRSUFFIX}" #export Boost_LIBRARY_DIR_RELEASE="$BOOSTINSTDIR/usr/lib${LIBDIRSUFFIX}" mkdir -p build-${PRGNAM} cd build-${PRGNAM} cmake \ -DCMAKE_C_FLAGS:STRING="$SLKCFLAGS" \ -DCMAKE_CXX_FLAGS:STRING="$SLKCFLAGS" \ -DCMAKE_C_FLAGS_RELEASE:STRING="$SLKCFLAGS" \ -DCMAKE_CXX_FLAGS_RELEASE:STRING="$SLKCFLAGS" \ -DCMAKE_BUILD_TYPE=Release \ -DCMAKE_INSTALL_PREFIX=/opt/domoticz \ -DLIB_SUFFIX=${LIBDIRSUFFIX} \ -DUSE_BUILTIN_JSONCPP=${DO_OLDSLACK} \ -DUSE_BUILTIN_MINIZIP=YES \ -DUSE_BUILTIN_MQTT=YES \ -DUSE_BUILTIN_SQLITE=${DO_OLDSLACK} \ -DUSE_STATIC_BOOST=YES \ -DUSE_STATIC_LIBSTDCXX=${DO_OLDSLACK} \ -DUSE_STATIC_OPENZWAVE=YES \ -DUSE_OPENSSL_STATIC=${DO_OLDSLACK} \ -DUSE_PYTHON=YES \ -DBoost_NO_BOOST_CMAKE=TRUE -DBOOST_ROOT=$BOOSTINSTDIR/usr \ -DOPENSSL_ROOT_DIR=$OSSLINSTDIR/usr \ .. make $NUMJOBS 2>&1 | tee $OUTPUT/make-${PRGNAM}.log make DESTDIR=$PKG install 2>&1 | tee $OUTPUT/install-${PRGNAM}.log cd - # Remove update script: rm -f $PKG/opt/domoticz/updatedomo # Install a boot script plus default configuration: install -D -m0664 $SRCDIR/rc.${PRGNAM} $PKG/etc/rc.d/rc.${PRGNAM}.new install -D -m0664 $SRCDIR/default.${PRGNAM} $PKG/etc/default/${PRGNAM}.new # Install a logrotate script: install -D -m0644 $SRCDIR/logrotate.${PRGNAM} $PKG/etc/logrotate.d/${PRGNAM}.new # Set up the database directory: mkdir -p $PKG/var/lib/domoticz chown $DOMO_UID:$DOMO_GID $PKG/var/lib/domoticz chmod 770 $PKG/var/lib/domoticz # Set up the pid directory: mkdir -p $PKG/var/run/domoticz chown $DOMO_UID:$DOMO_GID $PKG/var/run/domoticz # Set up the log directory: mkdir -p $PKG/var/log/domoticz chown $DOMO_UID:$DOMO_GID $PKG/var/log/domoticz # When running Domoticz without root (recommended), you need to add # the correct socket capabilities for this to function properly. # Add the e, i and p capabilities to domoticz binary: setcap cap_net_raw+eip $PKG/opt/domoticz/domoticz # Validate that these capabilities have indeed been added: setcap -v cap_net_raw+eip $PKG/opt/domoticz/domoticz # Add this to the doinst.sh: mkdir -p $PKG/install cat <> $PKG/install/doinst.sh # Handle the incoming configuration files: config() { for infile in \$1; do NEW="\$infile" OLD="\$(dirname \$NEW)/\$(basename \$NEW .new)" # If there's no config file by that name, mv it over: if [ ! -r \$OLD ]; then mv \$NEW \$OLD elif [ "\$(cat \$OLD | md5sum)" = "\$(cat \$NEW | md5sum)" ]; then # toss the redundant copy rm \$NEW fi # Otherwise, we leave the .new copy for the admin to consider... done } preserve_perms() { NEW="\$1" OLD="\$(dirname \$NEW)/\$(basename \$NEW .new)" if [ -e \$OLD ]; then cp -a \$OLD \${NEW}.incoming cat \$NEW > \${NEW}.incoming mv \${NEW}.incoming \$NEW fi config \$NEW } config etc/default/domoticz.new config etc/logrotate.d/domoticz.new preserve_perms etc/rc.d/rc.domoticz.new # Only way to create and use the correct uid and gid on the target system, # is to use chroot: chroot . </dev/null # Add the '${DOMO_GID}' group if it doesn't exist already: /usr/sbin/groupadd -g ${DOMO_GID} domoticz 2> /dev/null # Add the '${DOMO_UID}' user if it doesn't exist already: /usr/sbin/useradd -c "Domoticz home automation" -d ${DOMO_HOME} -M \\ -s "/bin/false" -g ${DOMO_GID} -u ${DOMO_UID} domoticz 2> /dev/null EOR # Update rc.local so that Domoticz daemon will be started on boot if ! grep "rc.domoticz" etc/rc.d/rc.local 1>/dev/null 2>&1 ; then cat <<_EOM_ >> etc/rc.d/rc.local if [ -x /etc/rc.d/rc.domoticz ]; then # Start Domoticz daemon echo "Starting Domoticz daemon: /etc/rc.d/rc.domoticz start" /etc/rc.d/rc.domoticz start fi _EOM_ fi # Update rc.local_shutdown so that Domoticz daemon will be stopped properly: if ! grep "rc.domoticz" etc/rc.d/rc.local_shutdown 1>/dev/null 2>&1 ; then cat <<_EOS_ >> etc/rc.d/rc.local_shutdown if [ -x /etc/rc.d/rc.domoticz ]; then # Stop Domoticz daemon echo "Stopping Domoticz daemon: /etc/rc.d/rc.domoticz stop" /etc/rc.d/rc.domoticz stop fi _EOS_ fi EOINS # Add documentation: mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION cp -a $DOCS $PKG/usr/doc/$PRGNAM-$VERSION || true cat $SRCDIR/$(basename $0) > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild chown -R root:root $PKG/usr/doc/$PRGNAM-$VERSION find $PKG/usr/doc -type f -exec chmod 644 {} \; # Strip binaries (if any): find $PKG | xargs file | grep -e "executable" -e "shared object" | grep ELF \ | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null || true # Add a package description: mkdir -p $PKG/install cat $SRCDIR/slack-desc > $PKG/install/slack-desc cat $SRCDIR/slack-required > $PKG/install/slack-required # Build the package: cd $PKG makepkg --linkadd y --chown n $OUTPUT/${PRGNAM}-${VERSION}-${ARCH}-${BUILD}${TAG}.${PKGTYPE:-txz} 2>&1 | tee $OUTPUT/makepkg-${PRGNAM}.log cd $OUTPUT md5sum ${PRGNAM}-${VERSION}-${ARCH}-${BUILD}${TAG}.${PKGTYPE:-txz} > ${PRGNAM}-${VERSION}-${ARCH}-${BUILD}${TAG}.${PKGTYPE:-txz}.md5 cd - cat $PKG/install/slack-desc | grep "^${PRGNAM}" > $OUTPUT/${PRGNAM}-${VERSION}-${ARCH}-${BUILD}${TAG}.txt cat $PKG/install/slack-required > $OUTPUT/${PRGNAM}-${VERSION}-${ARCH}-${BUILD}${TAG}.dep # Restore the original umask: umask ${_UMASK_}