# !/bin/sh

# Shell script to make gretl2 win32 distribution

GNUPLOT_ZIPFILE=/home/ftp/pub/gretl/winbuild/gp400w32.zip
HOME_WIN32=/home/cottrell/stats/esl/win32/runtime
CROSSDIR=/opt/cross-tools/mingw32

RECODE=recode
THISDIR=`pwd`

# add gnuplot distribution
function process_gnuplot {
    unzip -X $GNUPLOT_ZIPFILE | grep gretl | egrep -v /$\|\.zip \
	| awk '{ print $NF }' >> MANIFEST
}

# Shouldn't have to make changes below here...

function add_file {
    if [ `basename $1` != "CVS" ] && [ ! -d $1 ] ; then
	cp -p $1 windist/$2
	echo $2
	echo $2 >> windist/MANIFEST
    fi
}

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

rm -f windist/MANIFEST
rm -rf windist/gretl windist/gnuplot

# make 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/scripts
mkdir -p windist/gretl/scripts/misc
mkdir -p windist/gretl/db
mkdir -p windist/gretl/user
mkdir -p windist/gretl/plugins
mkdir -p windist/gretl/plugins/data

for lang in de es eu fr it ja pl rw tr ; do
   mkdir -p windist/gretl/locale/${lang}/LC_MESSAGES
   mkdir -p windist/gretl/lib/locale/${lang}/LC_MESSAGES
done

mkdir -p windist/gretl/lib/gtk-2.0/2.4.0/engines
mkdir -p windist/gretl/lib/gtk-2.0/2.4.0/loaders
mkdir -p windist/gretl/lib/pango/1.4.0/modules
mkdir -p windist/gretl/etc/gtk-2.0
mkdir -p windist/gretl/etc/pango

# mkdir -p windist/gretl/share/themes/Default/gtk-2.0
# mkdir -p windist/gretl/share/themes/MS-Windows/gtk-2.0

mkdir -p windist/gretl/share/gtksourceview-1.0/language-specs

# put binaries in place
add_file gretlcli.exe gretl/gretlcli.exe
add_file gretlw32.exe gretl/gretlw32.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

# gtk runtime files
for f in `cat ${HOME_WIN32}/gtk-runtime.files` ; do
    instfile=`echo $f | sed s+^bin/++`
    echo "gretl/$instfile" >> windist/MANIFEST
    cp -ap ${CROSSDIR}/$f windist/gretl/$instfile
done
cd $THISDIR

# auxiliary dlls
for f in \
    libgmp-3.dll libxml2.dll readline.dll zlib1.dll libpng12.dll \
    libblas.dll libf2c.dll liblapack.dll libgtksourceview.dll \
    iconv.dll intl.dll
do
    add_file $HOME_WIN32/misc-dll/$f gretl/$f
done

# gretl lang files for gtksourceview
add_file ${TOPDIR}/gui2/gretl.lang \
  gretl/share/gtksourceview-1.0/language-specs/gretl.lang
add_file ${TOPDIR}/gui2/gnuplot.lang \
  gretl/share/gtksourceview-1.0/language-specs/gnuplot.lang

# help files, license, logo
add_file ${TOPDIR}/share/gretl.hlp gretl/gretl_hlp.txt
add_file ${TOPDIR}/share/gretlcli.hlp gretl/gretlcli_hlp.txt
for lang in es it ; do
   $RECODE l1..utf-8 < ${TOPDIR}/share/gretl.hlp.$lang \
       > ${TOPDIR}/share/gretl_hlp_${lang}.txt
   add_file ${TOPDIR}/share/gretl_hlp_${lang}.txt \
       gretl/gretl_hlp_${lang}.txt
   $RECODE l1..utf-8 < ${TOPDIR}/share/gretlcli.hlp.$lang \
       > ${TOPDIR}/share/gretlcli_hlp_${lang}.txt
   add_file ${TOPDIR}/share/gretlcli_hlp_${lang}.txt \
       gretl/gretlcli_hlp_${lang}.txt
done
add_file ${TOPDIR}/COPYING gretl/COPYING
add_file ${TOPDIR}/pixmaps/gretl-logo.xpm gretl/gretl-logo.xpm
add_file ${TOPDIR}/doc/manual.chm/gretl.chm gretl/gretl.chm
add_file ${TOPDIR}/doc/manual_it.chm/gretl_it.chm gretl/gretl_it.chm
add_file /home/cottrell/stats/esl/es/gretl_es.chm gretl/gretl_es.chm

# user readme file
add_file $HOME_WIN32/readme.user gretl/user/README.txt

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

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

# database files
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 

# translations (make sure they're up to date first)
make -C mo
for lang in de es eu fr it ja pl rw tr ; 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 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 gnuplot distribution
cd windist
process_gnuplot

# make Inno builder file
cat MANIFEST | ./make_iss.pl $VERSION | todos > gretl.iss 

# make Inno installer
echo "Building gretl installer executable..."
nice wine "c:/Program Files/Inno Setup 4/ISCC.exe" ./gretl.iss 2>errlog && \
mv Output/setup.exe Output/gretl_install.exe && \
ls -l Output/gretl_install.exe

touch dist.stamp
