#!/bin/bash
# Either
#DL_URL=http://your_remote_pkg_host.tld/packages/
# Or
#DL_URL=file:///your/local/pkgbuild_folder/output/
# If local DL_URL is null then slapt-get --update will use SOURCE from /etc/slapt-get/slapt-getrc

# 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
# any later version.

# Updates:
# Originally from the author of slapt-get, below:
# http://software.jaos.org/git/slapt-get/plain/FAQ.html#slgFAQ17
# 20170204 step: speed-up on multi-cores; fix/adapt for local pkgbuild repo

TAR_EXT_RE="t?[gblx]z"
MAX_PROCS=${MAX_PROCS:-4}
function list_packages {
	find . -type f \! \( -name CHECKSUMS.md5.gz -o -name PACKAGES.TXT.gz \) \
		-a -regextype posix-egrep -regex '.*\.'"$TAR_EXT_RE"'$'
}

function gen_packages_txt {
	echo '' > PACKAGES.TXT
	find . -type f -name '*.meta' -exec cat {} \; >> PACKAGES.TXT
  cat PACKAGES.TXT | gzip -9 -c - > PACKAGES.TXT.gz
}

function gen_md5_checksums {
	echo '' > CHECKSUMS.md5
	list_packages | xargs -P$MAX_PROCS -n1 md5sum >> CHECKSUMS.md5
	#md5sum $(list_packages) >> CHECKSUMS.md5 # this is slower than above
	cat CHECKSUMS.md5 | gzip -9 -c - > CHECKSUMS.md5.gz
}

function gen_meta {
	if [ ! -f $1 ]; then
		echo "File not found: $1"
		exit 1;
	fi
	case $1 in
		*-*[.-]*[.-]*.* )
			case $1 in
				*.gz|*.bz|*.lz|*.xz|*.tgz|*.tbz|*.tlz|*.txz) : go on $1 ;;
				* ) return ;;
			esac
			;;
		* ) return ;;
	esac

  PKGEXT=${1##*.}
  case $PKGEXT in
	tgz) DECOMPRESS=gzip ;;
	tbz) DECOMPRESS=bzip2 ;;
	tlz) DECOMPRESS=lzma ;;
	txz) DECOMPRESS=xz ;;
	*) echo "Error: unexpected extension for $1. Skipped" >&2
    return ;;
  esac

	NAME=$(echo $1|sed -re "s/(.*\/)(.*.$PKGEXT)$/\2/")
	LOCATION=$(echo $1|sed -re "s/(.*)\/(.*.$PKGEXT)$/\1/")
	SIZE=$(du -bk $1 | awk '{print $1}')
	USIZE=$(expr $(cat $1 | $DECOMPRESS -dc | wc -c) / 1024)
	REQUIRED=$($DECOMPRESS -dc $1 | tar -xO install/slack-required 2>/dev/null|xargs -r -iZ echo -n "Z,"|sed -e "s/,$//")
	CONFLICTS=$($DECOMPRESS -dc $1 | tar -xO install/slack-conflicts 2>/dev/null|xargs -r -iZ echo -n "Z,"|sed -e "s/,$//")
	SUGGESTS=$($DECOMPRESS -dc $1 | tar -xO install/slack-suggests 2>/dev/null|xargs -r )
	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:  $REQUIRED" >> $LOCATION/$METAFILE
	echo "PACKAGE CONFLICTS:  $CONFLICTS" >> $LOCATION/$METAFILE
	echo "PACKAGE SUGGESTS:  $SUGGESTS" >> $LOCATION/$METAFILE
	echo "PACKAGE DESCRIPTION:" >> $LOCATION/$METAFILE
	$DECOMPRESS -dc $1 | tar -xO install/slack-desc |grep -E '\w+\:'|grep -v '^#' >> $LOCATION/$METAFILE
	echo "" >> $LOCATION/$METAFILE
}

case "$1" in
	pkg)
		if [ -e "$2" ]; then
			case $2 in
				/*) echo "Error: $2 isn't a relative pathname. Skipped" >&2 ;;
				*) gen_meta "$2" ;;
			esac
		else
			echo "$0 [pkg [file]|all|new|PACKAGESTXT|MD5]"
		fi
	;;
	all)
		list_packages | xargs -P$MAX_PROCS -n1 "$0" pkg
		"$0" PACKAGESTXT
		"$0" MD5
	;;
	new)
		list_packages |
		while read pkg; do
			if [ ! -f ${pkg%${pkg##*.}}meta ]; then
				gen_meta $pkg
			fi
		done
	;;
	PACKAGESTXT)
		gen_packages_txt
	;;
	MD5)
		gen_md5_checksums
	;;
	list)
		list_packages

	;;
	*)
		echo "$0 [pkg [file]|all|new|PACKAGESTXT|MD5]"
	;;
esac
