#!/bin/bash
#
# Copyright (C) 2015-2016 Red Hat, Inc.
#
# This file is part of atomic-devmode.
#
# atomic-devmode is free software: you can redistribute it
# and/or modify it under the terms of the GNU Lesser General
# Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your
# option) any later version.
#
# atomic-devmode 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 Lesser General Public License for
# more details.
#
# You should have received a copy of the GNU Lesser General
# Public License along with atomic-devmode. If not, see
# <http://www.gnu.org/licenses/>.

set -euo pipefail

# start up tmux only if it's an interactive session on tty1
# and we're not already in it

if [[ $- == *i* ]] && [[ $(tty) == /dev/tty1 ]] && [[ -z "${TMUX-}" ]]; then

    # silence has-session or it will complain if the tmux
    # server is not running yet
    if tmux has-session -t devmode 2>/dev/null; then
        tmux kill-session -t devmode
    fi

    # before entering tmux for good, let's shut up dmesg so
    # it doesn't pollute the tmux screen
    dmesg -n 1

    # mark the password as expired so that cockpit requests
    # a password reset on first login -- we can't do this in
    # bootcmd otherwise agetty will prompt for the password
    # reset during autologin
    chage -d 0 root

    exec tmux -f /usr/share/atomic-devmode/tmux.conf \
        new-session -s devmode -n main \
            /usr/libexec/atomic-devmode/init
fi

# provide an alias to print the root password to the command
# line, in case someone scrolls the top pane too far and
# doesn't know how to get back with tmux.
alias showpasswd='cat /run/atomic-devmode-root'

# drop strict mode so that normal shells don't inherit it
set +euo pipefail
