#!/bin/sh

. /lib/lsb/init-functions

conserver_main_config_file="/etc/fliwi-console-server/conserver/conserver.cf"
conserver_vhost_config_file="/etc/fliwi-console-server/conserver/conserver.fliwi-vhosts.cf"
conserver_custom_config_file="/etc/fliwi-console-server/conserver/conserver.fliwi-custom-configuration.cf"

conserver_oasswd_file="/etc/fliwi-console-server/conserver/conserver.passwd"

sshd_authorized_keys_file="/etc/fliwi-console-server/sshd/authorized_keys"
ssh_public_keys_dir="/ymc_config/shared/access/admins"

CONSERVER_DAEMON=/usr/sbin/conserver
CONSERVER_PIDFILE=/var/run/conserver.pid

IFS_LINEBREAK='
'

my_own_short_hostname=$(hostname -s)

if [ ! -e "$conserver_vhost_config_file" ]; then
  touch $conserver_vhost_config_file
fi

if [ ! -e "$conserver_custom_config_file" ]; then
  touch $conserver_custom_config_file
fi

if [ ! -e "$conserver_oasswd_file" ]; then
  touch $conserver_oasswd_file
fi

### Remember the md5sum of the current configs
md5sum_conserver_main_config_file="$(md5sum $conserver_main_config_file)"
md5sum_conserver_vhost_config_file="$(md5sum $conserver_vhost_config_file)"


### Rewrite aut
cat > $sshd_authorized_keys_file <<EOF
# Dynamic authorized_keys(5) file generated by $(basename $0)
#     DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
EOF
for ssh_key_file in $(find $ssh_public_keys_dir/ -maxdepth 1 -mindepth 1 -type f | sort)
do
  fliwi_admin_name="$(basename $ssh_key_file)"
  echo "# User: $fliwi_admin_name" >> $sshd_authorized_keys_file
  temp_ssh_key_count=0
  IFS_ORG=$IFS
  IFS=$IFS_LINEBREAK
  for line in $(cat $ssh_key_file | grep -E -e '^ssh-[a-z]+ [^ ]+ .+$')
  do
    echo "command=\"/usr/share/fliwi-console-server/ssh-startup '$fliwi_admin_name'\",no-port-forwarding $line" >> $sshd_authorized_keys_file
    temp_ssh_key_count=$(expr $temp_ssh_key_count + 1)
  done
  ORG=$IFS_ORG
  echo "" >> $sshd_authorized_keys_file
  echo "NOTE: Added $temp_ssh_key_count ssh-key(s) for user '$fliwi_admin_name'" 1>&2
done


### TBD: Currently we lak of any authentication...
cat > $conserver_oasswd_file <<EOF
# Dynamic conserver.passwd(5) file generated by $(basename $0)
#     DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
*any*:
EOF


### Main configuration for conserver
cat > $conserver_main_config_file <<EOF
# Dynamic conserver.cf(5) file generated by $(basename $0)
#     DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
##############################################################################
# If you need custom configurations, please use the following file, which
# capable of overriding any configuration previously done in here:
#     $conserver_custom_config_file
##############################################################################

config *
{
  sslrequired no;
}

default *
{
  # Note: The character '&' is substituted with the console name
  logfile /var/log/conserver/&.log;
  timestamp "";
  ro *;
  master localhost;
  type noop;
}

access *
{
  allowed 127.0.0.1;
  limited *;
}

console FLIWI-LOCAL-$my_own_short_hostname
{
  motd "This is $my_own_short_hostname serving...";
}

#include $conserver_vhost_config_file
#include $conserver_custom_config_file

EOF

### vHost configuration for conserver
cat > $conserver_vhost_config_file <<EOF
# Dynamic conserver.cf(5) file generated by $(basename $0)
#     DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN

EOF

