installerfw-sh-functions: fix installerfw_mnt_prefix
[platform/adaptation/setup-scripts.git] / setup-ivi-sh-functions
1 # Copyright 2013 Intel Corporation
2 # Author: Artem Bityutskiy
3 # License: GPLv2
4
5 # This file contains common functions for setup-ivi-* programs
6
7 # Own name
8 __PROG="$PROG:-setup-ivi-sh-functions"
9
10 fatal()
11 {
12         IFS= printf "%s\n" "$PROG: error: $*" 1>&2
13         exit 1
14 }
15
16 verbose()
17 {
18         if [ -n "$verbose" ]; then
19                 IFS= printf "%s\n" "$PROG (verbose): $*" >&2
20         fi
21 }
22
23 # Finds out the OS name and sets the "$1" variable to the OS name upon exit.
24 get_os_name()
25 {
26         local osrelease_path="$(installerfw_mnt_prefix "$__osrelease_file")"
27
28         # Make sure the OS release information file is present
29         [ -f "$osrelease_path" ] ||
30                 fatal "the \"$osrelease_path\" file not found"
31
32         # Get the OS name
33         local __os_name="$(LC_ALL=C sed -n -e 's/^PRETTY_NAME="\(.*\)"$/\1/p' "$osrelease_path")"
34         [ -n "$__os_name" ] || \
35                 fatal "cannot find \"PRETTY_NAME\" variable in \"$osrelease_path\""
36
37         if [ "${1:-%}" != "%" ]; then
38                 eval $1="\$__os_name"
39                 verbose "get_os_name(): OS name: $1=$__os_name"
40         fi
41 }
42
43 # Escape a string which is going to be used in a regexp. Shuould work for both
44 # sed and grep regexps.
45 esc_regexp()
46 {
47         local regexp="$1";
48
49         printf "%s" "$regexp" | LC_ALL=C sed -e 's/[]\/()$*.^|[]/\\&/g'
50 }
51
52 # Escape a string which is going to be used at the "replacement" part of the
53 # sed "substitute" command (as in s/regexp/replacement/flags')
54 # Usage: esc_sed_replacement <replacement>
55 esc_sed_replacement()
56 {
57         local replacement="$1";
58
59         printf "%s" "$replacement" | LC_ALL=C sed -e "s/[\&/]/\\&/g"
60 }
61
62 # Turn strings "abc" into "[Aa][Bb][Cc]" for case-insensitive matching in
63 # regular expressions.
64 case_insensitive_regexp()
65 {
66         local regexp="$1"
67
68         printf "%s" "$regexp" | LC_ALL=C sed -e 's/[[:alpha:]]/[\U&\l&]/g'
69 }
70
71 # Check if dash is available and we are not running in dash
72 can_switch_to_dash()
73 {
74         if command -v "dash" >/dev/null 2>&1; then
75                 if [ -n "${BASH_VERSION:-}" ]; then
76                         return 0
77                 fi
78         fi
79
80         return 1
81 }
82
83 # Get the newest kernel, "$1" is the directory to search at, "$2" is an
84 # optional argument, and if it present, it tells which kernel should not be
85 # returned by this function.
86 get_newest_kernel()
87 {
88         local bootdir="$1"; shift
89
90         # Generate the list of installed kernels
91         local kernels="$(ls -1 "$bootdir" | LC_ALL=C grep "^vmlinuz-" | sort -r)"
92
93         # Exclude the unwanted kernel, if any
94         if [ -n "${1:-}" ]; then
95                 local kernel="$(esc_regexp "$1")"
96                 kernels="$(printf "%s" "$kernels" | LC_ALL=C grep -v "^$kernel$")"
97         fi
98
99         printf "%s" "$kernels" | head -n1
100 }
101
102 # Remove all empty lines from the end of file, including lines which contain
103 # nothing but blanks (tabs and spaces).
104 remove_trailing_empty_lines()
105 {
106         local file="$1"
107
108         LC_ALL=C sed -i -n -e '
109             :l                   # sed jump lable named "l"
110             /^[[:blank:]\n]*$/ { # matches multiple blank lines with any
111                                  # number of spaces or tabs
112                  $d              # if these are last lines, delete them
113                  N;              # otherwise append to the current pattern buf
114                  bl              # and start over
115             }
116             /^[[:blank:]]*$/!p   # print the pattern buffer for non-blank lines
117             ' "$file"
118 }