summaryrefslogtreecommitdiffstats
path: root/modules-available/serversetup-bwlp-ipxe/inc/scriptbuilderbash.inc.php
diff options
context:
space:
mode:
Diffstat (limited to 'modules-available/serversetup-bwlp-ipxe/inc/scriptbuilderbash.inc.php')
-rw-r--r--modules-available/serversetup-bwlp-ipxe/inc/scriptbuilderbash.inc.php97
1 files changed, 97 insertions, 0 deletions
diff --git a/modules-available/serversetup-bwlp-ipxe/inc/scriptbuilderbash.inc.php b/modules-available/serversetup-bwlp-ipxe/inc/scriptbuilderbash.inc.php
new file mode 100644
index 00000000..86b2931f
--- /dev/null
+++ b/modules-available/serversetup-bwlp-ipxe/inc/scriptbuilderbash.inc.php
@@ -0,0 +1,97 @@
+<?php
+
+class ScriptBuilderBash extends ScriptBuilderBase
+{
+
+ public function output($string)
+ {
+ echo $string;
+ }
+
+ public function bootstrapLive() { return false; }
+
+ public function getMenu(IPxeMenu $menu, bool $bootstrap)
+ {
+ return $this->menuToScript($menu);
+ }
+
+ public function getBootEntry($entry)
+ {
+ if (!$entry) {
+ return "echo 'Invalid boot entry id'\nread -n1 -r _\n";
+ }
+ return $entry->toScript($this);
+ }
+
+ public function getMenuEntry($entry, $honorPassword = true)
+ {
+ if ($entry === null)
+ return "echo 'Invalid menu entry id - press any key to continue'\nread -n1 -r _\n";
+ return $entry->getBootEntryScript($this);
+ }
+
+ public function getSpecial($special)
+ {
+ return ''; // We can't really do localboot here I guess
+ }
+
+ public function menuToScript($menu)
+ {
+ $output = "declare -A items_name items_gap hotkey_item\ndeclare menu_default menu_timeout menu_title\n";
+ foreach ($menu->items as $entry) {
+ $id = $entry->menuentryid;
+ if ($entry->bootEntry === null || (!empty($this->platform) && !$entry->bootEntry->supportsMode($this->platform)))
+ continue;
+ if (!$entry->hidden) {
+ $output .= 'items_name[' . $id . ']=' . $this->bashString($entry->title) . "\n";
+ if ($entry->gap) {
+ $output .= 'items_gap[' . $id . "]=1\n";
+ }
+ }
+ if ($entry->hotkey !== false) {
+ $output .= 'hotkey_item[' . $entry->hotkey . ']=' . $id . "\n";
+ }
+ if ($id == $menu->defaultEntryId) {
+ $output .= "menu_default={$id}\n";
+ }
+ }
+ return $output . "menu_timeout=" . $menu->timeoutMs
+ . "\nmenu_title=" . $this->bashString($menu->title) . "\n";
+ }
+
+ public function execDataToScript($agnostic, $bios, $efi) : string
+ {
+ if ($agnostic !== null)
+ return $this->execDataToScriptInternal($agnostic);
+ if ($bios !== null && $this->platform === BootEntry::BIOS)
+ return $this->execDataToScriptInternal($bios);
+ if ($efi !== null && $this->platform === BootEntry::EFI)
+ return $this->execDataToScriptInternal($efi);
+ return $this->execDataToScriptInternal($bios ?? $efi ?? new ExecData());
+ }
+
+ private function execDataToScriptInternal(ExecData $entry) : string
+ {
+ $entry->sanitize();
+ $script = "declare -a initrd\ndeclare kernel kcl\n";
+ if (!empty($entry->initRd)) {
+ foreach ($entry->initRd as $initrd) {
+ if (empty($initrd))
+ continue;
+ $script .= 'initrd+=( ' . $this->bashString($initrd) . " )\n";
+ }
+ }
+ $script .= 'kernel=' . $this->bashString($entry->executable) . "\n";
+ $script .= 'kcl="' . str_replace('"', '"\\""', $entry->commandLine) . "\"\n"; // Allow expansion
+ return $script;
+ }
+
+ private function bashString($string)
+ {
+ if (strpos($string, "'") === false) {
+ return "'$string'";
+ }
+ return "'" . str_replace("'", "'\\''", $string) . "'";
+ }
+
+}