#!/bin/sh

po_domain="alterator-tc-lock"
alterator_api_version=1

. alterator-sh-functions
. shell-config
. shell-quote
. shell-var

readonly LOG="/tmp/alterator-tc-lock.log"
readonly CONFIG="/etc/alterator-tc-lock-nologin"

# Truncate log
echo > "$LOG"

on_message() {
    case "$in_action" in
        write)
            case "$in__objects" in
                /)
                    if shell_var_is_yes "$in_lock"; then
                        echo "$(date): ask to lock screen" >> "$LOG"
                        echo "Flag-file for altertor-tc-lock" > "$CONFIG"
                        # Lock screen
                        alterator-tc-lock
                    fi

                    if shell_var_is_no "$in_lock"; then
                        echo "$(date): ask to unlock screen" >> "$LOG"
                        rm -f "$CONFIG"
                        # Unlock screen
                        alterator-tc-lock
                    fi
                    ;;
            esac
            ;;
        read)
            case "$in__objects" in
                /)
                    if [ -f "$CONFIG" ]; then
                        echo "$(date): read status: locked" >> "$LOG"
                        write_string_param "locked" "yes"
                    else
                        echo "$(date): read status: unlocked" >> "$LOG"
                        write_string_param "locked" "no"
                    fi
                    ;;
            esac
            ;;
    esac
}

message_loop
