#!/bin/sh
#
# libslack - https://libslack.org
#
# Copyright (C) 1999-2004, 2010, 2020-2023 raf <raf@raf.org>
#
# 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
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <https://www.gnu.org/licenses/>.
#
# 20230824 raf <raf@raf.org>
#

# libslack-config - Helper utility for libslack clients

var() { eval $1='$2'; export $1; }
die() { echo "$@" >&2; exit 1; }

var url     "http://libslack.org/"
var name    "libslack"
var version "0.7.5"
var prefix  "/usr"
var cflags  "-DHAVE_GETOPT_LONG=1 -DHAVE_SNPRINTF=1 -DHAVE_VSSCANF=1 -DHAVE_PTHREAD_RWLOCK=1"
var libs    "-lslack -lpthread -lutil"

if test "$prefix" != "/usr" -a "$prefix" != "/usr/local"
then
	cflags="-I$prefix/include $cflags"
	libs="-L$prefix/lib $libs"
fi

usage()
{
	cat <<EOF
usage: libslack-config [options]
options:
    -h, --help      - Print this help and exit
    -v, --version   - Print the version of the currently installed libslack
    -L, --latest    - Print the latest version of libslack (uses wget)
    -p, --prefix    - Print the prefix directory of the libslack installation
    -c, --cflags    - Print CFLAGS needed to compile clients of libslack
    -l, --libs      - Print LDFLAGS needed to link against libslack
    -l, --ldflags   - Identical to --libs

Note: the dashes are optional for long option names

Command line example:
    gcc -o app app.c \`libslack-config --cflags --libs\`

Makefile example:
    CFLAGS  += \$(shell libslack-config --cflags)
    LDFLAGS += \$(shell libslack-config --libs)

EOF
	exit $1
}

latest()
{
	wget -q -O- "${url}/download/" | \
	perl -e '

		$ENV{suffix} =~ s/\./\\./g;

		while (<>)
		{
			$version{$1} = 1 if /[Hh][Rr][Ee][Ff]=".*$ENV{name}-([\d.]+)\.tar\.gz"/;
		}

		sub version_sort
		{
			my @anum = split /\./, $a;
			my @bnum = split /\./, $b;

			while ($#anum != -1 && $#bnum != -1)
			{
				return $x if $x = $bnum[0] - $anum[0];
				shift @anum;
				shift @bnum;
			}

			return -1 if $#anum != -1;
			return  1 if $#bnum != -1;
			return 0;
		}

		@version = sort { version_sort } keys %version;
		die "No versions found at $ENV{url}/download/\n" if $#version == -1;
		print "$ENV{url}/download/$ENV{name}-$version[0].tar.gz\n";
		exit 0;
	'
}


test $# -eq 0 && usage 1 1>&2

while test $# -gt 0
do
	case "$1" in
		-h|--help|help)           usage 0;;
		-v|--version|version)     echo "$version";;
		-L|--latest|latest)       latest;;
		-p|--prefix|prefix)       echo "$prefix";;
		-c|--cflags|cflags)       echo "$cflags";;
		-l|--libs|libs)           echo "$libs";;
		-l|--ldflags|ldflags)     echo "$libs";;
		*) usage 1 >&2;;
	esac
	shift
done

exit 0

# vim:set ts=4 sw=4:
