#!/bin/sh
# Configuration script for the PCI library
# (c) 1998--2013 Martin Mares <mj@ucw.cz>

LC_ALL=C
export LC_ALL

echo_n() {
	printf '%s' "$*"
}

if [ -z "$VERSION" ] ; then
	echo >&2 "Please run the configure script from the top-level Makefile"
	exit 1
fi

echo_n "Configuring libpci for your system..."
if [ -z "$HOST" ] ; then
	sys=`uname -s`
	rel=`uname -r`
	realsys="$sys"
	if [ "$sys" = "AIX" -a -x /usr/bin/oslevel -a -x /usr/sbin/lsattr ]
	then
		rel=`/usr/bin/oslevel`
		proc=`/usr/sbin/lsdev -C -c processor -S available -F name | head -1`
		cpu=`/usr/sbin/lsattr -F value -l $proc -a type | sed 's/_.*//'`
	else
		cpu=`uname -m | sed 's/^i.86-AT386/i386/;s/^i.86$/i386/;s/^sun4u$/sparc64/;s/^i86pc$/i386/;s/^BePC$/i386/;s/^BeMac$/powerpc/;s/^BeBox$/powerpc/'`
	fi
	if [ "$sys" = "DragonFly" ]
	then
		sys=freebsd
	fi
	if [ "$sys" = "GNU/kFreeBSD" ]
	then
		sys=kfreebsd
	fi
	if [ "$sys" = "GNU" ]
	then
		sys=gnu
	fi
	if [ "$sys" = "CYGWIN_NT-5.1" -o "$sys" = "CYGWIN_NT-6.0" ]
	then
		sys=cygwin
	fi
	HOST=${3:-$cpu-$sys}
fi
[ -n "$RELEASE" ] && rel="${RELEASE}"
# CAVEAT: tr on Solaris is a bit weird and the extra [] is otherwise harmless.
host=`echo $HOST | sed -e 's/^\([^-]*\)-\([^-]*\)-\([^-]*\)-\([^-]*\)$/\1-\3/' -e 's/^\([^-]*\)-\([^-]*\)-\([^-]*\)$/\1-\2/' -e 's/^\([^-]*\)-\([^-]*\)$/\1--\2/' | tr '[A-Z]' '[a-z]'`
cpu=`echo $host | sed 's/^\([^-]*\)-\([^-]*\)-\(.*\)$/\1/'`
sys=`echo $host | sed 's/^\([^-]*\)-\([^-]*\)-\(.*\)$/\3/'`
echo " $host $rel $cpu $sys"

c=config.h
m=config.mk
echo >$c '#define PCI_CONFIG_H'
echo >>$c "#define PCI_ARCH_`echo $cpu | tr '[a-z]' '[A-Z]'`"
echo >>$c "#define PCI_OS_`echo $sys | tr '[a-z]' '[A-Z]'`"
echo >$m 'WITH_LIBS='

echo_n "Looking for access methods..."
LIBRESOLV=-lresolv
LIBEXT=so
EXEEXT=
SYSINCLUDE=/usr/include
LSPCIDIR=SBINDIR

