#!/bin/sh -x
GC=7.6.2

MACHINE=`uname -m`
if [ $MACHINE == i686 ]; then
BINDIR=i386-windows
else
BINDIR=x86_64-windows
fi
SHARED=~/shared/asy
MAKEFLAGS=-j8
export MAKEFLAGS
VERSION=$1
if [ -z $VERSION ]; then
VERSION=`grep AC_INIT configure.ac | cut -s -d[ -f3 | cut -s -d] -f1`
if [ -z $VERSION ]; then
echo Usage: $0 VERSION
exit
fi
else
echo Building asymptote-$VERSION
cd ~/
rm -rf asymptote-$VERSION
mkdir -p CTAN
rm -rf CTAN/asymptote
cd CTAN
tar -vzxf $SHARED/asymptote-$VERSION.src.tgz | grep "/\." | xargs rm -rf
mv asymptote-$VERSION asymptote
cd ..
tar -zxf $SHARED/asymptote-$VERSION.src.tgz
mkdir -p CTAN/asymptote/binaries/$BINDIR/texlive
cd asymptote-$VERSION
fi
BUILD=build-$VERSION
ln -sf ../gc-$GC.tar.gz .
ln -sf ../gc-$GC .
make -n MSDOS=1 >& /dev/null || ./configure OSTYPE=msdos --enable-gc=$GC --disable-lsp --with-latex=/usr/local/share --with-context=/usr/local/share
rm -rf $BUILD
mkdir -p $BUILD
cp -a $HOME/dll/cygwin/*.dll $BUILD
cp /etc/pki/tls/certs/ca-bundle.crt $BUILD
chmod 0644 $BUILD/*.dll
cp -a $BUILD/*.dll ~/CTAN/asymptote/binaries/$BINDIR/
make MSDOS=1 DESTDIR="$BUILD/" docdir="$BUILD/"
test -f asy.list || make MSDOS=1 keywords DESTDIR="$BUILD/" docdir="$BUILD/"
make MSDOS=1 install-asy DESTDIR="$BUILD/" docdir="$BUILD/"
cp asy.ico README LICENSE* $BUILD
pushd $BUILD
mv usr/local/bin/* .
mv usr/local/share/asymptote/GUI .
rm -rf usr/local/share/asymptote/GUI
mv usr/local/share/asymptote/* .
cp $SHARED/asydoc/*.pdf .
rm -rf bin share doc usr
popd
echo \!define PRODUCT_VERSION \"$VERSION\" > AsymptoteInstallInfo.nsi
# generate uninstallation commands
ls -p1 $BUILD \
    | awk '{ if(index($0,"/")==length($0)) print "RMDir /r $INSTDIR\\" substr($0,0,length($0)-1); else print "Delete $INSTDIR\\" $0 }' \
	> AsymptoteUninstallList.nsi

rm $BUILD/xasy
if [ $MACHINE == i686 ]; then
sed asymptote.nsi -e 's/$PROGRAMFILES64/$PROGRAMFILES/g' > asymptote32.nsi
/cygdrive/c/Program\ Files\ \(x86\)/NSIS/makensis.exe asymptote32.nsi
SUFFIX=-i386_32
else
/cygdrive/c/Program\ Files\ \(x86\)/NSIS/makensis.exe asymptote.nsi
SUFFIX=
fi
cp -a --no-preserve=mode asymptote-$VERSION-setup.exe $SHARED/asymptote-$VERSION-setup$SUFFIX.exe
mv asymptote-$VERSION-setup.exe ~/asymptote-$VERSION-setup$SUFFIX.exe

cd ~/
rm -rf asymptote-${VERSION}TL || exit
cp -a asymptote-$VERSION asymptote-${VERSION}TL
sed asymptote-$VERSION/config.h -e 's/ASYMPTOTE_SYSDIR ".*"/ASYMPTOTE_SYSDIR ""/g' > asymptote-${VERSION}TL/config.h
#sed asymptote-$VERSION/configure.ac -e 's/\-std=c++11//g' > asymptote-${VERSION}TL/configure.ac
touch -r asymptote-$VERSION/config.h asymptote-${VERSION}TL/config.h
touch -r asymptote-$VERSION/configure.ac asymptote-${VERSION}TL/configure.ac
cd asymptote-${VERSION}TL
rm settings.o
rm -f doc/*.aux doc/*.log doc/*.dvi
make MSDOS=1 DESTDIR="$BUILD/"
make MSDOS=1 install-asy DESTDIR="$BUILD/"
mv $BUILD/usr/local/bin/asy.exe $BUILD
cp $BUILD/asy.exe ~/CTAN/asymptote/binaries/$BINDIR/texlive
cp -a configure.ac ~/CTAN/asymptote/
cp -a $SHARED/asydoc/* ~/CTAN/asymptote/doc
cd ~/CTAN/asymptote/binaries/$BINDIR
cp -a ../../README .
cd ~/CTAN
cp -a ~/asymptote/build-scripts/README-binaries asymptote/binaries/$BINDIR/texlive/README
SHAREDBIN=$SHARED/CTAN/asymptote
if [ $MACHINE == i686 ]; then
mkdir -p $SHAREDBIN
cp -a asymptote/binaries/$BINDIR $SHAREDBIN
else
cp -a $SHAREDBIN/i386-windows ~/CTAN/asymptote/binaries
fi
tar cfz asymptote-$VERSION-CTAN.tgz asymptote
cp -a --no-preserve=mode asymptote-$VERSION-CTAN.tgz $SHARED
