#!/bin/sh

alterator_api_version=1

. alterator-sh-functions

write_string_params(){
  while read key val; do
    write_string_param "${key%:}" "${val}"
  done
}

on_message() {
  case "$in_action" in
    type)
    ;;
    list)
      write_enum_item "0" "`_ "No image"`"
      run_localized alterator-netinst -l | write_enum
    ;;
    read)
      if [ "$in__objects" = "wait" ]; then
        run_localized alterator-netinst -S | write_string_params
        return
      fi
      local curr="$(alterator-netinst -c)"
      write_string_param "image" "$curr"
      write_string_param "get_from" "cd"
      run_localized alterator-netinst -r "$curr" | write_string_params
    ;;
    write)
      if [ -n "$in_cancel" ]; then
        run_localized alterator-netinst -D
      elif [ -n "$in_sel" ]; then
        run_localized alterator-netinst -s "$in_image"
      elif [ -n "$in_del" ]; then
        run_localized alterator-netinst -d "$in_image"
      elif [ -n "$in_add" ]; then
        [ "$in_get_from" = "url" ] &&
          run_localized alterator-netinst -A "$in_add_url" ||
          run_localized alterator-netinst -A "cdrom:"
      fi
    ;;
  esac
}

message_loop
