#!/bin/sh
#
# libftd2xx1.1.12_prep.sh <PSNAME> <PLNAME> <PVERS> <PWD> <ARCH> <PREFIX> <EXEC_PREFIX> <BINDIR> <LIBDIR> <INCDIR> <SYSCONFDIR> <USE_LIBUSB1>

orig_dir=release
build_dir=build
pkgconfig_dir=pkgconfig
examples_dir=examples
lic_file=LICENSE.FTDI

if [ -z "$1" ] ; then
  echo "ERROR: missing PSNAME"
  exit 1
fi
PSNAME=$1
shift 1
echo "INFO: project short name (PSNAME) is: ${PSNAME}"

if [ -z "$1" ] ; then
  echo "ERROR: missing PLNAME"
  exit 1
fi
PLNAME=$1
shift 1
echo "INFO: project long name (PLNAME) is: ${PLNAME}"

if [ -z "$1" ] ; then
  echo "ERROR: missing PVERS"
  exit 1
fi
PVERS=$1
shift 1
echo "INFO: project version (PVERS) is: ${PVERS}"

if [ -z "$1" ] ; then
  echo "ERROR: missing PWD"
  exit 1
fi
WD=$1
shift 1
echo "INFO: project working directory (PWD) is: ${WD}"

if [ -z "$1" ] ; then
  echo "ERROR: missing ARCH"
  exit 1
fi
ARCH=$1
shift 1
echo "INFO: architecture (ARCH) is: ${ARCH}"

if [ -z "$1" ] ; then
  echo "ERROR: missing PREFIX"
  exit 1
fi
PREFIX=$1
shift 1
echo "INFO: install prefix (PREFIX) is: ${PREFIX}"

if [ -z "$1" ] ; then
  echo "ERROR: missing EXEC_PREFIX"
  exit 1
fi
EXEC_PREFIX=$1
shift 1
echo "INFO: execution prefix (EXEC_PREFIX) is: ${EXEC_PREFIX}"

if [ -z "$1" ] ; then
  echo "ERROR: missing BINDIR"
  exit 1
fi
BINDIR=$1
shift 1
echo "INFO: binary directory (BINDIR) is: ${BINDIR}"

if [ -z "$1" ] ; then
  echo "ERROR: missing LIBDIR"
  exit 1
fi
LIBDIR=$1
shift 1
echo "INFO: library directory (LIBDIR) is: ${LIBDIR}"

if [ -z "$1" ] ; then
  echo "ERROR: missing INCDIR"
  exit 1
fi
INCDIR=$1
shift 1
echo "INFO: include directory (INCDIR) is: ${INCDIR}"

if [ -z "$1" ] ; then
  echo "ERROR: missing SYSCONFDIR"
  exit 1
fi
SYSCONFDIR=$1
shift 1
echo "INFO: sysconfig directory (SYSCONFDIR) is: ${SYSCONFDIR}"

if [ -n "$1" ] ; then
  if [ "x$1" = "xyes" -o "x$1" = "xYes" -o "x$1" = "xYES" -o "x$1" = "x1" ] ; then
    USE_LIBUSB1="yes"
    echo "INFO: enable libusb-1.0 linking (system library)"
  else
    USE_LIBUSB1="no"
    echo "INFO: disable libusb-1.0 linking (private library)"
  fi
  shift 1
fi