case $sys in
	linux*)
		echo_n " sysfs proc mem-ports ecam"
		echo >>$c '#define PCI_HAVE_PM_LINUX_SYSFS'
		echo >>$c '#define PCI_HAVE_PM_LINUX_PROC'
		echo >>$c '#define PCI_HAVE_PM_MMIO_CONF'
		echo >>$c '#define PCI_HAVE_PM_ECAM'
		echo >>$c '#define PCI_HAVE_LINUX_BYTEORDER_H'
		echo >>$c '#define PCI_PATH_PROC_BUS_PCI "/proc/bus/pci"'
		echo >>$c '#define PCI_PATH_SYS_BUS_PCI "/sys/bus/pci"'
		echo >>$c '#define PCI_PATH_DEVMEM_DEVICE "/dev/mem"'
		echo >>$c '#define PCI_PATH_ACPI_MCFG "/sys/firmware/acpi/tables/MCFG"'
		echo >>$c '#define PCI_PATH_EFI_SYSTAB "/sys/firmware/efi/systab"'
		case $cpu in
				i?86|x86_64)	echo_n " i386-ports"
						echo >>$c '#define PCI_HAVE_PM_INTEL_CONF'
						;;
		esac
		echo >>$c '#define PCI_HAVE_64BIT_ADDRESS'
		LSPCIDIR=BINDIR
		;;
	sunos)
		echo_n " mem-ports ecam"
		case $cpu in
				i?86)		echo_n " i386-ports"
						echo >>$c "#define PCI_HAVE_PM_INTEL_CONF"
						;;
		esac
		echo >>$c '#define PCI_HAVE_PM_MMIO_CONF'
		echo >>$c '#define PCI_HAVE_PM_ECAM'
		echo >>$c '#define PCI_PATH_DEVMEM_DEVICE "/dev/xsvc"'
		echo >>$c '#define PCI_PATH_ACPI_MCFG ""'
		echo >>$c '#define PCI_PATH_EFI_SYSTAB ""'
		;;
	freebsd*|kfreebsd*)
		echo_n " fbsd-device mem-ports ecam"
		echo >>$c '#define PCI_HAVE_PM_FBSD_DEVICE'
		echo >>$c '#define PCI_HAVE_PM_MMIO_CONF'
		echo >>$c '#define PCI_HAVE_PM_ECAM'
		echo >>$c '#define PCI_PATH_FBSD_DEVICE "/dev/pci"'
		echo >>$c '#define PCI_PATH_DEVMEM_DEVICE "/dev/mem"'
		echo >>$c '#define PCI_PATH_ACPI_MCFG ""'
		echo >>$c '#define PCI_PATH_EFI_SYSTAB ""'
		if [ "$sys" != "kfreebsd" ] ; then
			LIBRESOLV=
		fi
		;;
        openbsd)
	        echo_n " obsd-device mem-ports ecam"
		echo >>$c '#define PCI_HAVE_PM_OBSD_DEVICE'
		echo >>$c '#define PCI_HAVE_PM_MMIO_CONF'
		echo >>$c '#define PCI_HAVE_PM_ECAM'
		echo >>$c '#define PCI_PATH_OBSD_DEVICE "/dev/pci"'
		echo >>$c '#define PCI_PATH_DEVMEM_DEVICE "/dev/mem"'
		echo >>$c '#define PCI_PATH_ACPI_MCFG "/var/db/acpi/MCFG.*"'
		echo >>$c '#define PCI_PATH_EFI_SYSTAB ""'
		case $cpu in
				i386|amd64)	echo_n " i386-ports"
						echo >>$c '#define PCI_HAVE_PM_INTEL_CONF'
						echo >>$m 'WITH_LIBS+=-l'$cpu
						;;
		esac
		LIBRESOLV=
		;;

        darwin*)
	        echo_n " darwin"
		echo >>$c '#define PCI_HAVE_PM_DARWIN_DEVICE'
		echo >>$m 'WITH_LIBS+=-lresolv -framework CoreFoundation -framework IOKit'
		echo >>$c '#define PCI_HAVE_64BIT_ADDRESS'
		LIBRESOLV=
		LIBEXT=dylib
		SYSINCLUDE=$(xcrun --sdk macosx --show-sdk-path)/usr/include
		;;
	aix)
		echo_n " aix-device"
		echo >>$c '#define PCI_HAVE_PM_AIX_DEVICE'
		echo >>$m 'CFLAGS=-g'
		echo >>$m 'INSTALL=installbsd'
		echo >>$m 'DIRINSTALL=mkdir -p'
		;;
	netbsd)
		echo_n " nbsd-libpci mem-ports ecam"
		echo >>$c '#define PCI_HAVE_PM_NBSD_LIBPCI'
		echo >>$c '#define PCI_HAVE_PM_MMIO_CONF'
		echo >>$c '#define PCI_HAVE_PM_ECAM'
		echo >>$c '#define PCI_PATH_NBSD_DEVICE "/dev/pci0"'
		echo >>$c '#define PCI_PATH_DEVMEM_DEVICE "/dev/mem"'
		echo >>$c '#define PCI_PATH_ACPI_MCFG ""'
		echo >>$c '#define PCI_PATH_EFI_SYSTAB ""'
		echo >>$c '#define PCI_HAVE_64BIT_ADDRESS'
		echo >>$m 'LIBNAME=libpciutils'
		echo >>$m 'WITH_LIBS+=-lpci'
		LIBRESOLV=
		;;
	gnu)
		echo_n " hurd i386-ports"
		echo >>$c '#define PCI_HAVE_PM_HURD_CONF'
		echo >>$c '#define PCI_HAVE_PM_INTEL_CONF'
		;;
	djgpp)
		echo_n " i386-ports"
		echo >>$c '#define PCI_HAVE_PM_INTEL_CONF'
		EXEEXT=.exe
		;;
	cygwin|windows)
		echo_n " win32-cfgmgr32 win32-kldbg win32-sysdbg"
		echo >>$c '#define PCI_HAVE_64BIT_ADDRESS'
		echo >>$c '#define PCI_HAVE_PM_WIN32_CFGMGR32'
		echo >>$c '#define PCI_HAVE_PM_WIN32_KLDBG'
		echo >>$c '#define PCI_HAVE_PM_WIN32_SYSDBG'
		# Warning: MinGW-w64 (incorrectly) provides cfgmgr32 functions
		# also in other import libraries, not only in libcfgmgr32.a.
		# So always set -lcfgmgr32 as a first library parameter which
		# instruct linker to prefer symbols from cfgmgr32.dll.
		echo >>$m 'WITH_LIBS+=-lcfgmgr32'
		case $cpu in i?86|x86_64)
		echo_n " i386-ports"
		echo >>$c '#define PCI_HAVE_PM_INTEL_CONF'
		if [ "$sys" = "cygwin" ] ; then
			# ioperm is cygwin specific library and used only by lib/i386-io-cygwin.h
			echo >>$m 'WITH_LIBS+=-lioperm'
		elif [ "$sys" = "windows" ] ; then
			# advapi32 is windows system library and used only by lib/i386-io-windows.h
			echo >>$m 'WITH_LIBS+=-ladvapi32'
		fi
		;; esac
		EXEEXT=.exe
		LIBEXT=dll
		;;
	beos|haiku)
		echo_n " mem-ports ecam"
		case $cpu in
				i?86|x86_64)	echo_n " i386-ports"
						echo >>$c '#define PCI_HAVE_PM_INTEL_CONF'
						;;
		esac
		echo >>$c '#define PCI_HAVE_PM_MMIO_CONF'
		echo >>$c '#define PCI_HAVE_PM_ECAM'
		echo >>$c '#define PCI_PATH_DEVMEM_DEVICE "/dev/misc/mem"'
		echo >>$c '#define PCI_PATH_ACPI_MCFG ""'
		echo >>$c '#define PCI_PATH_EFI_SYSTAB ""'
		if [ "$sys" != "beos" ] ; then
			LIBRESOLV=-lnetwork
		fi
		;;
	sylixos)
		echo >>$c '#define PCI_PATH_SYLIXOS_DEVICE "/proc/pci"'
		echo >>$c '#define PCI_HAVE_64BIT_ADDRESS'
		echo >>$c '#define PCI_HAVE_PM_SYLIXOS_DEVICE'
		IDSDIR="/etc/pci"
		LIBRESOLV=
		;;
	amigaos)
		echo_n " aos-expansion"
		echo >>$c '#define PCI_HAVE_STDINT_H'
		echo >>$c '#define PCI_HAVE_PM_AOS_EXPANSION'
		IDSDIR="DEVS:"
		echo >>$m 'CC=gcc'
		;;
	*)
		echo " Unfortunately, your OS is not supported by the PCI Library"
		exit 1
		;;
