File ExtraData of Package systemd-vboxinit

#!/usr/bin/env bash
# ============================================================================================ #
#: Title           : ExtraData                                                                 #
#: Sypnosis        : ExtraData                                                                 #
#: Date Created    : Thu Oct 17 17:47:24 2013 +0800  /  Thu Oct 17 09:48:11 2013 UTC           #
#: Last Edit       : Tue Apr 15 20:22:29 2025 +0800  /  Tue Apr 15 12:22:29 2025 UTC           #
#: License         : GPLv3                                                                     #
#: Version         : 1.0.2                                                                     #
#: Author          : Jason V. Ferrer '<jetchisel@gmail.com>'                                   #
#: Description     : Set extradata to auto or manual of VirtualBox's vms.                      #
#: Options         : NONE                                                                      #
#: Home Page       : https://github.com/Jetchisel/VBoxAutostart                                #
#: ExtComm         : clear,sleep,tput,VBoxManage                                               #
#: Copyright       : Jason V. Ferrer 2013-2025                                                 #
# ============================================================================================ #

# ============================================================================================ #
#     Reset the font to normal on quit and force exit/quit via ctrl-c and other signals.       #
# ============================================================================================ #

trap 'tput sgr0;echo' 0
trap 'exit 2;echo' 1 2 3 15

# ============================================================================================ #
#                  Set shell option to have multiple test and avoid regexp.                    #
# ============================================================================================ #

shopt -s extglob

# ============================================================================================ #
#                 list only the vms names and save it in an array listvms.                     #
# ============================================================================================ #

listvms=()

while read -u6 -r line; do
  Vmname=${line#*'"'}
  Vmname=${Vmname%'"'*}
  listvms+=("$Vmname")
done 6< <(VBoxManage list vms)

# ============================================================================================ #
#                                     The colors.                                              #
# ============================================================================================ #

gb=$(tput setaf 2 && tput bold)
reset=$(tput sgr0)

# ============================================================================================ #
#              Prints the vms with it's corresponding number within the array.                 #
# ============================================================================================ #

numbered=()
num=()

for vm in "${!listvms[@]}"; do
  num+=("$vm")
  numbered+=("${listvms[vm]/#/${gb}$vm${reset}) }")
done

# ============================================================================================ #
#                          Function to set all the vms to auto.                                #
# ============================================================================================ #

AutoAll() {
  for vm in "${!listvms[@]}"; do
    VBoxManage setextradata "${listvms[vm]}" pvbx/startupMode auto
  done
}

# ============================================================================================ #
#                         Function to set all the vms to manual.                               #
# ============================================================================================ #

ManualAll() {
  for vm in "${!listvms[@]}"; do
    VBoxManage setextradata "${listvms[vm]}" pvbx/startupMode manual
  done
}

# ============================================================================================ #
#                         Function to Get status of all the vms.                               #
# ============================================================================================ #

StatusAll() {

  status() {
    for vm in "${!listvms[@]}"; do
      VBoxManage getextradata "${listvms[vm]}" pvbx/startupMode
    done
  }

  mapfile -t stats < <(status)

  for vm in "${!listvms[@]}"; do
    printf '%s\n' "VirtualMachine[\"${listvms[vm]}\"]=${stats[vm]#*: }"
  done
}

# ============================================================================================ #
#       Function to pause so user has the time to review the output and ask to coninue.        #
# ============================================================================================ #

paused() {
  while :; do
    printf "\n\n"
    printf -v prompt "Press [c] to continue. "
    read -r -e -p "$prompt" -n1
    case $REPLY in
      [Cc]) clear
            return 0
# shellcheck disable=SC2317
            clear;;
         *) printf "%s\n" "Invalid answer!" >&2
    esac
  done
}

# ============================================================================================ #
#                            Function to print an invalid message.                             #
# ============================================================================================ #

InValid() {
  # printf "\n%s" "Invalid option!" >&2
  printf "\nInvalid option!" >&2
}

# ============================================================================================ #
#       Function to wait for 1 second so user can see the output and clear the screen.         #
# ============================================================================================ #

Wait(){
  sleep 1
  clear
}

# ============================================================================================ #
#              Format the numbers in the array so it can be tested  in one line.               #
# ============================================================================================ #
# shellcheck disable=SC2145
printf -v new "%s|" "@(${num[@]})"
new=${new%|}

# ============================================================================================ #
#       The Header with the Message in between the LongBar. Formatted with "printf -v".        #
# ============================================================================================ #

ColoredChoice=${gb}NUMBER${reset}/${gb}LETTER${reset}

Message="|            PRESS THE $ColoredChoice OF THE CHOICES BELOW           |"
LongBar='+===================================================================+'

printf -v Header '%s\n' "$LongBar" "$Message" "$LongBar"

# ============================================================================================ #
#          The main menu when the script is started. (Bold fonts up to the submenus)           #
# ============================================================================================ #

BlankSpace=

Main_Menu=(
  "$Header"
  "${gb}1${reset}) SET VMS TO AUTO"
  "${gb}2${reset}) SET VMS TO MANUAL"
  "${gb}3${reset}) CHECK STATUS OF VMS"
  "${gb}Q${reset}) EXIT"
  "$BlankSpace"
  "(EnterInput) =>"
)

printf -v MainMenu '%s\n' "${Main_Menu[@]}"
MainMenu=${MainMenu%$'\n'}

# ============================================================================================ #
#                          Additional options for the submenus.                                #
# ============================================================================================ #

