#!/bin/sh
# Copyright © 2011  Luca Capello <luca@pca.it>
#
# schroot is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# schroot is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see
# <http://www.gnu.org/licenses/>.
#
#####################################################################
# 10extract-xauth: Extract X authority information for the running
# user, thus allowing X applications to be started from withing the
# schroot when the --preserve-environment option is used.
#####################################################################

set -e

. "$SETUP_DATA_DIR/common-data"
. "$SETUP_DATA_DIR/common-functions"

### this should be done in a better way
if $LIBEXEC_DIR/schroot-listmounts -m "$CHROOT_PATH" | grep -q "/home$"; then
    warn "/home mounted in the schroot, doing nothing for the Xauthority file"

else
    ### this is a bad hack because $HOME is undefined and it does not
    ### consider more complex setups like /home/group/user
    if [ "$AUTH_USER" != root ]; then
        HOME_AUTH_USER="/home/${AUTH_USER}"
        HOME_AUTH_USER_CHROOT="${CHROOT_PATH}/${HOME_AUTH_USER}"
        XAUTHORITY_FILE="${HOME_AUTH_USER}/.Xauthority"
        XAUTHORITY_FILE_CHROOT="${HOME_AUTH_USER_CHROOT}/.Xauthority"

        if [ $STAGE = "setup-start" ] || [ $STAGE = "setup-recover" ]; then
            if [ ! -d "$HOME_AUTH_USER_CHROOT" ]; then
                warn "HOME for user '$AUTH_USER' does not exist in the schroot, creating"
                UMASK=$(grep ^UMASK /etc/login.defs | awk '{print $2}')
                ### `umask -S $UMASK` does not work in dash
                HOME_AUTH_USER_CHROOT_MODE=$(expr 777 - "$UMASK")
                mkdir -m "$HOME_AUTH_USER_CHROOT_MODE" "$HOME_AUTH_USER_CHROOT"
                chown "$AUTH_USER" "$HOME_AUTH_USER_CHROOT"
            fi

            if [ ! -r "$XAUTHORITY_FILE" ] ; then
                warn "Xauthority file for user '$AUTH_USER' not found, skipping creation"
            elif [ -r "$XAUTHORITY_FILE_CHROOT" ]; then
                warn "Xauthority file for user '$AUTH_USER' already found in the schroot, skipping creation"
            else
                xauth -f "$XAUTHORITY_FILE" extract "$XAUTHORITY_FILE_CHROOT" :0
                chown "$AUTH_USER" "$XAUTHORITY_FILE_CHROOT"
            fi

        elif [ $STAGE = "setup-stop" ]; then
            if [ ! -r "$XAUTHORITY_FILE_CHROOT" ] ; then
                warn "Xauthority file for user '$AUTH_USER' not found in the schroot, skipping removal"
            else
                rm "$XAUTHORITY_FILE_CHROOT"
            fi
        fi
    fi
fi
