#!/bin/sh

# Shell script to make gretl Windows installer

BUILDTYPE=$1
ARCH=$2

if [ "x${BUILDTYPE}" = "x" ] ; then
  echo "mkwindist: argument must be 'cross' or 'native'"
  exit 1
fi

if [ "x${ARCH}" = "x" ] ; then
  X64="no"
else
  X64="yes"
fi

TOPDIR=".."
THISDIR=`pwd`
MPI=yes
OPENBLAS=yes

########## CONFIGURE HERE ###################

if [ "$BUILDTYPE" = "cross" ] ; then
  WINFILES=/home/cottrell/stats/esl/win32
  ADDONS=/home/cottrell/src/build/addons
  HLPDIR=${TOPDIR}/share
  DOCDIR=${TOPDIR}/build/doc/tex
  if [ $X64 = "yes" ] ; then
    SYSBASE=/opt/win64/x86_64-w64-mingw32
  else
    SYSBASE=/opt/win32/i686-pc-mingw32
  fi
  INNO_CMD="wine c:/innodir/ISCC.exe"
else
  WINFILES=/mingw/winbuild
  ADDONS=${WINFILES}/gretl-addons
  HLPDIR=${WINFILES}/help
  DOCDIR=${WINFILES}/doc
  SYSBASE=c:/mingw
  # C:/Program Files/Inno Setup 5/ISCC.exe
  INNO_CMD="c:/progra~1/innose~1/iscc.exe"
fi

SYSBIN=${SYSBASE}/bin
SYSTRANS=${SYSBASE}/share/locale
SYSTHEME=${SYSBASE}/share/themes
SYSSV=${SYSBASE}/share/gtksourceview-2.0
if [ $X64 = "yes" ] ; then
  GPDIST=${WINFILES}/gp51w64.tar.gz
else
  GPDIST=${WINFILES}/gp51w32.tar.gz
fi

#############################################

# function: add content of unexploded tar package
# the single argument is the path to the tarball
function process_tarball {
    echo "adding content of $1"
    tar tvfz $1 | awk '{ print $3" "$4" "$5" "$6 }' | \
       grep -v ^0 >> MANIFEST
    tar xfz $1
}

# function: add content of an addon zip file
# the single argument is the path to the file
function process_addon {
    echo "adding content of $1"
    unzip -l $1 | awk '{ print $1" 20"$2" "$3" gretl/functions/"$4 }' | \
     grep -ve "^0\|^Arc\|^Len\|\bfile" | grep -v ^'-' >> ../../MANIFEST
    unzip $1
}

# function: add individual file to package
# arg1 is path to file to add, arg2 is where to put it
function add_file {
    finfo=`ls -l $1 | awk '{ print $5" "$6" "$7 }'`
    cp -p $1 windist/$2
    echo $2
    echo "$finfo $2" >> windist/MANIFEST
}

# start of script proper