esac

echo >>$m "LIBEXT="$LIBEXT
echo >>$m "EXEEXT="$EXEEXT
echo >>$m "LSPCIDIR=\$($LSPCIDIR)"
echo >>$c '#define PCI_HAVE_PM_DUMP'
echo " dump"

echo_n "Checking for zlib support... "
if [ "$ZLIB" = yes -o "$ZLIB" = no ] ; then
	echo "$ZLIB (set manually)"
else
	if [ -f "$SYSINCLUDE/zlib.h" -o -f /usr/local/include/zlib.h ] ; then
		ZLIB=yes
	else
		ZLIB=no
	fi
	echo "$ZLIB (auto-detected)"
fi
if [ "$ZLIB" = yes ] ; then
	echo >>$c '#define PCI_COMPRESSED_IDS'
	echo >>$c '#define PCI_IDS "pci.ids.gz"'
	echo >>$m 'LIBZ=-lz'
	echo >>$m 'WITH_LIBS+=$(LIBZ)'
else
	echo >>$c '#define PCI_IDS "pci.ids"'
fi
echo >>$c "#define PCI_PATH_IDS_DIR \"$IDSDIR\""

echo_n "Checking for DNS support... "
if [ "$DNS" = yes -o "$DNS" = no ] ; then
	echo "$DNS (set manually)"
else
	if [ "$sys" != "windows" -a -f "$SYSINCLUDE/resolv.h" ] ; then
		DNS=yes
	else
		DNS=no
	fi
	echo "$DNS (auto-detected)"
fi
if [ "$DNS" = yes ] ; then
	echo >>$c "#define PCI_USE_DNS"
	echo >>$c "#define PCI_ID_DOMAIN \"pci.id.ucw.cz\""
	echo >>$m "WITH_LIBS+=$LIBRESOLV"
fi

