#!/bin/sh
#
# Contact: Pekka Lundstrom  <pekka.lundstrom@jollamobile.com>
#
# Copyright (c) 2013, Jolla Ltd.
# Copyright (c) 2014, Hedayat Vatankhah <hedayat.fwd@gmail.com>
# All rights reserved.
#
# Updates:
#    Hedayat Vatankhah <hedayat.fwd@gmail.com>:
#      * Modified to be compatible with new systemd. It is now run as default
#        user rather than root, and is used as the users's session manager.
#        Session restarts are now managed by systemd itself.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#     * Neither the name of the <organization> nor the
#       names of its contributors may be used to endorse or promote products
#       derived from this software without specific prior written permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#
# This starts user session either in ACT_DEAD or USER mode
# This also keeps eye on need for user session restart

wait_user_service_to_start() {
    # This waits untill user-session has started
    # We also check need for restart

    sleep 2 # Wait a little to make sure that user@.service is started
    local ST=""
    while [ 1 ]; do
        if [ -f $RESTART_FILE ]; then
            # User session needs to be restarted
            echo "Going to restart user session $DEF_UID"
            rm -f $RESTART_FILE
            return 1
        fi
        ST=$(systemctl is-active $USER_SERVICE)
        if [ "$ST" == "active" ]; then
            return 0
        fi
        if [ "$ST" == "activating" ]; then
            sleep 1
        else
            echo " User session failed to start, status = $ST"
            return 1
        fi
    done
}


# First find out the user id we use
DEF_UID=$(grep "^UID_MIN" /etc/login.defs |  tr -s " " | cut -d " " -f2)
DEF_UNAME=$(getent passwd $DEF_UID | cut -d ":" -f1)
USER_SERVICE=user\@${DEF_UID}.service

echo "Starting new session"
wait_user_service_to_start
st=$?
if [ $st -eq 0 ]; then
    echo "New session started"
    # We are the session manager, if we exit, the session is terminated!
    while sleep 365d; do
        true
    done
    # loop will be terminated if 'sleep' exits abnormaly, e.g. service stopped
    echo "User session terminated"
else
    echo "Starting new session in failed"
fi

# Will be restarted by systemd. To shutdown, user service must be stopped using systemctl
exit $st

