1 # Copyright 2013 Intel Corporation
2 # Author: Artem Bityutskiy
5 # This file contains common functions for setup-ivi-* programs
8 __PROG="$PROG:-setup-ivi-sh-functions"
10 # Just a useful variable for the newline character(s)
16 IFS= printf "%s\n" "$PROG: error: $*" 1>&2
22 if [ -n "$verbose" ]; then
23 IFS= printf "%s\n" "$PROG (verbose): $*" >&2
27 # Finds out the OS name and sets the "$1" variable to the OS name upon exit.
30 local osrelease_path="$(installerfw_mnt_prefix "$__osrelease_file")"
32 # Make sure the OS release information file is present
33 [ -f "$osrelease_path" ] ||
34 fatal "the \"$osrelease_path\" file not found"
37 local __os_name="$(LC_ALL=C sed -n -e 's/^PRETTY_NAME="\(.*\)"$/\1/p' "$osrelease_path")"
38 [ -n "$__os_name" ] || \
39 fatal "cannot find \"PRETTY_NAME\" variable in \"$osrelease_path\""
41 if [ "${1:-%}" != "%" ]; then
43 verbose "get_os_name(): OS name: $1=$__os_name"
47 # Escape a string which is going to be used in a regexp. Shuould work for both
48 # sed and grep regexps.
53 printf "%s" "$regexp" | LC_ALL=C sed -e 's/[]\/()$*.^|[]/\\&/g'
56 # Escape a string which is going to be used at the "replacement" part of the
57 # sed "substitute" command (as in s/regexp/replacement/flags')
58 # Usage: esc_sed_replacement <replacement>
61 local replacement="$1";
63 printf "%s" "$replacement" | LC_ALL=C sed -e "s/[\&/]/\\&/g"
66 # Turn strings "abc" into "[Aa][Bb][Cc]" for case-insensitive matching in
67 # regular expressions.
68 case_insensitive_regexp()
72 printf "%s" "$regexp" | LC_ALL=C sed -e 's/[[:alpha:]]/[\U&\l&]/g'
75 # Check if dash is available and we are not running in dash
78 if command -v "dash" >/dev/null 2>&1; then
79 if [ -n "${BASH_VERSION:-}" ]; then
87 # Get the newest kernel, "$1" is the directory to search at, "$2" is an
88 # optional argument, and if it present, it tells which kernel should not be
89 # returned by this function.
92 local bootdir="$1"; shift
94 # Generate the list of installed kernels
95 local kernels="$(ls -1 "$bootdir" | LC_ALL=C grep "^vmlinuz-" | sort -r)"
97 # Exclude the unwanted kernel, if any
98 if [ -n "${1:-}" ]; then
99 local kernel="$(esc_regexp "$1")"
100 kernels="$(printf "%s" "$kernels" | LC_ALL=C grep -v "^$kernel$")"
103 printf "%s" "$kernels" | head -n1
106 # Remove all empty lines from the end of file, including lines which contain
107 # nothing but blanks (tabs and spaces).
108 remove_trailing_empty_lines()
112 LC_ALL=C sed -i -n -e '
113 :l # sed jump lable named "l"
114 /^[[:blank:]\n]*$/ { # matches multiple blank lines with any
115 # number of spaces or tabs
116 $d # if these are last lines, delete them
117 N; # otherwise append to the current pattern buf
120 /^[[:blank:]]*$/!p # print the pattern buffer for non-blank lines