# This is a simple SMLinux-compatible build file with all the necessary explanation
# for helping users quickly understand the build system while referring to the bldpkg
# file and to start building and using SMLinux distro packages. Experienced slackware 
# users may find this file more familiar and can therefore parse more quickly.

# Simplified build file logic for slackware and non-slackware users:
# a. Set all necessary options in /etc/bldpkg.conf
# b. Create app directory and download its source
# c. Copy sample build file sample.SMBuild from /etc
# d. Define app, version, build, homepage, download, desc and requires  variables in the build file
# e. Optionally add and define sm_distcc, sm_ccache and sm_debug variables
# f. mkandenterbuilddir function removes old package build directory, makes a new one and cd's into $tmp
# g. Old source directory is removed from $tmp and new one is extracted into it
# h. fixbuilddirpermissions function fixes any permissions inside the new source directory
# i. Add any commands to extract the source directory if the source directory has a non-standard hierarchy
# j. Usual compile options like ./configure --prefix="" ; make ; make install DESTDIR=$pkg follow
# k. Add any runit service files, if optionally needed, using preprunitservice <service name> down
# l. mkfinalpkg function prepares the final package directory and places it inside a location defined in $pkgdest in /etc/bldpkg.conf


#!/bin/bash

app=openssh
# ^ define the application name to be built/compiled and packaged.
# **This variable must be defined.**
version=8.1
# ^ define the application's version number
# **This variable must be defined.**
build=1sml
# ^ define the build tag and count of the package in case changes are being introduced
# to this build file while the version is still the same. The count is incremented
# everytime a change is made to the build file. If the version is different, reset 
# this to 1sml for the new version and increment from there on if changes are made again.
# **This variable must be defined.**

homepage=""
# ^ Add Homepage URL or website of the author of package to this. This variable *must be defined*

download=""
# ^ Add Download URL of the package source ( can be tar.xz or tar.gz or any of the tarballs / zipfiles available ). 
# This variable *must be defined*

desc=""
# ^ Add a brief description of what the package is about. This variable *must be defined*.

requires=""
# ^ Add runtime dependencies to this variable, each dependency separated by a space. This variable *must be defined*.

sm_distcc=0
# ^ This variable, when set to 0, will cause the build system to not use distcc 
# for building this particular package. See also how sm_globaldistcc variable 
# in bldpkg.conf affects this option.
# This variable is optional.

sm_ccache=0
# ^ This variable, when set to 0,  will cause the build system to not use ccache 
# for building this particular package. See also how sm_globalccache variable 
# in bldpkg.conf affects this option. 
# This variable is optional.

sm_debug=1
# ^ This variable, when set to 1, will produce a debug build by preserving any 
# debug symbols in the binaries and libraries contained in $pkg. 
# Default is to strip all binaries and libraries. 
# This variable is optional.

sm_noautoconfsite=1
# ^ This variable, when set to 1, will disable exporting of the CONFIG_SITE variable.
# CONFIG_SITE points to a file path defined in bldpkg.conf that holds predefined answers
# to various configure tests. Use it if a package's configure script returns errors
# which it normally wouldn't when the cache file was not used.
# This variable is optional.

sm_skipchecksum=1
# ^ This variable, when set to 1, will cause the build system to skip checksum verifications. Use it when building 
# packages that contain files without any extensions, like scripts or any binaries

build() {
# ^ Start of the "build" function. This function lists the steps for the order in which to compile
# a given package. The order basically consists of invoking other functions that are defined
# in bldpkg.d.

  compileonlyfor x86_64
  # ^ Function defined in and sourced from bldpkg to compile the application only on 
  # a predefined architecture or exit with a code 1 if determined otherwise.

  mkandenterbuilddir
  # ^ Function defined in and sourced from bldpkg.conf to prepare the package directories. Similar
  # to SlackBuild's rm -rf $tmp/package-$app ; mkdir $tmp/package-$app, but in addition to that,
  # it also creates the "pkgdocs" directory that may contain a copy of this build file and
  # LICENSEs and READMEs if any, as defined by the user later in the build, and $pkgdest directory, 
  # where the final package will be put into.  When that is done, this function will *cd into $tmp*
  # ** This function is necessary for most builds** unless you have a compelling reason to preserve
  # old source directories.


  rm -rf $app-$version
  # ^ remove the old source directory inside *$TMP/$APP.src*


  tar xf $srcdir/$app-$version.tar.?z*
  # ^ Extract the source tarball from $SRCDIR, in effect creating a new source directory inside $TMP.
  # This source directory is useful when you have to delete further extracted source directories,
  # because this directory is unique, and when using TMPFS, the build system can delete this 
  # directory specifically instead of attempting to hunt for source directories it contains.
  # For example: /tmp/tmpfsdir/glib.src/glib-2.99
  # In this path, when using TMPFS, the build system will delete /tmp/tmpfsdir/glib.src instead
  # of deleting glib-2.99.


  cd $app-$version
  # ^ cd into $tmp/$app.src/$app-$version


  fixbuilddirpermissions
  # ^ Function defined in and sourced from bldpkg to fix permissions inside the newly extracted 
  # build directory
  # ** This function is necessary **


  ./configure \		     --------+
    --prefix="" \		     |
    --sysconfdir=/etc 		     |  
				     |    
  make				     |
  make install DESTDIR=$pkg    	     |	 
				     |
  #                 OR		     |
				     | <- the usual compile stuff that goes into building the package 
  mkdir -p build && cd build         |    prior to installing it into $PKG
  meson .. \ 			     |
    --prefix="" \                    |
    -Ddocs=false                     |
              			     |
  ninja             		     |
  DESTDIR="$pkg" ninja install ------+


  preprunitservice <openssh> <down> <finish>
  # ^ Function defined in and sourced from bldpkg to create etc/service/<service> and var/service/<service> 
  # directories and symlinks, and to copy the service.run and service.finish file to appropriate runit-aware
  # locations. 
  # preprunitservice is the name of the function with three possible arguments:
  # $1 - openssh, defines the name of the runit service
  # $2 - down, defines whether to add a "down" file in var/service/<service> to prevent its execution in
  #      the current session or at the next boot
  # $3 - finish, defines whether to add a "finish" file to cause runit to cleanly terminate the service.
  #      when told. The "finish" file should have all the cleanup routines defined.
  # This function and its arguments are optional

  
  cp LICENSE $pkgdocs/


  removestaticlibs
  # ^ Function to forcefully discard any static libraries in case explicitly disabling building them doesn't work


  mkfinalpkg
  # ^ Function defined in and sourced from bldpkg to:
  # a. Copy post-install files like slack-{desc,required} and doinst.sh
  # b. Compress and link man pages
  # c. Determine whether to strip or produce a debug build
  # d. Provide a copy of this entire build file insode $pkgdocs
  # e. Generate the final smlinux-compatible package inside $pkgdest
  # f. Generate a build summary including time when the build started, stopped, whether distcc or ccache
  #    was used, compressed/uncompressed sizes of the final package if the build completed successfully, 
  #    number of CPU threads, distcc options if any, build type (whether it was a debug build or not). 
  #    Note: the output of this summary will also be generated when the user invokes ctrl-C aka SIGINT 
  #    anytime during the build and the output may slightly vary. Kindly refer to bldpkg for more info.
  # ** This function is necessary **

}
# Closing brace for the build function.

# END OF THE BUILD FILE
