#!/bin/sh

set -eu

add_autostart_when_needed() {
  cat <<'EOF' > /etc/X11/Xsession.d/65dropbox-autostart-when-needed
# If user does not have the ~/Dropbox directory, he/she probably
# does not want it to start automatically.
if [ ! -d ~/Dropbox ]; then
  rm -f ~/.config/autostart/dropbox.desktop
fi
EOF
}

command=$1
shift

case "${command}" in
    configure)
        upstream_dir=$1
        mkdir -p /var/lib/dropbox
        ln -fns -T "${upstream_dir}/.dropbox-dist" /var/lib/dropbox/.dropbox-dist
        add_autostart_when_needed
        ;;
    unconfigure)
        rm -f /etc/X11/Xsession.d/65dropbox-autostart-when-needed \
              /var/lib/dropbox/.dropbox-dist
        ;;
    unpack)
        tar --no-same-owner -z -x -f "$1" -C "$2"

        ## Fix upstream pack: upstream distributes the file as only
        ## owner-readable, but it needs to be readable by all, otherwise
        ## dropbox fails to run as non-root (prompts for root password).
        find "$2" -type f -name top_level.txt -exec chmod a+r {} \;
        ;;
    *)
        ;;
esac
