#!/usr/bin/ruby

require 'cgi'
cgi = CGI.new
print cgi.header

user=ENV['REMOTE_USER'].partition('@').first
ENV.delete("HTTP_COOKIE")

unless user.nil? || cgi.params['action'].first.nil?
    case cgi.params['action'].first
    when 'user_id'
    command_file="export USER=#{user}; request=$(oneuser list | awk '$2~/#{user}/'); output=$(echo $request) > /dev/null 2>&1; echo $?; echo $output"
    fork { exec("#{command_file}") }
    when 'user_vmlist'
    command_file="export USER=#{user}; request=$(onevm list #{cgi.params['id'].first} -lNAME --csv | sed '1d' | tr '\n' ','); output=$(echo $request) > /dev/null 2>&1; echo $?; echo $output"
    fork { exec("#{command_file}") }
    when 'user_idlist'
    command_file="export USER=#{user}; request=$(onevm list #{cgi.params['id'].first} -lID | awk 'NR>1'); output=$(echo $request) > /dev/null 2>&1; echo $?; echo $output"
    fork { exec("#{command_file}") }
    when 'vm_state_list'
    command_file="export USER=#{user}; request=$(onevm list #{cgi.params['id'].first} -lSTAT | awk 'NR>1'); output=$(echo $request) > /dev/null 2>&1; echo $?; echo $output"
    fork { exec("#{command_file}") }
    when 'vm_status'
    unless cgi.params['id'].first.nil?
    command_file="export USER=#{user}; request=$(onevm show #{cgi.params['id'].first} | grep LCM_STATE | awk '{ print $NF }'); output=$(echo $request) > /dev/null 2>&1; echo$?; echo $output"
    fork { exec("#{command_file}") }
    end
    when 'vm_state'
    unless cgi.params['id'].first.nil?
    command_file="export USER=#{user}; request=$(onevm show #{cgi.params['id'].first} | grep STATE | awk '{ print $NF }' | sed -n '1p'); output=$(echo $request) > /dev/null 2>&1; echo$?; echo $output"
    fork { exec("#{command_file}") }
    end
    when 'resume'
    command_file="export USER=#{user}; onevm resume #{cgi.params['id'].first}; output=$(klist); > /dev/null 2>&1; echo $?; echo $output > ~/file.txt"
    fork { exec("#{command_file}") }
    when 'get_chardev'
    command_file="export USER=#{user}; request=$(virsh -c qemu+tcp://#{cgi.params['host'].first}/system qemu-monitor-command --hmp one-#{cgi.params['vmid'].first} 'info chardev' | tr -d $'\\r' | grep #{cgi.params['port'].first} | awk -F: '{print $1}'); output=$(echo $request); > /dev/null 2>&1; echo $?; echo $output"
    fork { exec("#{command_file}") }
    when 'delete_old_redir'
    command_file="export USER=#{user}; virsh -c qemu+tcp://#{cgi.params['host'].first}/system qemu-monitor-command --hmp one-#{cgi.params['vmid'].first} device_del redir#{cgi.params['dev_id'].first}; > /dev/null 2>&1; echo $?"
    fork { exec("#{command_file}") }
    when 'add_new_charredir'
    command_file="export USER=#{user}; virsh -c qemu+tcp://#{cgi.params['host'].first}/system qemu-monitor-command --hmp one-#{cgi.params['vmid'].first} chardev-add socket,id=charredir#{cgi.params['dev_id'].first},port=#{cgi.params['port'].first},host=#{cgi.params['chost'].first}; > /dev/null 2>&1; echo $?"
    fork { exec("#{command_file}") }
    when 'device_add'
    command_file="export USER=#{user}; virsh -c qemu+tcp://#{cgi.params['host'].first}/system qemu-monitor-command --hmp one-#{cgi.params['vmid'].first} device_add usb-redir,chardev=charredir#{cgi.params['dev_id'].first},id=redir#{cgi.params['dev_id'].first},bus=usb.0,debug=4; > /dev/null 2>&1; echo $?"
    fork { exec("#{command_file}") }
    when 'get_connection_type'
    command_file="export USER=#{user}; request=$(onevm list -x | xmlstarlet sel -t -v \"/VM_POOL/VM[ID=\"#{cgi.params['id'].first}\"]/TEMPLATE/GRAPHICS/TYPE\" -n); output=$(echo $request); > /dev/null 2>&1; echo $?; echo $output"
    fork { exec("#{command_file}") }
    when 'get_guest_agent'
    command_file="export USER=#{user}; request=$(onevm show #{cgi.params['id'].first} -x | xmlstarlet sel -t -v \"/VM/TEMPLATE/FEATURES/GUEST_AGENT\" -n); output=$(echo $request); > /dev/null 2>&1; echo $?; echo $output"
    fork { exec("#{command_file}") }
    when 'get_host'
    command_file="export USER=#{user}; request=$(onevm show #{cgi.params['id'].first} | grep HOST | awk '{ print $NF }'); output=$(echo $request) > /dev/null 2>&1; echo$?; echo $output"
    fork { exec("#{command_file}") }
    when 'password'
    command_file="export USER=#{user}; request=$(onevm list -x | xmlstarlet sel -t -v \"/VM_POOL/VM[ID=\"#{cgi.params['id'].first}\"]/TEMPLATE/GRAPHICS/PASSWD\" -n); output=$(echo $request); > /dev/null 2>&1; echo $?; echo $output"
    fork { exec("#{command_file}") }
    when 'connection_port'
    command_file="export USER=#{user}; request=$(onevm list -x | xmlstarlet sel -t -v \"/VM_POOL/VM[ID=\"#{cgi.params['id'].first}\"]/TEMPLATE/GRAPHICS/PORT\" -n); output=$(echo $request); > /dev/null 2>&1; echo $?; echo $output"
    fork { exec("#{command_file}") }
    when 'get_xrdp_install_pid'
    command_file="export USER=#{user}; request=$(virsh -c qemu+tcp://#{cgi.params['host'].first}/system qemu-agent-command one-#{cgi.params['id'].first} '{\"execute\":\"guest-exec\", \"arguments\":{\"path\":\"/bin/which\", \"arg\":[\"xrdp\"], \"capture-output\":true}}'); output=$(echo $request); > /dev/null 2>&1; echo $?; echo $output"
    fork { exec("#{command_file}") }
    when 'get_xrdp_install'
    command_file="export USER=#{user}; request=$(virsh -c qemu+tcp://#{cgi.params['host'].first}/system qemu-agent-command one-#{cgi.params['id'].first} '{\"execute\":\"guest-exec-status\", \"arguments\":{\"pid\":#{cgi.params['pid'].first}}}'); output=$(echo $request); > /dev/null 2>&1; echo $?; echo $output"
    fork { exec("#{command_file}") }
    when 'get_xrdp_pid'
    command_file="export USER=#{user}; request=$(virsh -c qemu+tcp://#{cgi.params['host'].first}/system qemu-agent-command one-#{cgi.params['id'].first} '{\"execute\":\"guest-exec\", \"arguments\":{\"path\":\"/bin/systemctl\", \"arg\":[\"is-active\", \"xrdp\"], \"capture-output\":true}}'); output=$(echo $request); > /dev/null 2>&1; echo $?; echo $output"
    fork { exec("#{command_file}") }
    when 'get_xrdp_status'
    command_file="export USER=#{user}; request=$(virsh -c qemu+tcp://#{cgi.params['host'].first}/system qemu-agent-command one-#{cgi.params['id'].first} '{\"execute\":\"guest-exec-status\", \"arguments\":{\"pid\":#{cgi.params['pid'].first}}}'); output=$(echo $request); > /dev/null 2>&1; echo $?; echo $output"
    fork { exec("#{command_file}") }
    when 'get_ip_pid'
    command_file="export USER=#{user}; request=$(virsh -c qemu+tcp://#{cgi.params['host'].first}/system qemu-agent-command one-#{cgi.params['id'].first} '{\"execute\":\"guest-exec\", \"arguments\":{\"path\":\"/bin/ip\", \"arg\":[\"route\", \"get\", \"1.2.3.4\"], \"capture-output\":true}}'); output=$(echo $request); > /dev/null 2>&1; echo $?; echo $output"
    fork { exec("#{command_file}") }
    when 'get_ip'
    command_file="export USER=#{user}; request=$(virsh -c qemu+tcp://#{cgi.params['host'].first}/system qemu-agent-command one-#{cgi.params['id'].first} '{\"execute\":\"guest-exec-status\", \"arguments\":{\"pid\":#{cgi.params['pid'].first}}}'); output=$(echo $request); > /dev/null 2>&1; echo $?; echo $output"
    fork { exec("#{command_file}") }
    end
else
    print "-1"
end