VERSION=$(grep GRETL_VERSION[^_] $TOPDIR/lib/src/version.h | \
     awk '{ print $NF}' | sed s/[\"\;]//g)

if [ -f windist/MANIFEST ] ; then
   cp windist/MANIFEST windist/MANIFEST.`date +%Y-%m-%d`
fi
if [ $X64 = "yes" ] ; then
  echo "VERSION $VERSION x64" > windist/MANIFEST
else
  echo "VERSION $VERSION" > windist/MANIFEST
fi
echo "DATE `date`" >> windist/MANIFEST
rm -rf windist/gretl

# make gretl directory structure
mkdir -p windist/gretl/data
mkdir -p windist/gretl/data/misc
mkdir -p windist/gretl/data/greene
mkdir -p windist/gretl/data/nist
mkdir -p windist/gretl/data/plotbars
mkdir -p windist/gretl/data/gnuplot
mkdir -p windist/gretl/functions
mkdir -p windist/gretl/functions/gig
mkdir -p windist/gretl/functions/ivpanel
mkdir -p windist/gretl/functions/SVAR
mkdir -p windist/gretl/functions/HIP
mkdir -p windist/gretl/scripts
mkdir -p windist/gretl/scripts/misc
mkdir -p windist/gretl/db
mkdir -p windist/gretl/doc
mkdir -p windist/gretl/helpfigs
mkdir -p windist/gretl/plugins
mkdir -p windist/gretl/plugins/data
mkdir -p windist/gretl/ui

mkdir -p windist/gretl/share/themes/Emacs/gtk-2.0-key
mkdir -p windist/gretl/share/themes/Default/gtk-2.0-key
mkdir -p windist/gretl/share/themes/MS-Windows/gtk-2.0
mkdir -p windist/gretl/share/themes/Raleigh/gtk-2.0
mkdir -p windist/gretl/share/themes/Clearlooks/gtk-2.0

mkdir -p windist/gretl/lib/gtk-2.0/modules
mkdir -p windist/gretl/lib/gtk-2.0/2.10.0/engines
mkdir -p windist/gretl/etc/gtk-2.0

# gretl and GTK translations
LANGS=`cat ${TOPDIR}/po/LINGUAS | grep -v ^#`
for lang in $LANGS ; do
  mkdir -p windist/gretl/locale/${lang}/LC_MESSAGES
  mkdir -p windist/gretl/share/locale/$lang/LC_MESSAGES
done

# put binaries in place
add_file gretlcli.exe gretl/gretlcli.exe
add_file gretl.exe gretl/gretl.exe
if [ $MPI = "yes" ] ; then
  add_file gretlmpi.exe gretl/gretlmpi.exe
fi

# home-made dynamic libs
for f in dlls/*.dll ; do
    add_file $f gretl/`basename $f`
done

# gretl plugins
for f in plugins/*.dll ; do
    add_file $f gretl/plugins/`basename $f`
done

# GTK and other basic dlls
for f in \
	intl.dll \
	libatk-1.0-0.dll \
	libcairo-2.dll \
	libcairo-gobject-2.dll \
	libcurl-4.dll \
	ssleay32.dll \
	libeay32.dll \
	libffi-6.dll \
	libfftw3-3.dll \
	libgailutil-18.dll \
	libgdk_pixbuf-2.0-0.dll \
	libgdk-win32-2.0-0.dll \
	libgio-2.0-0.dll \
	libglib-2.0-0.dll \
	libgmodule-2.0-0.dll \
	libgmp-10.dll \
	libgobject-2.0-0.dll \
	libgthread-2.0-0.dll \
	libgtksourceview-2.0-0.dll \
	libgtk-win32-2.0-0.dll \
	libiconv-2.dll \
	libmpfr-4.dll \
	libpango-1.0-0.dll \
	libpangocairo-1.0-0.dll \
	libpangowin32-1.0-0.dll \
	libxml2-2.dll \
	zlib1.dll \
	libpng16-16.dll \
	libreadline6.dll \
	libjson-glib-1.0-0.dll
do
    add_file ${SYSBIN}/$f gretl/$f
done

# extra dll for 64-bit build (statically linked for w32)
if [ $X64 = "yes" ] ; then
  add_file ${SYSBIN}/libpixman-1-0.dll gretl/libpixman-1-0.dll
fi

if [ $OPENBLAS = "yes" ] ; then
  add_file ${SYSBIN}/libopenblas.dll gretl/libopenblas.dll
else
  add_file ${SYSBIN}/libblas-3.dll gretl/libblas-3.dll
  add_file ${SYSBIN}/liblapack-3.dll gretl/liblapack-3.dll
fi

# system DLLs for threads/openmp, if required
# add_file ${SYSBIN}/mingwm10.dll gretl/mingwm10.dll
add_file ${SYSBIN}/pthreadGC2.dll gretl/pthreadGC2.dll

# cert for cURL
add_file ${WINFILES}/curl-ca-bundle.crt gretl/curl-ca-bundle.crt

# GTK theme files, standard
add_file $SYSTHEME/Emacs/gtk-2.0-key/gtkrc gretl/share/themes/Emacs/gtk-2.0-key/gtkrc
add_file $SYSTHEME/Default/gtk-2.0-key/gtkrc gretl/share/themes/Default/gtk-2.0-key/gtkrc
add_file $SYSTHEME/Raleigh/gtk-2.0/gtkrc gretl/share/themes/Raleigh/gtk-2.0/gtkrc
add_file $SYSTHEME/Clearlooks/gtk-2.0/gtkrc gretl/share/themes/Clearlooks/gtk-2.0/gtkrc

# GTK aux. files, gretl-specific
add_file $WINFILES/auxfiles/locale.alias gretl/share/locale/locale.alias
add_file $WINFILES/auxfiles/gtkrc gretl/share/themes/MS-Windows/gtk-2.0/gtkrc

add_file $SYSBASE/lib/gtk-2.0/modules/libgail.dll gretl/lib/gtk-2.0/modules/libgail.dll
add_file $SYSBASE/lib/gtk-2.0/2.10.0/engines/libpixmap.dll gretl/lib/gtk-2.0/2.10.0/engines/libpixmap.dll
add_file $SYSBASE/lib/gtk-2.0/2.10.0/engines/libwimp.dll gretl/lib/gtk-2.0/2.10.0/engines/libwimp.dll
add_file $SYSBASE/lib/gtk-2.0/2.10.0/engines/libclearlooks.dll gretl/lib/gtk-2.0/2.10.0/engines/libclearlooks.dll
add_file $SYSBASE/etc/gtk-2.0/im-multipress.conf gretl/etc/gtk-2.0/im-multipress.conf

# XML UI-definition files
for f in ${TOPDIR}/gui2/*.xml ; do
  add_file $f gretl/ui/`basename $f`
done

# data files for gtksourceview
mkdir -p windist/gretl/gtksourceview
add_file gretl.lang gretl/gtksourceview/gretl.lang
add_file ${TOPDIR}/gui2/gnuplot.lang gretl/gtksourceview/gnuplot.lang
add_file ${TOPDIR}/gui2/stata.lang gretl/gtksourceview/stata.lang
add_file ${TOPDIR}/gui2/gfnspec.lang gretl/gtksourceview/gfnspec.lang
add_file ${TOPDIR}/gui2/julia_sv2.lang gretl/gtksourceview/julia.lang
add_file ${SYSSV}/language-specs/R.lang gretl/gtksourceview/R.lang
add_file ${SYSSV}/language-specs/cpp.lang gretl/gtksourceview/cpp.lang
add_file ${SYSSV}/language-specs/octave.lang gretl/gtksourceview/octave.lang
add_file ${SYSSV}/language-specs/python.lang gretl/gtksourceview/python.lang
add_file ${SYSSV}/language-specs/def.lang gretl/gtksourceview/def.lang
add_file ${SYSSV}/language-specs/c.lang gretl/gtksourceview/c.lang
add_file ${SYSSV}/language-specs/gtk-doc.lang gretl/gtksourceview/gtk-doc.lang
add_file ${SYSSV}/language-specs/language2.rng gretl/gtksourceview/language2.rng
add_file ${SYSSV}/styles/classic.xml gretl/gtksourceview/classic.xml
add_file ${SYSSV}/styles/cobalt.xml gretl/gtksourceview/cobalt.xml
add_file ${SYSSV}/styles/kate.xml gretl/gtksourceview/kate.xml
add_file ${SYSSV}/styles/oblivion.xml gretl/gtksourceview/oblivion.xml
add_file ${SYSSV}/styles/tango.xml gretl/gtksourceview/tango.xml
add_file ${SYSSV}/styles/styles.rng gretl/gtksourceview/styles.rng
add_file ${TOPDIR}/share/styles/build.xml gretl/gtksourceview/build.xml
add_file ${TOPDIR}/share/styles/darkslate.xml gretl/gtksourceview/darkslate.xml
add_file ${TOPDIR}/share/styles/espresso.xml gretl/gtksourceview/espresso.xml
  
# release news file
add_file NEWS gretl/NEWS

# help files, license, logo
add_file ${HLPDIR}/gretlgui.hlp gretl/gretlgui_hlp.txt
add_file ${HLPDIR}/gretlcmd.hlp gretl/gretlcmd_hlp.txt
add_file ${HLPDIR}/gretlcli.hlp gretl/gretlcli_hlp.txt
add_file ${HLPDIR}/genrcli.hlp gretl/genrcli.hlp
add_file ${HLPDIR}/genrgui.hlp gretl/genrgui.hlp
add_file ${HLPDIR}/genrcli.hlp.it gretl/genrcli.hlp.it
add_file ${HLPDIR}/genrgui.hlp.it gretl/genrgui.hlp.it
add_file ${HLPDIR}/gretlhelp.refs gretl/gretlhelp.refs
for f in ${TOPDIR}/share/texfigs/*.png ; do
   add_file $f gretl/helpfigs/`basename $f`
done
for lang in es it pt ; do
  if test -f ${HLPDIR}/gretlgui.hlp.${lang} ; then \
     add_file ${HLPDIR}/gretlgui.hlp.${lang} gretl/gretlgui_hlp_${lang}.txt
  fi
  if test -f ${HLPDIR}/gretlcmd.hlp.${lang} ; then \
     add_file ${HLPDIR}/gretlcmd.hlp.${lang} gretl/gretlcmd_hlp_${lang}.txt
  fi
  if test -f ${HLPDIR}/gretlcli.hlp.${lang} ; then \
     add_file ${HLPDIR}/gretlcli.hlp.${lang} gretl/gretlcli_hlp_${lang}.txt
  fi
done
add_file ${TOPDIR}/COPYING gretl/COPYING
add_file ${TOPDIR}/pixmaps/gretl-logo.xpm gretl/gretl-logo.xpm

# PDF docs
for f in gretl-guide gretl-ref hansl-primer pkgbook gretl-keys ; do
    add_file ${DOCDIR}/$f.pdf gretl/doc/$f.pdf
done

# top-level data files
for f in ${TOPDIR}/share/data/*.gdt ; do
    add_file $f gretl/data/`basename $f`
done
for f in ${TOPDIR}/share/data/*.dtd ; do
    add_file $f gretl/data/`basename $f`
done
for f in ${TOPDIR}/share/data/descriptions ; do
    add_file $f gretl/data/`basename $f`
done

# Greene data files
for f in ${TOPDIR}/share/data/greene/*.gdt ; do
    add_file $f gretl/data/greene/`basename $f`
done
for f in ${TOPDIR}/share/data/greene/wg_descriptions ; do
    add_file $f gretl/data/greene/`basename $f`
done

# "misc" data files
for f in ${TOPDIR}/share/data/misc/*.gdt ; do
    add_file $f gretl/data/misc/`basename $f`
done
for f in ${TOPDIR}/share/data/misc/descriptions ; do
    add_file $f gretl/data/misc/`basename $f`
done

# Plot bars data files
for f in ${TOPDIR}/share/data/plotbars/*.txt ; do
    add_file $f gretl/data/plotbars/`basename $f`
done

# gnuplot colorname data
for f in ${TOPDIR}/share/data/gnuplot/*.txt ; do
    add_file $f gretl/data/gnuplot/`basename $f`
done

# NIST test data files
for f in ${TOPDIR}/tests/*.dat ; do
    add_file $f gretl/data/nist/`basename $f`
done

# top-level scripts
for f in ${TOPDIR}/share/scripts/*.inp ; do
    add_file $f gretl/scripts/`basename $f`
done
for f in ${TOPDIR}/share/scripts/*tions ; do
    add_file $f gretl/scripts/`basename $f`
done

# "misc" scripts
for f in ${TOPDIR}/share/scripts/misc/*.inp ; do
    add_file $f gretl/scripts/misc/`basename $f`
done
for f in ${TOPDIR}/share/scripts/misc/ps_descriptions ; do
    add_file $f gretl/scripts/misc/`basename $f`
done

# function package DTD and sample(s)
for f in ${TOPDIR}/share/functions/*.dtd ; do
    add_file $f gretl/functions/`basename $f`
done
for f in ${TOPDIR}/share/functions/*.gfn ; do
    add_file $f gretl/functions/`basename $f`
done
for f in ${TOPDIR}/share/functions/*.xml ; do
    add_file $f gretl/functions/`basename $f`
done

# database files
if [ "$BUILDTYPE" = "cross" ] ; then
  make -C ${TOPDIR}/build/share/bcih
  add_file ${TOPDIR}/build/share/bcih/fedstl.bin gretl/db/fedstl.bin
  add_file ${TOPDIR}/share/bcih/fedstl.idx gretl/db/fedstl.idx 
else
  make -C db
  add_file db/fedstl.bin gretl/db/fedstl.bin
  add_file ${TOPDIR}/share/bcih/fedstl.idx gretl/db/fedstl.idx 
fi

# translations (make sure they're up to date first)
make -C mo
for lang in $LANGS ; do
  add_file mo/$lang.mo gretl/locale/$lang/LC_MESSAGES/gretl.mo
  add_file $SYSTRANS/$lang/LC_MESSAGES/glib20.mo gretl/share/locale/$lang/LC_MESSAGES/glib20.mo
  add_file $SYSTRANS/$lang/LC_MESSAGES/gtk20.mo gretl/share/locale/$lang/LC_MESSAGES/gtk20.mo
done

# misc files
add_file ${TOPDIR}/plugin/data/urcdata.bin gretl/plugins/data/urcdata.bin
add_file ${TOPDIR}/plugin/data/dwdata.gz gretl/plugins/data/dwdata.gz
add_file gretl_website.url gretl/gretl_website.url
add_file ${TOPDIR}/win32/delold.bat gretl/delold.bat

# cd into distribution subdirectory
cd windist
WINDIST=`pwd`

if [ ! -f ${GPDIST} ] ; then
  echo "Couldn't find ${GPDIST}"
  exit 1
else
  process_tarball ${GPDIST}
fi

# add the approved gretl addons
cd gretl/functions
for f in gig ivpanel SVAR HIP ; do
  if [ ! -f ${ADDONS}/${f}/${f}.zip ] ; then
    echo "Couldn't find ${f}.zip"
    exit 1
  else
    process_addon ${ADDONS}/$f/$f.zip
  fi
done

cd $WINDIST

make gretl.iss

if [ $X64 = "yes" ] ; then
  EXENAME="gretl_install-64.exe"
else
  EXENAME="gretl_install.exe" 
fi

# make Inno installer
echo "Building gretl installer executable..."
${INNO_CMD} ./gretl.iss 2>errlog && \
mv Output/setup.exe Output/${EXENAME} && \
ls -l Output/${EXENAME}

touch dist.stamp