Other_Options=(
  "${gb}A${reset}) SELECT All VMS"
  "${gb}R${reset}) RETURN TO MAIN MENU"
  "${gb}Q${reset}) EXIT"
)

printf -v OtherOptions '%s\n' "${Other_Options[@]}"

# ============================================================================================ #
#                                     The SubMenus                                             #
# ============================================================================================ #

SubMenuAuto=(
  "$Header"
  "${numbered[@]}"
  "${OtherOptions}"
  "$BlankSpace"
  "(SetVmsToAuto) =>"
)

SubMenuManual=(
  "$Header"
  "${numbered[@]}"
  "${OtherOptions}"
  "$BlankSpace"
  "(SetVmsToManual) =>"
)

SubMenuStatus=(
  "$Header"
  "${numbered[@]}"
  "${OtherOptions}"
  "$BlankSpace"
  "(CheckVmsStatus) =>"
)

# ============================================================================================ #
#       Format the Menu's to a variable using printf -v so read can present it properly.       #
# ============================================================================================ #

printf -v sub_menu_auto '%s\n' "${SubMenuAuto[@]}"
sub_menu_auto=${sub_menu_auto%$'\n'}

printf -v sub_menu_manual '%s\n' "${SubMenuManual[@]}"
sub_menu_manual=${sub_menu_manual%$'\n'}

printf -v sub_menu_status '%s\n' "${SubMenuStatus[@]}"
sub_menu_status=${sub_menu_status%$'\n'}

# ============================================================================================ #
#           If vms is more than 9, one click button is disabled in the SubMenu's.              #
# ============================================================================================ #

OneClick() {
  if (( ${#listvms[@]} < 10  )); then
    read -r -e -p "$1" -n1
  elif (( ${#listvms[@]} > 9 )); then
    read -r -e -p "$1"
  fi
}

VmsTotalAuto() {
  OneClick "$sub_menu_auto "
}

VmsTotalManual() {
  OneClick "$sub_menu_manual "
}

VmsTotalStatus() {
  OneClick "$sub_menu_status "
}

# ============================================================================================ #
#                               Function to set vms to auto.                                   #
# ============================================================================================ #

Auto() {
  clear
  while true; do
    VmsTotalAuto
    # shellcheck disable=SC2053
     if  [[ ${REPLY} = $new ]]; then
       VBoxManage setextradata "${listvms[${REPLY}]}"  pvbx/startupMode auto
       printf -v single "[\"%s\"] " "${listvms[${REPLY}]}"
       printf '\n%s\n' "VirtualMachine $single is now on AUTO."
       paused
     elif [[ ${REPLY} = [Aa] ]]; then
       AutoAll
       printf -v all "[\"%s\"]\n" "${listvms[@]}"
       printf '%s\n' "$BlankSpace" "VirtualMachine(s):" "$BlankSpace" "$all" "Is now on AUTO."
       paused
     elif [[ ${REPLY} = [Rr] ]]; then
       return 0
     elif [[ ${REPLY} = [Qq] ]]; then
       clear
       exit 0
     else
       InValid
       Wait
     fi
  done
}

# ============================================================================================ #
#                                Function to set vms to manual.                                #
# ============================================================================================ #

Manual() {
  clear
  while true; do
    VmsTotalManual
      # shellcheck disable=SC2053
      if  [[ ${REPLY} = $new ]]; then
        VBoxManage setextradata "${listvms[${REPLY}]}"  pvbx/startupMode manual
        printf -v single "[\"%s\"] " "${listvms[${REPLY}]}"
        printf '\n%s\n' "VirtualMachine $single is now on MANUAL."
        paused
      elif [[ ${REPLY} = [Aa] ]]; then
        ManualAll
        printf -v all "[\"%s\"]\n"  "${listvms[@]}"
        printf '%s\n' "$BlankSpace" "VirtualMachine(s):" "$BlankSpace" "$all" "is now on MANUAL."
        paused
      elif [[ ${REPLY} = [Rr] ]]; then
        return 0
       # shellcheck disable=SC2317
        clear
      elif [[ ${REPLY} = [Qq] ]]; then
        clear
        exit 0
      else
        InValid
        Wait
      fi
  done
}

# ============================================================================================ #
#                            Function to check status of the vms.                              #
# ============================================================================================ #

Status() {
  clear
   while true; do
     VmsTotalStatus
       # shellcheck disable=SC2053
       if  [[ ${REPLY} = $new ]]; then
         result=$(VBoxManage getextradata "${listvms[${REPLY}]}"  pvbx/startupMode)
         printf '\n%s\n' "VirtualMachine[\"${listvms[${REPLY}]}\"]=${result#*: }"
         paused
       elif [[ ${REPLY} = [Aa] ]]; then
         echo
         StatusAll
         paused
       elif [[ ${REPLY} = [Rr] ]]; then
         return 0
      # shellcheck disable=SC2317
         clear
       elif [[ ${REPLY} = [Qq] ]]; then
         clear
         exit 0
       else
         InValid
         Wait
       fi
   done
}

# ============================================================================================ #
#                                  Parsing the main menu.                                      #
# ============================================================================================ #

while true; do
  clear
  read -r -e -p "${MainMenu} " -n1
    case ${REPLY} in
       1) Auto  ;;
       2) Manual
          clear ;;
       3) Status
          clear ;;
    [Qq]) clear
          exit 0;;
       *) InValid
          Wait
          clear ;;
    esac
done

# ============================================================================================ #
#                                   '>> End Of ExtraData <<'                                   #
# ============================================================================================ #
openSUSE Build Service is sponsored by