# Adapted from AUR: yubico-authenticator
#
# Ref: https://aur.archlinux.org/cgit/aur.git/tree/?h=yubico-authenticator&id=aa91641d83d6c7e8274543bb03ae9a26c7c434a9

abinfo "Building helper ..."
pushd "$SRCDIR"/helper
GIT_DIR='.' python3 -m build --wheel --no-isolation
popd

abinfo "Building yubioath-flutter ..."
fvm flutter build linux -v

abinfo "Installing files ..."

pushd "$SRCDIR"/helper
python3 -m installer --destdir="$PKGDIR" dist/*.whl
popd

install -Dvm755 "$SRCDIR"/helper/authenticator-helper.py \
    "$PKGDIR"/usr/lib/"$PKGNAME"/helper/authenticator-helper
install -Dvm755 "$SRCDIR"/helper/shell.py "$PKGDIR"/usr/lib/"$PKGNAME"/helper/shell.py

if ab_match_arch amd64; then
    export FLUTTER_ARCH="x64" 
elif ab_match_arch arm64; then
    export FLUTTER_ARCH="arm64"
fi

install -Dvm755 "$SRCDIR"/build/linux/"$FLUTTER_ARCH"/release/bundle/authenticator \
    "$PKGDIR"/usr/lib/"$PKGNAME"/authenticator
install -Dvm644 "$SRCDIR"/resources/linux/linux_support/com.yubico.yubioath.desktop \
    "$PKGDIR"/usr/share/applications/com.yubico.yubioath.desktop
install -Dvm644 "$SRCDIR"/resources/icons/com.yubico.yubioath.png \
    "$PKGDIR"/usr/share/pixmaps/com.yubico.yubioath.png
cp -rv "$SRCDIR"/build/linux/"$FLUTTER_ARCH"/release/bundle/{data,lib} "$PKGDIR"/usr/lib/"$PKGNAME"

abinfo "Creating symlink to /usr/bin ..."
install -dv "$PKGDIR"/usr/bin
ln -sv ../lib/"$PKGNAME"/authenticator "$PKGDIR"/usr/bin/"$PKGNAME"
