| 1 | #!/bin/bashscip-shell filesystem . . `/bin/bash`. |
| 2 | # Author: Steven Shiau <steven _at_ clonezilla org> |
| 3 | # License: GPL |
| 4 | # Description: switch the PXE menu in the config file |
| 5 | |
| 6 | # Load DRBL setting and functions |
| 7 | DRBL_SCRIPT_PATH="${DRBL_SCRIPT_PATH:-/usr/share/drbl}" |
| 8 | |
| 9 | . $DRBL_SCRIPT_PATH/sbin/drbl-conf-functions |
| 10 | |
| 11 | # |
| 12 | # DIA (="dialog") is loaded from drbl.conf |
| 13 | PXE_CONF_DEF="$PXELINUX_DIR/default" |
| 14 | |
| 15 | # functions |
| 16 | clean_tmpscip-shell shell shell-project 0.0.0 clean_tmp().() { |
| 17 | [scip-shell system . . `[`. -f "$TMP" ] && rmscip-shell system . . rm. -f $TMP |
| 18 | [scip-shell system . . `[`. -f "$PXE_CONF_TMP" ] && rmscip-shell system . . rm. -f $PXE_CONF_TMP |
| 19 | } |
| 20 | |
| 21 | # |
| 22 | USAGEscip-shell shell shell-project 0.0.0 USAGE().() { |
| 23 | echoscip-shell system . . echo. "Usage:" |
| 24 | echoscip-shell system . . echo. "To hide, reveal or set default PXE client menu:" |
| 25 | echoscip-shell system . . echo. "`basenamescip-shell system . . basename. $0` [OPTION] [hide|reveal|setdefault]" |
| 26 | echoscip-shell system . . echo. "Options:" |
| 27 | language_help_prompt_by_idx_no |
| 28 | echoscip-shell system . . echo. "-i, --menu MENU Assign MENU to be processed" |
| 29 | echoscip-shell system . . echo. "-c, --config CONF use the CONF file instead of default one ($PXE_CONF_DEF)" |
| 30 | dialog_like_prog_help_prompt |
| 31 | echoscip-shell system . . echo. "-o, --output-result-file FILE Output the selecting results to the FILE. It includes the results of action and image_selected." |
| 32 | echoscip-shell system . . echo. "-v, --verbose show verbose messages" |
| 33 | echoscip-shell system . . echo. "-h, --help display this help and exit" |
| 34 | echoscip-shell system . . echo. |
| 35 | echoscip-shell system . . echo. "EXAMPLE" |
| 36 | echoscip-shell system . . echo. " To hide the menu 'local' and 'memtest', you can run:" |
| 37 | echoscip-shell system . . echo. " $0 -i 'local memtest' hide" |
| 38 | } |
| 39 | |
| 40 | select_imagescip-shell shell shell-project 0.0.0 select_image().() { |
| 41 | all_label="$(awkscip-shell system . . awk. '/^[[:space:]]*label[[:space:]]+.*([[:space:]]|$)+/ {print $2}scip-shell filesystem . . `/^[[:space:]]*label[[:space:]]+.*([[:space:]]|$)+/ {print $2}`.' $PXE_CONF_TMP)" |
| 42 | [scip-shell system . . `[`. -n "$VERBOSE" ] && echoscip-shell system . . echo. "all_label: $all_label" |
| 43 | if [scip-shell system . . `[`. -z "$all_label" ]; then |
| 44 | $DIA --title "No PXE image found!" --clear \ |
| 45 | --msgbox "I can NOT find any PXE image in $PXE_CONF! \nMake sure you already setup DRBL server properly!" 0 0 |
| 46 | exit 0 |
| 47 | fi |
| 48 | |
| 49 | MENU="" |
| 50 | for i in $all_label; do |
| 51 | # EX for i: rh-8.0-netinstall |
| 52 | # get the description for the netinstall image from pxelinux default |
| 53 | lines="$(get_pxecfg_image_block $i $PXE_CONF_TMP)" |
| 54 | begin_line="$(echoscip-shell system . . echo. $lines | awkscip-shell system . . awk. -F" " '{print $1}')" |
| 55 | end_line="$(echoscip-shell system . . echo. $lines | awkscip-shell system . . awk. -F" " '{print $2}')" |
| 56 | search_cmd="if ($begin_line..$end_line) {print}" |
| 57 | des="$(LC_ALL=C perlscip-shell system . . perl. -n -e "$search_cmd" $PXE_CONF_TMP | grepscip-shell system . . grep. -i "^[[:space:]]*MENU LABEL" | sedscip-shell system . . sed. -e "s/^[[:space:]]*MENU LABEL //gi" -e "s/ /_/g")" |
| 58 | |
| 59 | case "$action" in |
| 60 | "hide"|"setdefault") |
| 61 | grep_cmd="^[[:space:]]*MENU HIDE" |
| 62 | ;; |
| 63 | "reveal") |
| 64 | grep_cmd="^[[:space:]]*[#]+[[:space:]]+MENU HIDE" |
| 65 | ;; |
| 66 | esac |
| 67 | add_menu="$(LC_ALL=C perlscip-shell system . . perl. -n -e "$search_cmd" $PXE_CONF_TMP | grepscip-shell system . . grep. -Ei "$grep_cmd")" |
| 68 | if [scip-shell system . . `[`. -z "$add_menu" ]; then |
| 69 | MENU="$MENU $i $des off " |
| 70 | fi |
| 71 | done |
| 72 | |
| 73 | # check if we can find any target menu |
| 74 | if [scip-shell system . . `[`. -z "$MENU" ]; then |
| 75 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 76 | echoscip-shell system . . echo. "Error!!! No any unhidden menu was found!" |
| 77 | echoscip-shell system . . echo. "Maybe they are all unhidden!" |
| 78 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 79 | clean_tmpscip-shell shell shell-project 0.0.0 clean_tmp(). |
| 80 | echoscip-shell system . . echo. "$msg_program_stop" |
| 81 | exit 1 |
| 82 | fi |
| 83 | |
| 84 | # |
| 85 | eval title="\$msg_title_${action}" |
| 86 | eval list_des="\$msg_${action}_list_des" |
| 87 | |
| 88 | case "$action" in |
| 89 | "hide"|"reveal") |
| 90 | $DIA \ |
| 91 | --backtitle "$msg_nchc_title" \ |
| 92 | --title "$title" \ |
| 93 | --separate-output \ |
| 94 | --checklist "$list_des ($msg_press_space_to_mark_selection)" 0 80 0 \ |
| 95 | $MENU 2> $TMP |
| 96 | image_selected="$(catscip-shell system . . cat. $TMP)" |
| 97 | ;; |
| 98 | "setdefault") |
| 99 | # Here we use --radiolist so that the format of MENU is compatible with --checklist |
| 100 | $DIA \ |
| 101 | --backtitle "$msg_nchc_title" \ |
| 102 | --title "$title" \ |
| 103 | --radiolist "$list_des" 0 80 0 \ |
| 104 | $MENU 2> $TMP |
| 105 | image_selected="$(catscip-shell system . . cat. $TMP)" |
| 106 | ;; |
| 107 | esac |
| 108 | [scip-shell system . . `[`. -f "$TMP" ] && rmscip-shell system . . rm. -f $TMP |
| 109 | [scip-shell system . . `[`. -z "$image_selected" ] && exit 1 |
| 110 | [scip-shell system . . `[`. -n "$VERBOSE" ] && echoscip-shell system . . echo. "image_selected: $image_selected" |
| 111 | } # end of select_image |
| 112 | |
| 113 | select_hide_reveal_setdefaultscip-shell shell shell-project 0.0.0 select_hide_reveal_setdefault().() { |
| 114 | TMP=$(mktempscip-shell system . . mktemp. /tmp/menu.XXXXXXscip-shell filesystem . . `/tmp/menu.XXXXXX`.) |
| 115 | $DIA \ |
| 116 | --backtitle "$msg_nchc_title" \ |
| 117 | --title "$msg_hide_reveal" \ |
| 118 | --menu "$msg_choose_the_action:" 0 60 0 \ |
| 119 | "hide" "$msg_hide_pxe_menus" \ |
| 120 | "reveal" "$msg_reveal_pxe_menus" \ |
| 121 | "setdefault" "$msg_setdefault_pxe_menu" \ |
| 122 | 2> $TMP |
| 123 | action=$(catscip-shell system . . cat. $TMP) |
| 124 | [scip-shell system . . `[`. -f "$TMP" ] && rmscip-shell system . . rm. -f $TMP |
| 125 | [scip-shell system . . `[`. -z "$action" ] && echoscip-shell system . . echo. "No action selected! Program terminated!!!" && exit 1 |
| 126 | } # end of select_hide_reveal_setdefault |
| 127 | |
| 128 | image_selected="" |
| 129 | # Parse command-line options |
| 130 | while [scip-shell system . . `[`. $# -gt 0 ]; do |
| 131 | case "$1" in |
| 132 | -l|--language) |
| 133 | shift |
| 134 | if [scip-shell system . . `[`. -z "$(echoscip-shell system . . echo. $1 |grepscip-shell system . . grep. ^-.)" ]; then |
| 135 | # skip the -xx option, in case |
| 136 | specified_lang="$1" |
| 137 | shift |
| 138 | fi |
| 139 | ;; |
| 140 | -i|--image) |
| 141 | shift |
| 142 | # skip the -xx option, in case |
| 143 | if [scip-shell system . . `[`. -z "$(echoscip-shell system . . echo. $1 |grepscip-shell system . . grep. ^-.)" ]; then |
| 144 | image_selected="$image_selected $1" |
| 145 | shift |
| 146 | fi |
| 147 | ;; |
| 148 | -c|--config) |
| 149 | shift |
| 150 | # skip the -xx option, in case |
| 151 | if [scip-shell system . . `[`. -z "$(echoscip-shell system . . echo. $1 |grepscip-shell system . . grep. ^-.)" ]; then |
| 152 | PXE_CONF="$1" |
| 153 | shift |
| 154 | fi |
| 155 | ;; |
| 156 | -o|--output-result-file) |
| 157 | shift |
| 158 | # skip the -xx option, in case |
| 159 | if [scip-shell system . . `[`. -z "$(echoscip-shell system . . echo. $1 |grepscip-shell system . . grep. ^-.)" ]; then |
| 160 | RESULT_FILE="$1" |
| 161 | shift |
| 162 | fi |
| 163 | ;; |
| 164 | -h|--help) |
| 165 | USAGEscip-shell shell shell-project 0.0.0 USAGE(). >& 2 |
| 166 | exit 2 ;; |
| 167 | -d0|--dialog) DIA="dialog"; shift;; |
| 168 | -d1|--Xdialog) DIA="Xdialog"; shift;; |
| 169 | -d2|--whiptail) DIA="whiptail"; shift;; |
| 170 | -d3|--gdialog) DIA="gdialog"; shift;; |
| 171 | -d4|--kdialog) DIA="kdialog"; shift;; |
| 172 | -v|--verbose) VERBOSE="on"; shift;; |
| 173 | -*) echoscip-shell system . . echo. "${0}: ${1}: invalid option" >&2 |
| 174 | USAGEscip-shell shell shell-project 0.0.0 USAGE(). >& 2 |
| 175 | exit 2 ;; |
| 176 | *) break ;; |
| 177 | esac |
| 178 | done |
| 179 | # |
| 180 | ask_and_load_lang_set $specified_lang |
| 181 | |
| 182 | # check DIA |
| 183 | check_DIA_set_ESC $DIA |
| 184 | |
| 185 | # |
| 186 | action="$1" |
| 187 | [scip-shell system . . `[`. -z "$action" ] && select_hide_reveal_setdefaultscip-shell shell shell-project 0.0.0 select_hide_reveal_setdefault(). |
| 188 | [scip-shell system . . `[`. -z "$PXE_CONF" ] && PXE_CONF=$PXE_CONF_DEF |
| 189 | |
| 190 | # |
| 191 | TMP=$(mktempscip-shell system . . mktemp. /tmp/menu.XXXXXXscip-shell filesystem . . `/tmp/menu.XXXXXX`.) |
| 192 | PXE_CONF_TMP="$(mktempscip-shell system . . mktemp. /tmp/pxecfg.XXXXXXscip-shell filesystem . . `/tmp/pxecfg.XXXXXX`.)" |
| 193 | |
| 194 | # copy the original pxe_conf to be a template |
| 195 | if [scip-shell system . . `[`. ! -f "$PXE_CONF" ]; then |
| 196 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 197 | echoscip-shell system . . echo. -e "Unable to find any PXE config file! \nMake sure you already setup DRBL server properly!" |
| 198 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 199 | clean_tmpscip-shell shell shell-project 0.0.0 clean_tmp(). |
| 200 | exit 1 |
| 201 | fi |
| 202 | cpscip-shell system . . cp. -f $PXE_CONF $PXE_CONF_TMP |
| 203 | |
| 204 | [scip-shell system . . `[`. -z "$image_selected" ] && select_imagescip-shell shell shell-project 0.0.0 select_image(). |
| 205 | # Make $image_selected in one line |
| 206 | image_selected="$(echoscip-shell system . . echo. $image_selected)" |
| 207 | |
| 208 | # hide or reveal the specified menu |
| 209 | case "$action" in |
| 210 | "hide") |
| 211 | for img in $image_selected; do |
| 212 | echoscip-shell system . . echo. "Hide the label \"$img\"." |
| 213 | hide_reveal_pxe_img $img hide $PXE_CONF_TMP |
| 214 | done |
| 215 | ;; |
| 216 | "reveal") |
| 217 | for img in $image_selected; do |
| 218 | echoscip-shell system . . echo. "Reveal the label \"$img\"." |
| 219 | hide_reveal_pxe_img $img reveal $PXE_CONF_TMP |
| 220 | done |
| 221 | ;; |
| 222 | "setdefault") |
| 223 | echoscip-shell system . . echo. "Set the default label \"$image_selected\"." |
| 224 | set-default-pxe-img -i $image_selected -c $PXE_CONF_TMP |
| 225 | ;; |
| 226 | esac |
| 227 | # check if all menu is hidden!!! We can not do that! |
| 228 | if ! grepscip-shell system . . grep. -qiE "[[:space:]]*[#]+[[:space:]]*MENU[[:space:]]HIDE" $PXE_CONF_TMP; then |
| 229 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 230 | echoscip-shell system . . echo. "$msg_error!!! $msg_you_can_not_hide_all_pxe_menus!!!" |
| 231 | echoscip-shell system . . echo. "$msg_program_stop!!! $msg_no_modification!" |
| 232 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 233 | clean_tmpscip-shell shell shell-project 0.0.0 clean_tmp(). |
| 234 | exit 1 |
| 235 | fi |
| 236 | |
| 237 | # |
| 238 | [scip-shell system . . `[`. -n "$VERBOSE" ] && echoscip-shell system . . echo. "Copy the template file $PXE_CONF_TMP to actual one $PXE_CONF" |
| 239 | cpscip-shell system . . cp. -f $PXE_CONF_TMP $PXE_CONF |
| 240 | cp_rc="$?" |
| 241 | [scip-shell system . . `[`. -f "$PXE_CONF_TMP" ] && rmscip-shell system . . rm. -f $PXE_CONF_TMP |
| 242 | |
| 243 | # Write settings for later use |
| 244 | if [scip-shell system . . `[`. "$cp_rc" -eq 0 -a -n "$RESULT_FILE" ]; then |
| 245 | echoscip-shell system . . echo. "Output action and selected menus to $RESULT_FILE." |
| 246 | catscip-shell system . . cat. <<-RESULT_END > $RESULT_FILE |
| 247 | action="$action" |
| 248 | image_selected="$image_selected" |
| 249 | RESULT_END |
| 250 | fi |
| 251 | |
| 252 | exit 0 |