#!/usr/bin/sh

SOCKET=/var/run/hylafax-enterprise-appliance-console.socket

read nrows ncols <<< $(stty size)
printf "Current geometry: %s lines, %s columns" "$nrows" "$ncols" | \
    /usr/bin/logger -t "attach-console(${$})"

if [[ ! -S $SOCKET ]]; then
    echo No socket found for HylaFAX Enterprise Appliance Console
    exit 1
fi

restore_stty() {
    trap - 0 1 2 15
    stty cols "$ncols" rows "$nrows"
}

trap restore_stty 0 1 2 15

stty cols 80 rows 25
if [ "$1" = "tty" ]
then
    dtach -a $SOCKET -Ez
else
    dtach -a $SOCKET -r ctrl_l
fi
