#!/bin/bash

# basename="PyOPUS-0.11.2"
tag=`git describe --tags --dirty --broken --match "v*"`
basename="PyOPUS-${tag:1}"
echo "File base name: $basename"

docdemofile=$basename-doc-demo.tar.gz

# Remove generated version file
rm -f pyopus/_version.py

# Uninstall before building
# At this time (Debian 12, 2023-08-22) this does not work in a virtual environment
# echo Uninstalling PyOPUS. 
# pip3 uninstall -y pyopus

#######

echo Cleaning up
rm -rf build
rm -rf dist

echo
echo Building wheel
python3 -m build --no-isolation --wheel

# Remove past developer install
echo 
echo Removing developer install
rm -f easy-install.pth
rm -rf PyOPUS.egg-info
rm -f PyOPUS.egg-link 
rm -f site.py*

echo
echo Building source distribution
rm MANIFEST
python3 -m build --no-isolation --sdist

# Developer install via pip3
echo
echo Performing developer install in current folder. 
pip3 install -e . --no-deps

echo
echo Building documentation
cd docsrc
export LC_ALL=C
make clean
make html SPHINXBUILD='python3 -m sphinx'
cd ..

echo
echo Collecting files for docs and demos
mkdir -p build/$basename/docsrc/_build/html
cp -R docsrc/_build/html/* build/$basename/docsrc/_build/html

mkdir -p build/$basename/demo
cp -R demo/* build/$basename/demo
find build/$basename/demo/gui -name pyopusgui.config -exec rm \{\} \;
find build/$basename/demo/gui -type f -name "*.pck" -exec rm \{\} \;

echo
echo Packing
cd build
tar cfz ../dist/$docdemofile $basename

echo
echo Final cleanup
cd ..
rm -rf build/$basename

# Uninstall PyOPUS
# At this time (Debian 12, 2023-08-22) this does not work in a virtual environment
# pip3 uninstall -y pyopus

echo Done.

