#!/usr/bin/bash
# Copyright © 2018 TermySequence LLC
#
# SPDX-License-Identifier: GPL-2.0-only

usage () {
    echo "Usage: termy-setup [options]"
    echo
    echo "Perform termy-server setup actions for a given user."
    echo
    echo "Options:"
    echo "  --systemd  Enable and start systemd user service"
    echo "  --bash     Enable shell integration for bash"
    echo "  --zsh      Enable shell integration for zsh"
    echo
    echo "  --man      Launch man page"
    echo "  --version  Print version information"
}

action_systemd=
action_bash=
action_zsh=
server_pid=

if [ "$#" -lt 1 ]; then
    echo "No option specified" 1>&2
    usage
    exit 1
fi
for i in "$@"; do
    case "$i" in
        --help)
            usage
            exit 0
            ;;
        --version)
            echo 1.1.4
            exit 0
            ;;
        --man)
            exec man termy-setup
            ;;
        --systemd)
            action_systemd=1
            ;;
        --bash)
            action_bash=1
            ;;
        --zsh)
            action_zsh=1
            ;;
        *)
            echo "Unknown argument $i" 1>&2
            exit 1
            ;;
    esac
done

echo "This is termy-setup, version 1.1.4"
echo

if [ -t 1 ]; then isatty=1; else isatty=; fi

print_start() {
    task_failure=
    echo "● $1"
}

print_result() {
    if [ "$task_failure" = 1 ]; then
        echo -n "        "
        if test "$isatty"; then echo -ne "\033[31m"; fi
        echo -n "✘"
        if test "$isatty"; then echo -ne "\033[m"; fi
    else
        echo -n "        "
        if test "$isatty"; then echo -ne "\033[32m"; fi
        echo -n "✔"
        if test "$isatty"; then echo -ne "\033[m"; fi
    fi

    case "$task_failure" in
        1)
            echo " Failed"
            ;;
        2)
            echo " Not necessary"
            ;;
        3)
            echo " Skipped"
            ;;
        *)
            echo " Success"
            ;;
    esac
}

stop_server() {
    pidfile="/tmp/termy-server$UID/pid"
    if [ -z "$server_pid" -a -f $pidfile ]; then
        server_pid=$(<$pidfile)
    fi

    numre='^[0-9]+$'
    if [[ "$server_pid" =~ $numre ]]
    then
        kill $server_pid || task_failure=1
    else
        task_failure=2
    fi
}

check_for_existing() {
    print_start "Enable shell integration in $1"
    if grep -q iterm2_shell_integration "$1" 2>/dev/null; then
        echo "  ! Existing shell integration code detected, skipping" 1>&2
        task_failure=3
        return 1
    fi
    conf_comment="# Inserted by termy-setup 1.1.4 on $(date)"
    return 0
}

if test "$action_systemd"; then
    print_start "Enable systemd user service"
    if [ "$XDG_SESSION_ID" ]; then
        systemctl --user -q enable termy-server.socket || task_failure=1
        print_result

        print_start "Start systemd user service"
        systemctl --user -q start termy-server.socket || task_failure=1
        print_result

        print_start "Enable long-running services (may prompt for password)"
        loginctl enable-linger
        print_result

        print_start "Stop existing non-systemd server"
        stop_server
    else
        echo "Error: XDG_SESSION_ID not set" 1>&2
        echo "Please run this command within a fully formed login session (console or ssh)" 1>&2
        task_failure=1
    fi
    print_result
fi

if test "$action_bash"; then
    if [ -f "$HOME/.profile" ]; then
        conffile="$HOME/.profile"
    else
        conffile="$HOME/.bash_profile"
    fi

    if check_for_existing "$conffile"; then
        cat <<EOF >>"$conffile" || task_failure=1

$conf_comment
if [ "\$TERMYSEQUENCE" ]; then
  source /usr/share/termy-server/iterm2_shell_integration.bash
fi
EOF
    fi
    print_result
fi

if test "$action_zsh"; then
    conffile="$HOME/.zshrc"
    if check_for_existing "$conffile"; then
        cat <<EOF >>"$conffile" || task_failure=1

$conf_comment
if [ "\$TERMYSEQUENCE" ]; then
  source /usr/share/termy-server/iterm2_shell_integration.zsh
fi
EOF
    fi
    print_result
fi

echo
echo "Setup finished!"
exit 0
