#!/bin/bash
set -eux

. mkp4a.common

# NOTE: $1 will be set with fdroid builds only.

echo '### prepare sdk'

if [[ $# == 0 ]]
then
  ./initsupport
  ./initsdk
fi

echo '### install p4a'

if [[ $# == 0 ]]
then
    if not python3 -m pip install -q --user "python-for-android==${p4aver}"
    then
        echo "obviously inside a virtualenv, so omit --user"
        if python3 -m pip install -q "python-for-android==${p4aver}"
        then
            echo "done"
        fi
    else
        echo "done"
    fi
fi

echo '### prepare source'

(cd .. && make rules && make all_games_html && make mo)

mkdir -p ${tmpdir}
rm -rf ${tmpdir}
cp -a .. ${tmpdir}
rm -rf ${tmpdir}/android
rm -rf ${tmpdir}/src

# remove useless load from the app
rm -rf ${tmpdir}/.git
rm -rf ${tmpdir}/.gitignore
rm -rf ${tmpdir}/Screenshots
rm -rf ${tmpdir}/build
rm -rf ${tmpdir}/contrib
rm -rf ${tmpdir}/dist
rm -rf ${tmpdir}/runtests.pl
rm -rf ${tmpdir}/tests
rm -rf ${tmpdir}/Brewfile
rm -rf ${tmpdir}/.appveyor.yml
rm -rf ${tmpdir}/.perltidyrc
rm -rf ${tmpdir}/.travis.yml

rm -rf ${tmpdir}/setup.py
rm -rf ${tmpdir}/setup_osx.py
rm -rf ${tmpdir}/setup.cfg

cp -a ${pkgdir}/supportlib/support-v4-24.1.1.aar ${tmpdir}
cp -a main.py ${tmpdir}
mkdir -p ${tmpdir}/data/images/cards/bottoms/trumps-only
echo "" > ${tmpdir}/data/images/cards/bottoms/trumps-only/.keep

echo '### prepare cardsets'

./mkcards

cp -a ${cardsets_dir}/* ${tmpdir}/data

echo '### end init'
