#!/bin/bash
#
# Create MacOS releases on my development machine. Mostly
# dedicated to the setup on this machine. Might be useful as a starting
# point for anyone doing something similar. It automates the download,
# building and upload. In addition, it validates that the there are no
# obvious major mistakes during the process.

# Where to upload the final binary
downloaddir="ec:/home/swipl/web/download/stable/bin"

check-src()
{ version=$(git describe | sed 's/V[0-9]\.[0-9][0-9]*\.[0-9][0-9]*\(-\([a-z0-9]*\)\)\{0,1\}//')
  if [ ! -z "$version" ]; then
    echo "Version is dirty"
    exit 1
  fi
}

check-doc()
{ [ -f doc-version -a `cat VERSION` = `cat doc-version` ] || exit 1
}

update-src()
{ git pull --tags vu master
  ./prepare --server=http://eu.swi-prolog.org --yes
  check-src || exit 1
  check-doc || exit 1
  version=`cat VERSION`
}

check-build()
{ if grep -i error: *.out; then
    exit 1
  fi
}

build-swipl-win()
{ (cd packages/swipl-win && make clean)
  (cd packages/swipl-win && make -j)
  (cd packages/swipl-win && ./build-app)
  (cd packages/swipl-win && make -f Makefile.dmg)
}

upload()
{ for d in $downloaddir; do
    rsync -P packages/swipl-win/SWI-Prolog-$version.dmg $d
  done
}

build()
{ update-src || exit 1
  make distclean
  rm -rf $HOME/lib/swipl
  ./build
  build-swipl-win
  check-build || exit 1
  upload
}
