#!/bin/sh -e
#DEBHELPER#
mkdir /Applications 2>/dev/null || echo "/Applications already exists"
mkdir -p /etc/skel/.local/share/applications/appimage || echo ""
mkdir -p /etc/skel/Applications || echo ""

# Pre-populate existing users' home directories,
# otherwise they need to log in twice before Gnome picks up the
# desktop file location
echo "Installing ~/.local/share/applications/appimage, ~/Applications"
echo "and adding users to fuse group"
HOMES="$(ls -l /home/ | tail -1 |  awk  '{ print $3 "/" $4}')"
for HOME in "${HOMES}" ; do
    USER=$(echo "${HOME}" | cut -d / -f 1)
    DIR=$(echo "${HOME}" | cut -d / -f 2)
    mkdir -p "/home/${DIR}/.local/share/applications/appimage" || echo ""
    mkdir -p "/home/${DIR}/Applications" || echo ""
    chown -R "${USER}" "/home/${DIR}/.local" || echo ""
    chown -R "${USER}" "/home/${DIR}/Applications" || echo ""
    usermod -aG fuse "${USER}" || echo ""
done

# For Fedora-like systems (e.g., MeeGo Netbook 1.1)
if [ -e /etc/init.d/fuse ] ; then
    ln -sf /etc/init.d/fuse /etc/rc.d/rc5.d/S25fuse || echo ""
    ln -sf /etc/init.d/fuse /etc/rc.d/rc4.d/S25fuse || echo ""
    ln -sf /etc/init.d/fuse /etc/rc.d/rc3.d/S25fuse || echo ""
    ln -sf /etc/init.d/fuse /etc/rc.d/rc2.d/S25fuse || echo ""
    ln -sf /etc/init.d/fuse /etc/rc.d/rc1.d/S25fuse || echo ""
    /etc/init.d/fuse start || echo ""
fi

echo "You need to log out of your GNOME session for this to take effect"

# Make sure the script returns 0
true
