#!/bin/bash

set -euo pipefail

apt-get install -y zip || true

cd ./venv/lib/python3.*/site-packages

python -m compileall .

candidates=$(
  for d in $(find . -mindepth 1 -maxdepth 1 -type d | grep -Ev '__pycache__|dist-info|_hack|librosa|demucs|botocore'); do
    if [ -z "$(find "$d" -name '*.so' -or -name '*.so.*')" ]; then
      echo "$d"
    fi
  done
)

find $candidates -name "*.cpython-*.pyc" \
  | awk '{print $1, $1}' \
  | sed 's/__pycache__\///2' \
  | sed 's/.cpython-[0-9]\{2,\}//2' \
  | xargs -n 2 mv
find $candidates -name "*.py" -delete
find $candidates -name "__pycache__" -exec rm -r {} +

zip -0r wavtool_optimized.zip $candidates
echo 'wavtool_optimized.zip' > wavtool_optimized.pth

rm -rf $candidates
