#!/bin/sh


### Load some configs
. /etc/fliwi-vhost/base.conf

machine=$1
auto_set_pid_file=1

if [ ! -d "/var/run/fliwi-vhost" ]; then
  mkdir /var/run/fliwi-vhost
  if [ $? -ne 0 ]; then
    echo "ERROR: Can not create directory '/var/run/fliwi-vhost'" 1>&2
    exit 1
  fi
fi

if [ -f "$machine" ]; then
  machine=$(basename $machine | sed -r 's/\.conf$//')
fi

kvm=$KVM_EXECUTABLE
machines_dir=$MACHINES_CONFIG_DIR
machine_option_file=$machines_dir"/"$machine".conf"

if [ ! -e $machine_option_file ]; then
  echo "No such machine!" 1>&2
  exit 1
fi

machine_option_string=$( cat $machine_option_file \
                           | grep -ve '^\s*$' \
                           | grep -ve '^#' \
                           | grep -v -E -e '^fliwi-' \
                           | while read option; do echo "-"$option; done )

if [ $(cat $machine_option_file | grep -c -e '^pidfile') -ne 0 ]; then
  auto_set_pid_file=0
fi

### nice value
nicevalue=$(grep -E -e '^fliwi-nice=' $machine_option_file | cut -d '=' -f 2)
test 100 -le $nicevalue > /dev/null 2>&1
if [ $? -ne 1 ] || \
   [ "$nicevalue" = '' ] || \
   [ $nicevalue -lt -20 ] || \
   [ $nicevalue -gt 19 ]; then
  nicevalue=17
fi
echo Nicevalue is: $nicevalue 1>&2

### Conserver
use_conserver=$(cat $machine_option_file | grep -c -E -e '^fliwi-conserver=enabled')
serial_console_string=''
serial_kvm_mon_string=''
sgabios_string=''
serial_console_socket="/var/run/fliwi-vhost/$machine.serial"
if [ $use_conserver -eq 1 ]; then
  #NOT NEEDED, since "mon:..." does this for use#serial_console_string="-serial unix:$serial_console_socket,server,nowait"
  serial_kvm_mon_string="-serial mon:unix:$serial_console_socket,server,nowait"
  sgabios_string="-device sga"
  if [ $(grep -c -E -e '^vga ' $machine_option_file) -le 0 ]; then
    ### No explicit vga-adpater configured - we assume one of type 'cirrus' should be loaded...
    sgabios_string="-vga cirrus $sgabios_string"
  fi

  if [ $(grep -c -E -e '^(display|vnc|nographic|curses) ' $machine_option_file) -le 0 ]; then
    ### No graphics-adapter configured - configure qemu-kvm to use a vnc-server, but not start it automatically...
    sgabios_string="-vnc none $sgabios_string"
  fi
fi

### PID-file
if [ $auto_set_pid_file -eq 1 ]; then
  pid_file_option="-pidfile /var/run/fliwi-vhost/$machine.pid"
fi

machine_option_string=$sgabios_string" "$serial_console_string" "$serial_kvm_mon_string" "$pid_file_option" "$machine_option_string
cd $machines_dir
echo "Options are:" 1>&2
echo $machine_option_string 1>&2
nice -n $nicevalue $kvm $machine_option_string
kvm_exit_code=$?
if [ $kvm_exit_code -ne 0 ]; then
  echo "ERROR: Could not start vServer '$machine'" 1>&2
  exit $kvm_exit_code
fi

if [ $use_conserver -eq 1 ] && \
   [ -S "$serial_console_socket" ]; then
  chown conservr.root $serial_console_socket
  chmod 770 $serial_console_socket

  ### Update conserver's config
  /usr/sbin/fliwi-vhost-update-conserver-config 2>&1 | grep -F "'$machine'"

  ### Reload conserver (if possible)
  conserver_pid_file="/var/run/conserver.pid"
  if [ -f "$conserver_pid_file" ]; then
    chmod 644 $conserver_pid_file
    conserver_pid=$(cat $conserver_pid_file)
    if [ $(ps -f --no-headers --pid $conserver_pid | grep -F "$(which conserver)" | grep -c $conserver_pid) -eq 1 ]; then
      /etc/init.d/conserver-server reload
    fi
  fi
fi

exit 0