if [ "$sys" = linux ] ; then
	echo_n "Checking for libkmod... "
	LIBKMOD_DETECTED=
	if [ -z "$PKG_CONFIG" ] ; then
		PKG_CONFIG=pkg-config
	fi
	if [ "$LIBKMOD" != no ] ; then
		if ! command -v $PKG_CONFIG >/dev/null ; then
			echo_n "($PKG_CONFIG not found) "
		elif $PKG_CONFIG libkmod ; then
			LIBKMOD_DETECTED=1
		fi
	fi
	if [ "$LIBKMOD" = yes -o "$LIBKMOD" = no ] ; then
		echo "$LIBKMOD (set manually)"
		if [ "$LIBKMOD" = yes -a -z "$LIBKMOD_DETECTED" ] ; then
			echo "Requested use of libkmod, but it is not available. Giving up."
			exit 1
		fi
	else
		if [ -n "$LIBKMOD_DETECTED" ] ; then
			LIBKMOD=yes
		else
			LIBKMOD=no
		fi
		echo "$LIBKMOD (auto-detected)"
	fi
	if [ "$LIBKMOD" = yes ] ; then
		echo >>$c "#define PCI_USE_LIBKMOD"
		echo >>$m "LIBKMOD_CFLAGS=$($PKG_CONFIG --cflags libkmod)"
		echo >>$m "LIBKMOD_LIBS=$($PKG_CONFIG --libs libkmod)"
	fi

	echo_n "Checking for udev hwdb support... "
	if [ "$HWDB" = yes -o "$HWDB" = no ] ; then
		echo "$HWDB (set manually)"
	else
		if `command -v $PKG_CONFIG >/dev/null && $PKG_CONFIG --atleast-version=196 libudev` ; then
			HWDB=yes
		else
			HWDB=no
		fi
		echo "$HWDB (auto-detected)"
	fi
	if [ "$HWDB" = yes ] ; then
		echo >>$c '#define PCI_HAVE_HWDB'
		echo >>$m 'LIBUDEV=-ludev'
		echo >>$m 'WITH_LIBS+=$(LIBUDEV)'
	fi
fi

echo "Checking whether to build a shared library... $SHARED (set manually)"
if [ "$SHARED" = no ] ; then
	echo >>$m 'PCILIB=$(LIBNAME).a'
	echo >>$m 'LDLIBS=$(WITH_LIBS)'
	echo >>$m 'LIB_LDLIBS='
else
	if [ "$LIBEXT" = so ]; then
		echo >>$m 'PCILIB=$(LIBNAME).$(LIBEXT).$(VERSION)'
	elif [ "$LIBEXT" = dll ]; then
		echo >>$m 'PCILIB=$(LIBNAME)$(ABI_VERSION).$(LIBEXT)'
	else
		echo >>$m 'PCILIB=$(LIBNAME).$(VERSION).$(LIBEXT)'
	fi
	# We link the dependencies _to_ the library, so we do not need explicit deps in .pc
	echo >>$m 'LDLIBS='
	echo >>$m 'LIB_LDLIBS=$(WITH_LIBS)'
	echo >>$c '#define PCI_SHARED_LIB'
	if [ "$LIBEXT" = so ]; then
		echo >>$m 'PCILIB_LDFLAGS+=-Wl,-soname,$(LIBNAME).$(LIBEXT).$(ABI_VERSION)'
		echo >>$m 'PCILIB_LDFLAGS+=-Wl,--version-script=libpci.ver'
	elif [ "$LIBEXT" = dylib ]; then
		echo >>$m 'PCILIB_LDFLAGS+=-Wl,-install_name,$(LIBDIR)/$(PCILIB)'
	elif [ "$LIBEXT" = dll ]; then
		echo >>$m 'PCIIMPDEF=$(LIBNAME)$(ABI_VERSION).def'
		# GCC's -fvisibility=hidden is broken for Windows targets, use -Wl,--exclude-all-symbols instead (supported since GNU LD 2.21)
		echo >>$m 'PCILIB_LDFLAGS+=-Wl,--exclude-all-symbols'
	fi
fi
echo >>$m 'PCILIBPC=$(LIBNAME).pc'

if [ "$SHARED" != no ] && [ "$LIBEXT" = dll ]; then
	echo >>$m 'PCIIMPLIB=$(PCILIB).a'
else
	echo >>$m 'PCIIMPLIB=$(PCILIB)'
fi

echo >>$c "#define PCILIB_VERSION \"$VERSION\""
echo >>$c "#define PCILIB_DATE_AMIGAOS \"`echo $DATE | sed 's/\(....\)-\(..\)-\(..\)/\3.\2.\1/'`\""
sed '/"/{s/^#define \([^ ]*\) "\(.*\)"$/\1=\2/;p;d;};s/^#define \(.*\)/\1=1/' <$c >>$m
