#!/bin/tcsh -f

if(! -f configure) then
  echo "run this script from the root directory of CVS working directory"
  echo "which is where the 'conigure' script resides"
  exit 1
endif

if( "$1" == "") then
  echo "usage : $0 <version> <win-flavor> <name extension> <options to configure ...>"
exit
endif

set version=$1
set winflavor=$2
set darflavor=$3

if (! $?LDFLAGS) then
  setenv LDFLAGS "-L/usr/local/lib"
else
  setenv LDFLAGS "${LDFLAGS} -L/usr/local/lib"
endif

if (! $?LD_LIBRARY_PATH) then
  setenv LD_LIBRARY_PATH "/usr/local/lib"
else
  setenv LD_LIBRARY_PATH "${LD_LIBRARY_PATH}:/usr/local/lib"
endif

if(! $?CXXFLAGS) then
  setenv CXXFLAGS "-I/usr/local/include"
else
  setenv CXXFLAGS "${CXXFLAGS} -I/usr/local/include"
endif

set dir_name="dar$darflavor-$version-$winflavor"
set build_dir=`pwd`/$dir_name
echo "install directory is $build_dir"
if( -e $build_dir) then
   echo "cannot create $build_dir file exists"
   exit 1
endif
mkdir "$build_dir"
make clean distclean || echo "ignoring error, tree already clean"
# setenv CXXFLAGS -O
./configure --prefix=/ --bindir=/ --libdir=/ --datarootdir=/share --sysconfdir=/$dir_name/etc --disable-gpgme-linking $4 $5 $6 $7 $8 $9 ${10} ${11}
make $MAKE_OPT
make DESTDIR="$build_dir" install-strip

foreach exec ( `ls "$build_dir/"*.exe` )
  echo "importing dlls used by $exec"
  set dlls=`cygcheck "$exec" | sed -rn -e 's#\s##g'  -e 's#\\#\\#g' -e 's#(.*\.dll)$#\1#p'`
  cp $dlls "$build_dir"
end

mv "$build_dir/share/dar" "$build_dir/doc"
mv "$build_dir/$dir_name/etc" "$build_dir/etc"
rm -rf "$build_dir/share" "$build_dir/include" "$build_dir/pkgconfig" "$build_dir/python3" "$build_dir/$dir_name"

foreach fichier (README TODO INSTALL ChangeLog THANKS COPYING)
   cp $fichier "$build_dir/`basename $fichier`.txt"
   misc/todos "$build_dir/`basename $fichier`.txt"
end

zip -9 -r "$dir_name".zip $dir_name
rm -rf $build_dir
