#!/bin/bash

# configure prepares Makefile for wminfo 4.2.1
# Copyright (C) 2011-2015 Cezary M. Kruk <c.kruk@bigfoot.com>

version=4.2.1

if [ "$1" == "-h" -o "$1" == "--help" ]
then
cat << EOF
\`configure' configures wminfo $version.

Usage: ./configure [OPTION]

Configuration:
  -h, --help              displays this help and exits

Installation directory:
  --prefix=PREFIX         installs architecture-independent files in PREFIX
                          default is: /usr/local
                          allowed is: /usr
EOF
exit
else
if [ "`echo $1 | grep '\-\-prefix='`" != "" ]
then
    PREFIX=`echo $1 | sed 's/--prefix=//'`
fi
if [ "$PREFIX" == "" ]
then
    PREFIX=/usr/local
fi

FILE=Makefile
> $FILE

cat >> $FILE <<EOF
PREFIX=$PREFIX
PACKAGE=wminfo-$version

BINDIR=\$(PREFIX)/bin
DOCDIR=\$(PREFIX)/doc/\$(PACKAGE)
MANDIR=\$(PREFIX)/man/man1

LIBDIR = -L/usr/X11R6/lib
LIBS   = -lXpm -lXext -lX11
CFLAGS = -march=native -O2 -pipe
OBJS   = wminfo.o ../wmgeneral/wmgeneral.o ../wmgeneral/misc.o ../wmgeneral/list.o

.c.o:
	gcc -I/usr/X11R6/share/include \$(CFLAGS) -c -Wall \$< -o \$*.o

all: \$(OBJS)
	gcc \$(CFLAGS) -o wminfo \$^ -lXext \$(LIBDIR) \$(LIBS) && strip --strip-unneeded wminfo
	gcc regex.c \$(CFLAGS) -Wall -o regex && strip --strip-unneeded regex

	for f in ../plugins.binary/*.c ; do gcc \$\$f \$(CFLAGS) -Wall -o \$\${f%c}bin && strip --strip-unneeded \$\${f%c}bin ; done

CONTRIB = accuweather conky-parser counter default.wmi dos-unix bash-empty.wmi forecast format \\
	html-iso1 html-iso2 html-iso5 iso1-utf8 iso2-utf8 iso5-utf8 macos-unix netmon plugin \\
	print-letter print-number print-number-padded punctuation run-all-wminfo-plugins suspend \\
	sysmon time+date timestring traffic utf8-iso1 utf8-iso2 utf8-iso5 weather- weather-poland \\
	wminfo-benchmark wminfo-conky

DOCS    = BUGS COPYING ChangeLog INSTALL Plugins-HOWTO README README.1st README.CPU.usage \\
	README.colors README.benchmarks README.multi+wrapper README.regex README.plugin.rerun \\
	THANKS TODO

DIRS    = plugins.binary plugins.conky plugins.offline plugins.online plugins.system-1 plugins.system-2 samples

install:
	cp -p wminfo \$(BINDIR)
	chmod 755 \$(BINDIR)/wminfo
	chown root:root \$(BINDIR)/wminfo

	cp -p regex \$(BINDIR)
	chmod 755 \$(BINDIR)/regex
	chown root:root \$(BINDIR)/regex

	for f in \$(CONTRIB) \\
	; do cp -p ../contrib/\$\$f \$(BINDIR) ; done

	if [ ! -d \$(MANDIR) ]; then mkdir -p \$(MANDIR); fi

	cp -p ../man/wminfo.1 \$(MANDIR)

	if [ ! -d \$(DOCDIR) ]; then mkdir -p \$(DOCDIR); fi

	for f in \$(DOCS) \\
	; do cp -p ../\$\$f \$(DOCDIR) ; done

	for d in contrib \$(DIRS) \\
	; do if [ ! -d \$(DOCDIR)/\$\$d ]; then mkdir \$(DOCDIR)/\$\$d; fi ; \\
	cp -p ../\$\$d/* \$(DOCDIR)/\$\$d ; done

	for d in \$(DIRS) \\
	; do if [ ! -x \$(DOCDIR)/\$\$d/run-all-wminfo-plugins ]; then ln -s \$(BINDIR)/run-all-wminfo-plugins \$(DOCDIR)/\$\$d/run-all-wminfo-plugins; fi ; done

	if [ ! -x \$(BINDIR)/\$\$d/empty.wmi ]; then ln -s \$(BINDIR)/bash-empty.wmi \$(BINDIR)/\$\$d/empty.wmi; fi

	for f in ../plugins.binary/*.bin ; do chmod 755 \$\$f ; chown root:root \$\$f ; cp -p \$\$f \$(BINDIR)/ ; done

uninstall:
	for f in wminfo regex empty.wmi \$(CONTRIB) \\
	; do rm -f \$(BINDIR)/\$\$f ; done

	rm -f \$(MANDIR)/wminfo.1

	for d in contrib \$(DIRS) \\
	; do rm -f \$(DOCDIR)/\$\$d/* ; \\
	if [ -d \$(DOCDIR)/\$\$d ]; then rmdir \$(DOCDIR)/\$\$d; fi ; done

	rm -f \$(DOCDIR)/*

	if [ -d \$(DOCDIR) ]; then rmdir \$(DOCDIR); fi

	for f in \$(BINDIR)/*.bin ; do rm -f \$\$f ; done

clean:
	for i in \$(OBJS) ; do rm -f \$\$i ; done
	rm -f wminfo
	rm -f regex
	rm -f *~
	rm -f core
	rm -f Makefile

	for f in ../plugins.binary/*.bin ; do rm \$\$f ; done

EOF
fi

