#!/bin/bash
# Part of SMLinux distribution
# Package build file generator
set -e

# Date format
FILEDATE="$(date +%Y%m%d%H%M%S)"

# Archive files
ARCHIVEFILES=( *.tar.* *.zip *.t?z )

# Function to generate usage options
mkusage() {
   cat << EOF

mksm is a tool to generate SMLinux-compatible package build files. It currently
supports generation of build files for sources that support compilation using:
-> Make
-> Autoconf
-> Cmake
-> Meson

Usage: mksm <application_name> <application_version> <build_system>

**You must be inside the directory containing source tarball when running mksm.**. 
For example if you want to build glib 2.60.0, 

$ mkdir glib && cd glib
$ wget <glib-source-tarball-URL>
$ ls 
... 
glib-2.60.0.tar.xz
...

$ mksm glib 2.60.0 meson

EOF
}

# If/else to check if first argument is set. If it isn't, invoke mkusage
# function and exit
if [ -z $1 ] ; then
  mkusage
  exit 0
fi
  

# Explanation same as above, but this time, if first argument is set then
# set use it as a value for $APPNAME variable
if [ -n "$1" ] ; then
  if [ "$1" = "-h" ] || [ "$1" = "-help" ] || [ "$1" = "--help" ] ; then
    mkusage
    exit 0
  else
    APPNAME="$1"
   fi
fi

# If/else to check if second argument is set. If it is, set it as a value
# for $VERSION variable. If it isn't, throw an error and exit.
if [ -n "$2" ] ; then
  VERSION="$2"
else
  echo "Version not defined. Kindly define it. Exiting!"
  exit 1
fi

# If/else to check if third argument is set. If it is, set it as a value for
# $BUILDSYS variable. If it isn't, throw an error and exit.
if [ -n "$3" ] ; then
  if [ "$3" = "make" ] ; then
     BUILDSYS="make" 
  elif [ "$3" = "autoconf" ] ; then
     BUILDSYS="autoconf" 
  elif [ "$3" = "cmake" ] ; then
     BUILDSYS="cmake" 
  elif [ "$3" = "meson" ] ; then
     BUILDSYS="meson" 
  else 
     echo "ERROR: Invalid build system. Aborting!"
     exit 1
  fi
else
  echo "Build system not defined. Kindly define one from make, autoconf, cmake or meson. Exiting!"
  exit 1
fi

# If $APPNAME, $VERSION and $BUILDSYS variables are set, generate a build file.
if [ -n "$APPNAME" ] && [ -n "$VERSION" ] && [ -n "$BUILDSYS" ] ; then
  echo "Generating build file for application '"$APPNAME"' version '"$VERSION"' which uses build system '"$BUILDSYS"'..."
  echo ""
  echo "Current directory is $PWD"
  echo ""

  # Copy the sample build file based on the build system argument passed
  if [ -f "$APPNAME.SMBuild" ] ; then
    echo "Found an existing $APPNAME.SMBuild in the current directory." 
    echo "Backing it up inside a directory 'old' and creating a new one here." 
    mkdir -p old
    mv "$APPNAME.SMBuild" old/"$APPNAME.SMBuild.$FILEDATE"
  fi
   
   # This one came in handy:
   # https://unix.stackexchange.com/questions/505949/expanding-only-certain-variables-inside-a-heredoc
   # Expand variables in this heredoc
   cat << EOF > $APPNAME.SMBuild
APP=$APPNAME
VERSION=$VERSION
BUILD=1sml
HOMEPAGE=""
DOWNLOAD=""
DESC=""
REQUIRES="musl"
EOF
   
   # Single-quoted 'EOF' prevents variables from being expanded in this heredoc
   cat << 'EOF' >> $APPNAME.SMBuild
   
build() {
  mkandenterbuilddir
  rm -rf $APP-$VERSION

  tar xf $SRCDIR/$APP-$VERSION.tar.?z*
  cd $APP-$VERSION
  fixbuilddirpermissions

EOF
   
   # Again prevent variables from being expanded in this heredoc
   gen_autoconf() {
     cat << 'EOF' >> $APPNAME.SMBuild
  ./configure \
    --prefix="" \
    --sysconfdir=/etc

  make 
  make install DESTDIR=$PKG

  cp LICENSE $PKGDOCS/

  mkfinalpkg
}
EOF
}

   gen_make() {
    cat << 'EOF' >> $APPNAME.SMBuild
  make
  make install DESTDIR=$PKG

  cp LICENSE $PKGDOCS/

  mkfinalpkg
}
EOF
}
   # And again
   gen_cmake() {
    cat << 'EOF' >> $APPNAME.SMBuild
  mkdir smbuild && cd smbuild
  cmake .. \
    -DCMAKE_INSTALL_PREFIX="/" \
    -DCMAKE_C_FLAGS="$CFLAGS" \
    -DCMAKE_CXX_FLAGS="$CXXFLAGS" \
    -Wno-dev

  make
  make install DESTDIR=$PKG
  
  cp ../LICENSE $PKGDOCS/

  mkfinalpkg
}
EOF
}
   
   # Again :)
   gen_meson() {
    cat << 'EOF' >> $APPNAME.SMBuild
  mkdir smbuild && cd smbuild
  meson .. \
    --prefix="/" 

  ninja
  DESTDIR="$PKG" ninja install

  cp ../LICENSE $PKGDOCS/

  mkfinalpkg
}
EOF
}

   case "$BUILDSYS" in 
      autoconf) gen_autoconf ;;
      make) gen_make ;;
      cmake) gen_cmake ;;
      meson) gen_meson ;;
   esac

   chmod +x $APPNAME.SMBuild
   echo "...done"
   echo ""

fi

# Generate sha512sums in the build file

  echo "Adding new SHA512SUMS in $APPNAME.SMBuild..."
  printf '\n' >> "$APPNAME".SMBuild
  printf 'SHA512SUMS="\n' >> "$APPNAME".SMBuild

  # File types
  FILES=( *.tar.* *.zip *.t?z *.patch *.diff *.c *.h )

  # Checksum digest to be used along with arguments
  CHECKSUMBINARY="sha512sum"

  for FILE in ${FILES[@]} ; do
     if [ -f "$FILE" ] ; then
       $CHECKSUMBINARY $FILE >> "$APPNAME".SMBuild
     fi
  done
  printf '"' >> "$APPNAME".SMBuild
  exit 0

