#!/bin/sh
set -e

if [ -t 2 ]
then
  BOLD="\033[1m"
  GREEN="\033[1;32m"
  NORMAL="\033[0m"
  RED="\033[1;31m"
  YELLOW="\033[1;33m"
else
  BOLD=""
  GREEN=""
  NORMAL=""
  RED=""
  YELLOW=""
fi

die () {
  echo "${BOLD}$script: ${RED}error:${NORMAL} $*" 1>&2
  exit 1
}

fatal () {
  echo "${BOLD}$script: ${RED}fatal error:${NORMAL} $*" 1>&2
  exit 1
}

msg () {
  echo "$script: $*" 1>&2
}

wrn () {
  echo "${BOLD}$script: ${YELLOW}warning:${NORMAL} $*" 1>&2
}

script=`basename $0`
PDF=$1

cd `dirname $0`/..
root=`pwd`
tmp=/tmp/prepare-isasat-eda2021-submission.log

#if [ x"$PDF" != x"" ] && [ -f $PDF ]; then
#    msg "using file $PDF as description"
#else
#    fatal "missing PDF file as description"
#fi;
VERSION="eda2021"
rm -f $tmp
##########################################################################
echo "make-src-release.sh"
cd $root
./scripts/make-src-release.sh >$tmp || exit 1
tar=`awk '{print $2}' $tmp |sed -e "s,',,g"`
echo `cat $tmp`
printf "$tar\n"
##########################################################################
echo "now starexec"
cd $root
base=isasat-${VERSION}-edachallenge
dir=/tmp/$base
rm -rf $dir
mkdir $dir
mkdir $dir/code
cp -a $tar $dir/code
cp -a ./src/LICENSE $dir/

echo "build script"
cat <<EOF >$dir/build.sh
#!/bin/sh
#mkdir -p binary
#tar xf ./code/isasat*
#mv isasat* isasat
#cd isasat/src
#make competition
#install -s isasat ../../binary/
EOF
chmod 755 $dir/build.sh

echo "run script"
cat <<EOF >$dir/run.sh
#!/bin/sh
tar xf ./code/isasat*
mv isasat* isasat
./isasat/binary/isasat \$@
EOF

chmod 755 $dir/run.sh

cat <<EOF >$dir/readme.txt
IsaSAT is a formally verified SAT solver using Isabelle

To simplify the building process we provide directly the generated files instead
of the Isabelle files; i.e., we provide the source code of the solver instead of
the correctness proof. The files are automatically generated in the LLVM
intermediate representation and require clang (test with version >= 9) to
compile. The full Isabelle files (with some comments) can be found on the
repository 'https://bitbucket.org/isafol/isafol/wiki/Home'.

The easiest solution is to use the package manager to install clang, but you can also:
  - download the official release from https://github.com/llvm/llvm-project/releases/download/llvmorg-12.0.0/clang+llvm-12.0.0-x86_64-linux-gnu-ubuntu-16.04.tar.xz
  - run the makefile with 'make CLANG=path/to/clang/bin/clang-12 CFLAGS="-flto -std=c11 -fuse-ld=path/to/clang/bin/ld.lld"'

Currently no proofs are generated by IsaSAT (this is the point of
verification---no need to check proofs!). The files 'isasat_restart.ll' and
'isasat_restart.h' are the generated files. The file 'isasat.c' is a
hand-written wrapper that parses the input file and is in charge of printing (in
particular the outcome).

To run the solver execute './binary/isasat'.
EOF

archive=/tmp/$base.zip
rm -f $archive
cd $dir
zip -r $archive .
cd /tmp/
ls -l $archive
rm -f $tmp
rm -rf $dir/

zipfileAll=/tmp/$base-all.zip
mkdir -p $dir
cd $dir
rm -rf *
cp $archive .
#cp /home/zmaths/Documents/repos/isafol-private/Papers/2021_EDA_Challenge/paper.pdf system_description_same_as_kissat.pdf
zip -q -r $zipfileAll .

rm -rf $dir/

msg "generated ${GREEN}'$zipfileAll'${NORMAL}"
BYTES="`ls --block-size=1 -s $zipfileAll 2>/dev/null |awk '{print $1}'`"
msg "full zip file has $BYTES bytes"
