#!/bin/bash
#/**
#@file    otherservices.sh
#@brief   Script de inicio para cargar otros servicios complementarios.
#@version 1.0.3
#@author  Ramon Gomez, ETSII Universidad de Sevilla
#@date    2012-01-12
#*/

# Montar efivar filesystem
ogIsEfiActive && mount -t efivarfs none /sys/firmware/efi/efivars

# Lanzar servicios complementarios del cliente.
echo "${MSG_OTHERSERVICES:-.}"

# stunnel start
cat >/etc/stunnel/menu.conf <<__EOF__
setuid = stunnel4
setgid = stunnel4
pid = /var/run/stunnel4/menu.pid
foreground = yes
debug = info

[menu]
client = yes
accept = 127.0.0.1:81
connect = $ogcore:8443
__EOF__
if [[ true == "$ogusetls" ]]; then
    if [[ true == "$ogverifytls" ]]; then
        ## use tls and verify
        cat >>/etc/stunnel/menu.conf <<__EOF__
cert = /opt/opengnsys/etc/ogagent.crt
key = /opt/opengnsys/etc/ogagent.key
CAfile = /opt/opengnsys/etc/ca.crt
requireCert = yes
verifyChain = yes
__EOF__
    else
        ## use tls but not verify
        cat >>/etc/stunnel/menu.conf <<__EOF__
cert = /opt/opengnsys/etc/ogagent.crt
key = /opt/opengnsys/etc/ogagent.key
CAfile = /opt/opengnsys/etc/ca.crt
requireCert = no
verifyChain = no
__EOF__
    fi
else
    ## don't use tls
    cat >>/etc/stunnel/menu.conf <<__EOF__
requireCert = no
verifyChain = no
__EOF__
fi
mkdir -p /var/run/stunnel4; chown stunnel4:stunnel4 /var/run/stunnel4
stunnel /etc/stunnel/menu.conf &>/var/log/stunnel4/menu.log &
# stunnel end

# Iniciar dbus
if [ -e /etc/dbus-1/system.d/ogbrowser.conf ]; then
    mkdir -p /run/dbus
    DBUS_SESSION_BUS_ADDRESS=$(dbus-daemon --print-address --system --nosyslog)
    export DBUS_SESSION_BUS_ADDRESS
    dbus-monitor --system &>/var/log/dbus-monitor.log &
fi

# Iniciar rsyslog, si es necesario.
[ -S /dev/log ] || service rsyslog start

# Adpatar la clave de "root" para acceso SSH.
PASS=$(grep "^[ 	]*\(export \)\?OPTIONS=" /scripts/ogfunctions 2>&1 | \
	sed 's/\(.*\)pass=\(\w*\)\(.*\)/\2/')
PASS=${PASS:-"og"}
echo -ne "$PASS\n$PASS\n" | passwd root 2>/dev/null
# Cargar el entorno OpenGnsys en conexión SSH.
cp -a $OPENGNSYS/etc/preinit/loadenviron.sh /etc/profile.d/
# Arrancar SSH.
/etc/init.d/ssh start &>/dev/null

# Desactivado apagado de monitor.
#setterm -blank 0 -powersave off -powerdown 0 < /dev/console > /dev/console 2>&1

# Activado WOL en la interfaz usada en arranque PXE.
ethtool -s $DEVICE wol g 2>/dev/null

# TODO Localizar correctamente el script de arranque.
[ -f /opt/opengnsys/scripts/runhttplog.sh ] && /opt/opengnsys/scripts/runhttplog.sh 2>/dev/null


