#!/bin/sh
#
# linbo_cmd: legacy command wrapper for the linbo_gui
#
# thomas@linuxmuster.net
# 20250801
# GPL v3
#

prefix="/usr/bin/linbo_"

usage(){
  echo
  echo "Usage: linbo_cmd <command> <options>"
  echo
  echo "Commands are:"
  ls -1 ${prefix}* | sed "s|^${prefix}| |g" | grep -v cmd | grep -v wrapper
  echo
  echo "This is only a legacy wrapper for the linbo_gui."
  echo "Please run the real commands directly:"
  echo
  echo "linbo_<command> help"
  echo
  exit 0
}

remove_args(){
  local item
  for item in $@; do shift; done
}

[ -z "$1" ] && usage

# get command, substitute outdated commands for legacy reasons
case "$1" in
  authenticate) cmd="auth" ;;
  create) cmd="create_image" ;;
  label) cmd="$1" ; options="all" ;;
  memory) cmd="mem" ;;
  partition) cmd="partition_format" ; remove_args ;;
  partition_noformat) cmd="partition" ; remove_args ;;
  sync|syncr) cmd="syncstart" ;;
  synconly) cmd="sync" ;;
  cmd|wrapper) usage ;;
  size)
    # workaround for linbo_gui's wrong invokation of linbo_cmd size
    case "$2" in
      /dev/nvmenp) options="/dev/nvme0n1" ;;
      /dev/diskp) options="/dev/disk0" ;;
      *) ;;
    esac
    cmd="$1"
  ;;
  *) cmd="$1" ;;
esac
shift

# test real command
realcmd="$prefix${cmd}"
[ -x "$realcmd" ] || usage

# get options
[ -z "$options" ] && options="$@"

# run real command
$realcmd $options

exit "$?"
