# !/bin/sh

# Shell script to make gretl win32 distribution

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

THISDIR=`pwd`

# add gnuplot distribution
function process_gnuplot {
    echo "adding content of $GNUPLOT_ZIPFILE" >> ../FILELIST
    unzip -l $GNUPLOT_ZIPFILE | grep gretl | egrep -v /$\|\.zip \
        | awk '{ print $1" "$2" "$3" "$4 }' >> MANIFEST
    unzip -X $GNUPLOT_ZIPFILE
}

# add GTK runtime
function process_gtk {
    echo "adding GTK runtime..."
    mkdir -p $HOME_WIN32/tmp
    cd $HOME_WIN32/tmp
    tar tvfz ../gtk-runtime.tgz | awk '{ print $3" "$4" "$5" "$6 }' > gtk.files
    tar xfz ../gtk-runtime.tgz
    for d in `cat gtk.files | awk '{ print $NF }' | grep /$` ; do
       echo "make dir: $d"
       mkdir -p $THISDIR/windist/gretl/$d
    done 
    save_ifs=$IFS
    IFS='
'
    for line in `cat gtk.files | grep -v /$` ; do
       fsize=`echo $line | awk '{ print $1 }'`
       fdate=`echo $line | awk '{ print $2 }'`
       ftime=`echo $line | awk '{ print $3 }'`
       fname=`echo $line | awk '{ print $NF }'`
       instfile=`echo $fname | sed s+^bin/++`
       echo "$fname -> gretl/$instfile" >> $THISDIR/FILELIST
       echo "$fsize $fdate $ftime gretl/$instfile" >> $THISDIR/windist/MANIFEST
       cp -ap $fname $THISDIR/windist/gretl/$instfile || exit 1
    done
    IFS=$save_ifs
    cd $THISDIR
    rm -rf $HOME_WIN32/tmp
}

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

function add_file {
    if [ `basename $1` != "CVS" ] && [ ! -d $1 ] ; then
        finfo=`ls -l $1 | awk '{ print $5" "$6" "$7 }'`
	cp -p $1 windist/$2
	echo $2
        echo "$1 -> $2" >> FILELIST
        echo "$finfo $2" >> windist/MANIFEST
    fi
}

TOPDIR=..
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 -f FILELIST
rm -rf windist/gretl windist/gnuplot

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

# 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

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

# GTK directories and runtime files
process_gtk

# auxiliary dlls
for f in \
    libgmp-3.dll libxml2.dll readline5.dll history5.dll zlib1.dll \
    libblas.dll liblapack.dll libgtksourceview.dll \
    iconv.dll intl.dll libfftw3-3.dll libpng13.dll
do
    add_file $HOME_WIN32/misc-dll/$f gretl/$f
done

# Uncomment if not using cairo in gnuplot
# add_file $HOME_WIN32/misc-dll/libpdf.dll gretl/$f

# gretl lang files for gtksourceview
mkdir -p windist/gretl/share/gtksourceview-1.0/language-specs
add_file 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
add_file ${TOPDIR}/gui2/R.lang \
  gretl/share/gtksourceview-1.0/language-specs/R.lang

# help files, license, logo
add_file ${TOPDIR}/share/gretlgui.hlp gretl/gretlgui_hlp.txt
add_file ${TOPDIR}/share/gretlcmd.hlp gretl/gretlcmd_hlp.txt
add_file ${TOPDIR}/share/gretlcli.hlp gretl/gretlcli_hlp.txt
add_file ${TOPDIR}/share/genrcli.hlp gretl/genrcli.hlp
add_file ${TOPDIR}/share/genrgui.hlp gretl/genrgui.hlp
add_file ${TOPDIR}/share/genrcli.hlp.it gretl/genrcli.hlp.it
add_file ${TOPDIR}/share/genrgui.hlp.it gretl/genrgui.hlp.it
for f in ${TOPDIR}/share/texfigs/*.png ; do
   add_file $f gretl/helpfigs/`basename $f`
done
for lang in es it ; do
   add_file ${TOPDIR}/share/gretlgui.hlp.${lang} \
       gretl/gretlgui_hlp_${lang}.txt
   add_file ${TOPDIR}/share/gretlcmd.hlp.${lang} \
       gretl/gretlcmd_hlp_${lang}.txt
   add_file ${TOPDIR}/share/gretlcli.hlp.${lang} \
       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}/build/doc/tex/gretl-guide.pdf gretl/doc/gretl-guide.pdf
add_file ${TOPDIR}/build/doc/tex/gretl-ref.pdf gretl/doc/gretl-ref.pdf

# 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 pt pt_BR ; 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 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 5/ISCC.exe" ./gretl.iss 2>errlog && \
mv Output/setup.exe Output/gretl_install.exe && \
ls -l Output/gretl_install.exe

touch dist.stamp