### Scan fliwi-vhosts //start
for vhost_services in $(fliwi-get-all-services 2>/dev/null | grep -E -e '^fliwi-vhost')
do
  for numbered_vhost_service in $(fliwi-get-services $vhost_services 2>/dev/null)
  do
    echo "console FLIWI-REMOTE-$numbered_vhost_service" >> $conserver_vhost_config_file
    echo "{" >> $conserver_vhost_config_file
    echo "  type noop;" >> $conserver_vhost_config_file
    echo "  master $numbered_vhost_service;" >> $conserver_vhost_config_file
    echo "  motd \"This is a dummy entry to retrieve console-information from '$numbered_vhost_service'\";" >> $conserver_vhost_config_file
    echo "}" >> $conserver_vhost_config_file
    echo "" >> $conserver_vhost_config_file
    echo "NOTE: Added conserver-setup for '$numbered_vhost_service'" 1>&2

    for info in $(LC_ALL=C console -i -M $numbered_vhost_service | \
                             grep -E -e '^([^:]*:){14}\\n' | \
                             cut -d ',' -f 1 --only-delimited | \
                             sort -n | \
                             uniq )
    do
      console=$(echo $info | cut -d : -f 1 -s)
      host=$(echo $info | cut -d : -f 2 -s)
      echo "console $console" >> $conserver_vhost_config_file
      echo "{" >> $conserver_vhost_config_file
      echo "  type noop;" >> $conserver_vhost_config_file

      services_on_host="$(echo $(fliwi-get-services $console))"
      if [ -n "$services_on_host" ]; then
        echo "  aliases $(echo $services_on_host | sed -r 's/[[:space:]]+/,/g');" >> $conserver_vhost_config_file
      fi

      echo "  master $host;" >> $conserver_vhost_config_file
      echo "}" >> $conserver_vhost_config_file
      echo "" >> $conserver_vhost_config_file

      echo "NOTE: Added conserver-setup for '$console'" 1>&2
    done

    echo "" >> $conserver_vhost_config_file
  done
done
### Scan fliwi-vhosts //start

### Scan YClegacy ymc-vhosts //start
for numbered_vhost_service in $(fliwi-get-services ymc-vhost 2>/dev/null)
do
  echo "console INFO-$numbered_vhost_service" >> $conserver_vhost_config_file
  echo "{" >> $conserver_vhost_config_file
  echo "  type noop;" >> $conserver_vhost_config_file
  echo "  master $numbered_vhost_service;" >> $conserver_vhost_config_file
  echo "  motd \"This is a dummy entry to retrieve console-information from '$numbered_vhost_service'\";" >> $conserver_vhost_config_file
  echo "}" >> $conserver_vhost_config_file
  echo "" >> $conserver_vhost_config_file
  echo "NOTE: Added conserver-setup for '$numbered_vhost_service'" 1>&2

  for info in $(LC_ALL=C console -i -M $numbered_vhost_service | \
                           grep -E -e '^([^:]*:){14}\\n' | \
                           cut -d ',' -f 1 --only-delimited | \
                           sort -n | \
                           uniq )
  do
    console=$(echo $info | cut -d : -f 1 -s)
    host=$(echo $info | cut -d : -f 2 -s)
    echo "console $console" >> $conserver_vhost_config_file
    echo "{" >> $conserver_vhost_config_file
    echo "  type noop;" >> $conserver_vhost_config_file

    services_on_host="$(echo $(fliwi-get-services $console))"
    if [ -n "$services_on_host" ]; then
      echo "  aliases $(echo $services_on_host | sed -r 's/[[:space:]]+/,/g');" >> $conserver_vhost_config_file
    fi

    echo "  master $host;" >> $conserver_vhost_config_file
    echo "}" >> $conserver_vhost_config_file
    echo "" >> $conserver_vhost_config_file

    echo "NOTE: Added conserver-setup for '$console'" 1>&2
  done

  echo "" >> $conserver_vhost_config_file
done
### Scan YClegacy ymc-vhosts //end


### Reload conserver if configuration has changed
if [ "$md5sum_conserver_main_config_file" != "$(md5sum $conserver_main_config_file)" ] || \
   [ "$md5sum_conserver_vhost_config_file" != "$(md5sum $conserver_vhost_config_file)" ]; then
  conserver_pid=$(pidofproc -p $CONSERVER_PIDFILE $CONSERVER_DAEMON 2>/dev/null)
  if [ $? -eq 0 ] && \
     [ -n "$conserver_pid" ]; then
    echo "INFO: Reloading conserver-server..." 1>&2
    kill -1 $conserver_pid
  else
    echo "INFO: Not reloading conserver-server, since it is not running..." 1>&2
  fi
else
  echo "INFO: Not reloading conserver-server, since configuration is unchanged..." 1>&2
fi





