#!/bin/sh
set -e

# 'appstreamcli refresh --source=os' which is invoked by 'apt-get update'
# creates cache files that will be re-generated by live-config.
# These cache files are generated by lb for the C locale, while after boot in
# the live image the actual language is used (default=en_US) when a network
# connection was made. If no network is available 'GNOME Software' still works
# correctly without these cache files.
# Therefore these files can safely be removed from the live image.
#
# However, after all hooks have run, 'apt-get update' is run once again by
# 'chroot_archives chroot remove', therefore the original executable
# 'appstreamcli' is moved away by this script, to be restored upon the next
# invocation.

# Don't run if AppStream is not installed
if [ ! -e /usr/bin/appstreamcli ];
then
  exit 0
fi

# Don't run if the version of appstream is too old
if dpkg --compare-versions $(dpkg-query --show --showformat '${Version}\n' appstream | head -1) lt 0.15.2~;
then
  exit 0
fi

# Create a wrapper that restores itself on the next invocation
mv /usr/bin/appstreamcli /usr/bin/appstreamcli.orig
cat << EOF > /usr/bin/appstreamcli
#!/bin/sh
/usr/bin/appstreamcli.orig \$@
rm /usr/bin/appstreamcli
mv /usr/bin/appstreamcli.orig /usr/bin/appstreamcli
# Delete the cache files
rm -f /var/cache/swcatalog/cache/C-*.xb
EOF
chmod a+x /usr/bin/appstreamcli

echo "P: $(basename $0) Reproducible hook has been applied"
