#!/bin/bash

# Originally written by Jason Woodward, https://software.jaos.org/
# Modified extensively for use in SMLinux

# Part of SMLinux distribution
# slapt-get-compatible package list generator
# 
# Copyright (c) 2022 PktSurf <smlinux@pktsurf.in> 

# 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.

# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

all() {
  echo "Generating meta files, package lists and md5sum files..."
for pkg in $(find . -type f -regex '.*\.[tgblzikx]+$' -print)
do
  gen_meta $pkg
done

# Generate package text files
 echo '' > PACKAGES.TXT
 find . -type f -name '*.meta' -exec cat {} \; >> PACKAGES.TXT
  cat PACKAGES.TXT | gzip -9 -c - > PACKAGES.TXT.gz

# Generate md5 files
  echo '' > CHECKSUMS.md5
  find . -type f -regextype posix-egrep -regex '.*\.[tgblzikx]+$' -exec md5sum {} \; >> CHECKSUMS.md5
  cat CHECKSUMS.md5 | gzip -9 -c - > CHECKSUMS.md5.gz

}
  
help() {  
cat << EOF
SMLinux package list generator

This program creates PACKAGES.TXT, CHECKSUMS.md5 and .meta files for use with slapt-get package manager.

$ ls
CHECKSUMS.md5 CHECKSUMS.md5.gz
PACKAGES.TXT PACKAGES.TXT.gz
packages/

Typical usage: put all the final packages inside a directory. copy genpackagelist to
that directory and then run 

$ ./genpackagelist -a

This will create PACKAGES.TXT{.gz}, CHECKSUMS.md5{.gz} and <package>.meta files
Copy those files to a web server's DOCROOT directory and point slapt-get to it by 
creating a new SOURCE line in /etc/slapt-get/slapt-get.rc:

SOURCE=http://example.com/path/to/above/files:CUSTOM

Then run
$ sudo slapt-get -u 
$ sudo slapt-get -i <package>

Usage: 
    -a : Generate package lists, checksum and meta files
    
    -h : Display this message
EOF
exit 0
}
  
gen_meta() {
  # Begin subshell
  (
  tarball="$1"
  if [[ ! -f $tarball ]]; then
    echo "File not found: $1"
    exit 1;
  fi

  if [ "$(echo $tarball|grep -E '(.*{1,})\-(.*[\.\-].*[\.\-].*).[tgblzikx]{2,}[ ]{0,}$')" == "" ]; then
	  return;
  fi

  pkgext=${tarball##*.}
  case $pkgext in
    tgz) decompress=gzip ;;
    tbz) decompress=bzip2 ;;
    tlz) decompress=lzip ;;
    txz) decompress=xz ;;
  esac
 
  echo "Processing $tarball..."

  # Create a unique temporary directory and untar the package inside it
  tempdir=$(mktemp -d $tarball.XXXXXX)
  tar -xf $tarball -C"$tempdir"

  name=$(echo $tarball | sed -re "s/(.*\/)(.*.$pkgext)$/\2/")

  location=$(echo $tarball | sed -re "s/(.*)\/(.*.$pkgext)$/\1/")
  size=$(du -bk $tarball | awk '{print $1}')
  usize=$(expr $( $decompress -dc $tarball | wc -c) / 1024)

  # There is only one SMBuild file per package shipped in all SMLinux packages
  buildfile="$(find $tempdir/share/doc/ -name "*.SMBuild")"
  source $buildfile
  #name="$app"

  metafile=${name%$pkgext}meta
  echo "PACKAGE NAME:  $name" > $location/$metafile
  if [ -n "$dl_url" ]; then
    echo "PACKAGE MIRROR:  $dl_url" >> $location/$metafile
  fi
  echo "PACKAGE LOCATION:  $location" >> $location/$metafile
  echo "PACKAGE SIZE (compressed):  $size K" >> $location/$metafile
  echo "PACKAGE SIZE (uncompressed):  $usize K" >> $location/$metafile
  echo "PACKAGE REQUIRED:  $requires" >> $location/$metafile

  echo "PACKAGE DESCRIPTION: $desc" >> $location/$metafile
  echo "" >> $location/$metafile
  echo "...done!"
  rm -rf $tempdir
  )
}

while getopts ':a' option ; do
  case "$option" in
    a) all ;;
    *) help ;;
  esac
done

if [[ $OPTIND -le 1 ]] ; then
  help
fi