echo "WORK: disintegrate directory: ${orig_dir}"
mv -vf ${orig_dir}/* .
rm -vrf ${orig_dir}

if [ "x${USE_LIBUSB1}" = "xyes" ]; then
  echo "WORK: clean-up build directory: ${build_dir}/${ARCH}"
  rm -vrf ${build_dir}/${ARCH}/libusb
  pkgconfig_reqires="libusb-1.0"
  libusb1_libs=$(pkg-config --libs ${pkgconfig_reqires})
else
  pkgconfig_reqires=""
  libusb1_libs=""
fi

echo "WORK: create make rules in: ${build_dir}/${ARCH}/Makefile"
cat >${build_dir}/${ARCH}/Makefile << EOF
CC = gcc
AR = ar
INSTALL = install -vp
LN = ln -vs
PREFIX = ${PREFIX}
EXEC_PREFIX = ${EXEC_PREFIX}
BINDIR = ${BINDIR}
LIBDIR = ${LIBDIR}
DESTDIR = 

RECURSIVE_MANIFEST = \$(shell find . -type f -print)
OBJS = \$(filter %.o,\$(RECURSIVE_MANIFEST))

all: libs

libs: ${PLNAME}.a ${PLNAME}.so

clean:
	@rm -vf ${PLNAME}.*

install: ${PLNAME}.a ${PLNAME}.so ${PLNAME}.so.1 ${PLNAME}.so.${PVERS}
	@\$(INSTALL) -d \$(DESTDIR)\$(LIBDIR)
	@\$(INSTALL) -m755 -D ${PLNAME}.so.${PVERS} \$(DESTDIR)\$(LIBDIR)
	@\$(LN) ${PLNAME}.so.${PVERS} \$(DESTDIR)\$(LIBDIR)/${PLNAME}.so.1
	@\$(LN) ${PLNAME}.so.${PVERS} \$(DESTDIR)\$(LIBDIR)/${PLNAME}.so
	@\$(INSTALL) -m644 -D ${PLNAME}.a \$(DESTDIR)\$(LIBDIR)

${PLNAME}.a: \$(OBJS)
	\$(AR) rcs \$@ \$^

${PLNAME}.so.${PVERS}: \$(OBJS)
	\$(CC) -shared -Wl,-soname,${PLNAME}.so.1 \
	  -pthread -ldl -lrt ${libusb1_libs} -o \$@ \$^

${PLNAME}.so: ${PLNAME}.so.1

${PLNAME}.so ${PLNAME}.so.1: ${PLNAME}.so.${PVERS}
	ln -s \$< \$@
EOF

echo "WORK: create make rules in: ${build_dir}/Makefile"
cat >${build_dir}/Makefile << EOF
libs all clean install:
	@\$(MAKE) -C ${ARCH} \$@
EOF

echo "WORK: fix all includes to: <ftd2xx/ftd2xx.h>"
#ln -vfs ${WD} ftd2xx
#ln -vfs . ftd2xx
mkdir -vp ${WD}/ftd2xx
cp -v *.h ${WD}/ftd2xx
find ${examples_dir} -type f -name "*.c" | xargs \
  sed -i -e 's|^.*include.*\(ftd2xx\.h\).*$|#include <ftd2xx/\1>|g'

echo "WORK: clean up directory: ${examples_dir}"
rm -vf ${examples_dir}/*.h
rm -vrf ${examples_dir}/W32

echo "WORK: create make rules in: ${examples_dir}/Rules.make"
cat >${examples_dir}/Rules.make << EOF
CC = gcc
PREFIX = ${PREFIX}
EXEC_PREFIX = ${EXEC_PREFIX}
BINDIR = ${BINDIR}
DESTDIR = 
FTD2XX_INCDIR = \$(shell pkg-config --cflags-only-I ${PLNAME})
FTD2XX_CFLAGS = \$(shell pkg-config --cflags ${PLNAME})
FTD2XX_LIBDIR = \$(shell pkg-config --libs-only-L ${PLNAME})
FTD2XX_LIBS = \$(shell pkg-config --libs ${PLNAME})
CFLAGS += -Wall -Wextra \$(FTD2XX_INCDIR) \$(FTD2XX_CFLAGS)
CFLAGS += \$(FTD2XX_LIBDIR) \$(FTD2XX_LIBS)

.DEFAULT_GOAL := all
install:
	install -p -m744 -D \$(APP) \$(DESTDIR)\$(BINDIR)/ftd2xx-\$(APP)
EOF

echo "WORK: expand install rule in: ${examples_dir}/Makefile"
cat >>${examples_dir}/Makefile << EOF
install:
	for n in \$(SUBDIRS); do \$(MAKE) -C \$\$n install || exit 1; done
EOF

echo "WORK: create pkg-config rules in: ${pkgconfig_dir}/${PLNAME}.pc"
mkdir -vp ${pkgconfig_dir}
cat >${pkgconfig_dir}/${PLNAME}.pc << EOF
prefix=${PREFIX}
exec_prefix=${EXEC_PREFIX}
libdir=${LIBDIR}
includedir=${INCDIR}

Name: ${PLNAME}
Description: Library to ease porting of D2XX Windows applications
Version: ${PVERS}

Requires: ${pkgconfig_reqires}
Libs: -L\${libdir} -l${PSNAME} -pthread -ldl -lrt
Cflags: -I\${includedir} -pthread
EOF

echo "WORK: create udev rules in: 99-${PLNAME}.rules"
cat >99-${PLNAME}.rules << EOF
# allow users to claim the device
# VID/PID see: http://www.ftdichip.com/Support/Documents/TechnicalNotes/TN_100_USB_VID-PID_Guidelines.pdf

# FT232BM/L/Q, FT245BM/L/Q, VNC1L with VDPS Firmware, VNC2 with FT232Slave
SUBSYSTEM=="usb", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6001", MODE="0664", GROUP="plugdev"

# FT2232C/D/L, FT2232HL/Q
SUBSYSTEM=="usb", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6010", MODE="0664", GROUP="plugdev"

# FT4232HL/Q
SUBSYSTEM=="usb", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6011", MODE="0664", GROUP="plugdev"

# FT232HL/Q
SUBSYSTEM=="usb", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6014", MODE="0664", GROUP="plugdev"
EOF

echo "WORK: create make rules in: Makefile"
cat >Makefile << EOF
INSTALL = install -vp
PREFIX = ${PREFIX}
EXEC_PREFIX = ${EXEC_PREFIX}
LIBDIR = ${LIBDIR}
INCDIR = ${INCDIR}
SYSCONFDIR = ${SYSCONFDIR}
DESTDIR = 

all: libs examples

libs:
	@\$(MAKE) -C ${build_dir} \$@

examples:
	@\$(MAKE) -C ${examples_dir} \$@

privexamples:
	@PKG_CONFIG_PATH=$PWD/pkgconfig \$(MAKE) -C ${examples_dir} \
	  FTD2XX_INCDIR="-I${WD}" FTD2XX_LIBDIR="-L${WD}/${build_dir}/${ARCH}" all

clean:
	@-\$(MAKE) -C ${build_dir} \$@
	@-\$(MAKE) -C ${examples_dir} \$@

install:
	@\$(MAKE) -C ${build_dir} \$@
	@\$(MAKE) -C ${examples_dir} \$@
	@\$(INSTALL) -d \$(DESTDIR)\$(LIBDIR)/pkgconfig
	@\$(INSTALL) -m644 -D ${pkgconfig_dir}/${PLNAME}.pc \$(DESTDIR)\$(LIBDIR)/pkgconfig
	@\$(INSTALL) -d \$(DESTDIR)\$(INCDIR)/ftd2xx
	@\$(INSTALL) -m644 -D *.h \$(DESTDIR)\$(INCDIR)/ftd2xx
	@\$(INSTALL) -d \$(DESTDIR)\$(SYSCONFDIR)/udev/rules.d
	@\$(INSTALL) -m644 -D 99-${PLNAME}.rules \$(DESTDIR)\$(SYSCONFDIR)/udev/rules.d
EOF

echo "WORK: create FTDI license file in: ${lic_file}"
cat >${lic_file} << EOF
Files: ${build_dir}/* ${examples_dir}/*
Copyright: © 2001-2011 Future Technology Devices International Limited
License:
   The FTDI D2XX for Linux license

   This software is provided by Future Technology Devices International
   Limited "as is" and any express or implied warranties, including,
   but not limited to, the implied warranties of merchantability and
   fitness for a particular purpose are disclaimed. In no event shall
   future technology devices international limited be liable for any
   direct, indirect, incidental, special, exemplary, or consequential
   damages (including, but not limited to, procurement of substitute
   goods or services; loss of use, data, or profits; or business
   interruption) however caused and on any theory of liability, whether
   in contract, strict liability, or tort (including negligence or
   otherwise) arising in any way out of the use of this software, even
   if advised of the possibility of such damage.

   FTDI drivers may be used only in conjunction with products based on
   FTDI parts.

   FTDI drivers may be distributed in any form as long as license
   information is not modified.

   If a custom vendor ID and/or product ID or description string are
   used, it is the responsibility of the product manufacturer to maintain
   any changes and subsequent WHQL re-certification as a result of making
   these changes.

The original text of the FTDI D2XX for Linux license can be found in
the Internet only at the FTDI D2XX driver download page:
(Wed, 03 Oct 2012 18:16:56 +0200)

   http://www.ftdichip.com/Drivers/D2XX.htm

-------------------------------------------------------------------------------

Files: libusb/* ${build_dir}/*/libusb/*
Copyright: © 2001 Johannes Erdfelt <johannes@erdfelt.com>
           © 2007-2009 Daniel Drake <dsd@gentoo.org>
           © 2008-2010 Nathan Hjelm <hjelmn@users.sourceforge.net>
License: LGPL-2.1+

On Linux systems, the complete text of the GNU General Public License
can be found in '/usr/share/common-licenses/LGPL-2.1'.
EOF



exit 0
