#!/usr/bin/env bash
set -e

if [ -z "$SUDO_USER" ]; then
	USER_HOME=~
else
	USER_HOME=$(getent passwd "$SUDO_USER" | cut -d: -f6)
fi

[ -d /opt ] || mkdir -p /opt
[ -d "$USER_HOME/.config" ] || mkdir -p "$USER_HOME/.config"

# remove old settings
if [ -f "$USER_HOME/.droidcam/settings" ]; then
	mv "$USER_HOME/.droidcam/settings" "$USER_HOME/.config/droidcam"
	rm -rf "$USER_HOME/.droidcam"
fi
# remove old-old binaries
rm -f /usr/bin/droidcam*

echo "Copying files"
set -x
cp uninstall    /opt/droidcam-uninstall
cp icon2.png    /opt/droidcam-icon.png
cp droidcam     /usr/local/bin/
cp droidcam-cli /usr/local/bin/
set +x
if [ "$(command -v xdg-desktop-menu)" != "" ]; then
	xdg-desktop-menu install --novendor droidcam.desktop
fi
echo "Done"
