#!/bin/sh

# Shell script to make gretl win32 distribution

BUILDTYPE=$1

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

TOPDIR=".."
THISDIR=`pwd`

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

if [ "$BUILDTYPE" = "cross" ] ; then
  WINFILES=/home/cottrell/stats/esl/win32
  ADDONS=/home/cottrell/stats/gretl-addons
  HLPDIR=${TOPDIR}/share
  DOCDIR=${TOPDIR}/build/doc/tex
  SYSBIN=/opt/cross-tools/mingw32/bin
  INNO_CMD="wine c:/innodir/ISCC.exe"
else
  WINFILES=/mingw/winbuild
  ADDONS=${WINFILES}/gretl-addons
  HLPDIR=${WINFILES}/help
  DOCDIR=${WINFILES}/doc
  SYSBIN=c:/mingw/bin
  # C:/Program Files/Inno Setup 5/ISCC.exe
  INNO_CMD="c:/progra~1/innose~1/iscc.exe"
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\|\bfiles" | 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
echo "VERSION $VERSION" > windist/MANIFEST
echo "DATE `date`" >> windist/MANIFEST
rm -rf windist/gretl

# make gretl directory structure
mkdir -p windist
mkdir -p windist/gretl
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/functions
mkdir -p windist/gretl/functions/gig
mkdir -p windist/gretl/functions/ivpanel
mkdir -p windist/gretl/functions/SVAR
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

# put binaries in place
add_file gretlcli.exe gretl/gretlcli.exe
add_file gretl.exe gretl/gretl.exe

# 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

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

# auxiliary dlls (this did include freetype6.dll)
for f in \
    libgmp-3.dll libmpfr-1.dll readline5.dll history5.dll \
    libblas-3.dll liblapack-3.dll libfftw3-3.dll
do
    add_file ${WINFILES}/misc-dll/$f gretl/$f
done

# more auxiliary dlls
for f in \
    libxml2-2.dll libiconv-2.dll intl.dll libcurl-4.dll \
    libgtksourceview-2.0-0.dll libffi-6.dll libcairo-2.dll
do
    add_file ${SYSBIN}/$f gretl/$f
done

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

# 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 ${WINFILES}/gtksourceview-data/R.lang \
  gretl/gtksourceview/R.lang
add_file ${WINFILES}/gtksourceview-data/cpp.lang \
  gretl/gtksourceview/cpp.lang
add_file ${WINFILES}/gtksourceview-data/octave.lang \
  gretl/gtksourceview/octave.lang
add_file ${WINFILES}/gtksourceview-data/octave.lang \
  gretl/gtksourceview/python.lang
add_file ${WINFILES}/gtksourceview-data/def.lang \
  gretl/gtksourceview/def.lang
add_file ${WINFILES}/gtksourceview-data/classic.xml \
  gretl/gtksourceview/classic.xml
add_file ${WINFILES}/gtksourceview-data/language2.rng \
  gretl/gtksourceview/language2.rng
add_file ${WINFILES}/gtksourceview-data/styles.rng \
  gretl/gtksourceview/styles.rng

# 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
add_file ${DOCDIR}/gretl-guide.pdf gretl/doc/gretl-guide.pdf
add_file ${DOCDIR}/gretl-ref.pdf gretl/doc/gretl-ref.pdf

# 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

# 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
done

# ensure updater is au courant
make -C updater

# misc files
add_file ${TOPDIR}/plugin/data/urcdata.gz gretl/plugins/data/urcdata.gz
add_file ${TOPDIR}/plugin/data/dwdata.gz gretl/plugins/data/dwdata.gz
add_file gretl_website.url gretl/gretl_website.url
add_file updater/gretl_updater.exe gretl/gretl_updater.exe
date > gretl.stamp
add_file gretl.stamp gretl/gretl.stamp

# add GTK runtime and gnuplot distribution: note that we move
# into the "windist" subdir first
# RTFILE=gtk-runtime.tar.gz
RTFILE=gtk-2.24-runtime.tgz
cd windist
if [ ! -f ${WINFILES}/${RTFILE} ] ; then
  echo "Couldn't find ${RTFILE}"
  exit 1
else 
  process_tarball ${WINFILES}/${RTFILE}
fi

# additional GTK-related runtime files (includes intl.dll,
# libpng, zlib)
if [ -f ${WINFILES}/gtk-3rdparty.tgz ] ; then
  process_tarball ${WINFILES}/gtk-3rdparty.tgz
fi

if [ ! -f ${WINFILES}/gp463w32.tar.gz ] ; then
  echo "Couldn't find gp463w32.tar.gz"
  exit 1
else
  process_tarball ${WINFILES}/gp463w32.tar.gz
fi

# add the approved gretl addons
WINDIST=`pwd`
cd gretl/functions
for f in gig ivpanel SVAR ; 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

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

touch dist.stamp
