aboutsummaryrefslogtreecommitdiff
path: root/pxeflatten
blob: 225047b5919b272078d4fa2471bc832d2854d277 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#! /usr/bin/env bash

# Flatten a multi-line PXELINUX config file ignoring comments/blank lines.
# Can also be used for GRUB config files (note that GRUB rewrites x="y" as
# "x=y").
#
# The text that appears after the 'append' clause in PXELINUX config files
# must all be on a single line. There is no support even for line
# continuation. This makes managing complex configurations a pain.
#
# To help with this situation, this script will take a milti-line file (or
# stdin if none is specified) and write flattened, single line output into
# stdout ignoring #-comments and blank lines while at it. Note that this
# script prints a leading space.
#
# If you have prepared your PXELINUX config file so that the 'append' clause
# is the last line without a trailing newline (and without a trailing space),
# then you can do:
#
# pxeflatten pxeconfig >>pxelinux.cfg/default
#
# Options:
#
# -n
#   Don't write trailing newline (useful for appending multiple configs).
#
usage="usage: $0 [-n] [<file>]"

owd="$(pwd)"
trap "{ cd '$owd'; exit 1; }" ERR
set -o errtrace # Trap in functions.

function info () { echo "$*" 1>&2; }
function error () { info "$*"; exit 1; }

nl=true

while [ "$#" -gt 0 ]; do
  case "$1" in
    -n)
      nl=
      shift
      ;;
    -*)
      error "unknown option: $1"
      ;;
    --)
      shift
      break
      ;;
    *)
      break
      ;;
  esac
done

if [ -z "$1" ]; then
  input="-"
else
  input="$1"
fi

l=
while read l || [ -n "$l" ]; do
  echo -n " $l"
done < <(sed -e '/^\s*#/d;/^\s*$/d;s/\s\s*/ /g' "$input")

# Final newline.
#
if [ "$nl" ]; then
  echo
fi