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