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