#!/bin/sh

# Shell script to install msys2/mingw64 build of gretl

ARCH=$1

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

MPI=yes

PREFIX="c:/users/cottrell"
TARGET="${PREFIX}/gretl"
TOPDIR="../gretl-git"

EXTRA=$HOME/gretl-extra
ADDONS=${EXTRA}/gretl-addons
HLPDIR=${EXTRA}/help
DOCDIR=${EXTRA}/doc
SYSBASE=/mingw64
# C:/Program Files/Inno Setup 5/ISCC.exe
INNO_CMD="c:/progra~1/innose~1/iscc.exe"

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

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

mkdir -p ${TARGET}/share/themes/Emacs/gtk-2.0-key
mkdir -p ${TARGET}/share/themes/Default/gtk-2.0-key
mkdir -p ${TARGET}/share/themes/MS-Windows/gtk-2.0
mkdir -p ${TARGET}/share/themes/Raleigh/gtk-2.0
mkdir -p ${TARGET}/share/themes/Clearlooks/gtk-2.0

mkdir -p ${TARGET}/lib/gtk-2.0/modules
mkdir -p ${TARGET}/lib/gtk-2.0/2.10.0/engines
mkdir -p ${TARGET}/etc/gtk-2.0

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

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

# home-made dynamic libs
for f in dlls/*.dll ; do
    cp $f ${TARGET}/`basename $f`
done

# gretl plugins
for f in plugins/*.dll ; do
    cp $f ${TARGET}/plugins/`basename $f`
done

# GTK and other basic dlls
for f in \
	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 \
	libintl-8.dll \
	libpango-1.0-0.dll \
	libpangocairo-1.0-0.dll \
	libpangowin32-1.0-0.dll \
	libpixman-1-0.dll \
	libxml2-2.dll \
	zlib1.dll \
	libpng16-16.dll \
	libreadline7.dll \
	libjson-glib-1.0-0.dll \
	libopenblas.dll \
	libwinpthread-1.dll \
	libtermcap-0.dll \
	libgomp-1.dll \
	libnghttp2-14.dll \
	libidn2-0.dll \
	libgcc_s_seh-1.dll \
	libssh2-1.dll \
	liblzma-5.dll \
	librtmp-1.dll \
	libpcre-1.dll \
	libunistring-2.dll \
	libgfortran-4.dll \
	libhogweed-4.dll \
	libgnutls-30.dll \
	libnettle-6.dll \
	libquadmath-0.dll \
	libidn-11.dll \
	libp11-kit-0.dll \
	libtasn1-6.dll \
	libfontconfig-1.dll \
	libfreetype-6.dll \
	libpangoft2-1.0-0.dll \
	libexpat-1.dll \
	libharfbuzz-0.dll \
	libbz2-1.dll \
	libgraphite2.dll \
	libstdc++-6.dll \
	libsvm.dll
do
    cp ${SYSBIN}/$f ${TARGET}/$f
done

# certs for cURL
cp ${EXTRA}/curl-ca-bundle.crt ${TARGET}/curl-ca-bundle.crt

# GTK theme files, standard
cp $SYSTHEME/Emacs/gtk-2.0-key/gtkrc ${TARGET}/share/themes/Emacs/gtk-2.0-key/gtkrc
cp $SYSTHEME/Default/gtk-2.0-key/gtkrc ${TARGET}/share/themes/Default/gtk-2.0-key/gtkrc
cp $SYSTHEME/Raleigh/gtk-2.0/gtkrc ${TARGET}/share/themes/Raleigh/gtk-2.0/gtkrc
cp $EXTRA/themes/Clearlooks/gtk-2.0/gtkrc ${TARGET}/share/themes/Clearlooks/gtk-2.0/gtkrc

# GTK aux. files, gretl-specific
cp $EXTRA/auxfiles/locale.alias ${TARGET}/share/locale/locale.alias
cp $EXTRA/auxfiles/gtkrc ${TARGET}/share/themes/MS-Windows/gtk-2.0/gtkrc

cp $SYSBASE/lib/gtk-2.0/modules/libgail.dll ${TARGET}/lib/gtk-2.0/modules/libgail.dll
cp $SYSBASE/lib/gtk-2.0/2.10.0/engines/libpixmap.dll ${TARGET}/lib/gtk-2.0/2.10.0/engines/libpixmap.dll
cp $SYSBASE/lib/gtk-2.0/2.10.0/engines/libwimp.dll ${TARGET}/lib/gtk-2.0/2.10.0/engines/libwimp.dll
cp $EXTRA/engines/libclearlooks.dll ${TARGET}/lib/gtk-2.0/2.10.0/engines/libclearlooks.dll
cp $SYSBASE/etc/gtk-2.0/im-multipress.conf ${TARGET}/etc/gtk-2.0/im-multipress.conf

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

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

# help files, license, logo
cp ${HLPDIR}/gretlhelp.refs ${TARGET}/gretlhelp.refs
for f in ${TOPDIR}/share/texfigs/*.png ; do
   cp $f ${TARGET}/helpfigs/`basename $f`
done
for lang in en es it pt gl ; do
    for f in gretl_gui_help gretl_gui_cmdref gretl_cli_cmdref \
       gretl_cli_fnref gretl_gui_fnref ; do
	if test -f ${HLPDIR}/${f}.${lang} ; then \
	    cp ${HLPDIR}/${f}.${lang} ${TARGET}/${f}.${lang}
	fi
    done
done

cp ${TOPDIR}/COPYING ${TARGET}/COPYING
cp ${TOPDIR}/pixmaps/gretl-logo.xpm ${TARGET}/gretl-logo.xpm

# libsvm copyright
cp ${EXTRA}/copyright_libsvm ${TARGET}/copyright_libsvm

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

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

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

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

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

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

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

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

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

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

# FRED database
make -C db
cp db/fedstl.bin ${TARGET}/db/fedstl.bin
cp ${TOPDIR}/share/bcih/fedstl.idx ${TARGET}/db/fedstl.idx 

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

# misc files
cp ${TOPDIR}/plugin/data/urcdata.bin ${TARGET}/plugins/data/urcdata.bin
cp ${TOPDIR}/plugin/data/dwdata.gz ${TARGET}/plugins/data/dwdata.gz
cp ${TOPDIR}/win32/gretl_website.url ${TARGET}/gretl_website.url

exit 0

# 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

