-
Notifications
You must be signed in to change notification settings - Fork 3
/
patch.sh
executable file
·99 lines (85 loc) · 3.37 KB
/
patch.sh
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/bin/bash
project_root="$PWD"
# Source the utils.sh file
source "$project_root/TOOLS/helpers/utils.sh" "$project_root"
# the default options file
optionsfile="options.cfg"
# the result of installed options log
installed_options="installed_options.log"
# check the parameters for a custom options file
if [ $# == 1 ]; then
optionsfile="$1"
if [ ! -f "$optionsfile" ]; then
optionsfile="${optionsfile}.cfg"
fi
fi
# check the options file
if [ ! -f "$optionsfile" ]; then
echo -e "${RED}ERROR: Cannot find the '$optionsfile' file ${NC}"
exit 1
fi
check_tools "awk head sed"
# remove the old result file for the installed options
rm -f "$installed_options"
# parse the enabled options that have a set value
options=$(awk -F '=' '{if (! ($0 ~ /^;/) && ! ($0 ~ /^#/) && ! ($0 ~ /^$/) && ! ($2 == "")) print $1}' "$optionsfile")
# for each enabled option
for option in $options; do
# skip the options build_input & build_output that are used only in build.sh
# skip the option auto_install that is used only in pack.sh
if [ "$option" = "build_input" ] || [ "$option" = "build_output" ] || [ "$option" = "auto_install" ]; then
continue
fi
echo -e "${PURPLE}Processing option '$option' ...${NC}"
# parse the parameters (only from the first found option)
# duplicated options are not supported, if needed use list of parameters for the same option:
# startup_script="script1.sh" "script2.sh" "script3.sh"
parameters=$(awk -F '=' "{if (! (substr(\$0,1,1) == \"#\") && ! (substr(\$0,1,1) == \";\") && ! (\$1 == \"\") && ! (\$2 == \"\") && (\$1 ~ /$option/ ) ) print \$2}" "$optionsfile" | head -n 1)
# replace the project root requests
parameters="${parameters/@/"$project_root"}"
# execute the script
opt_script="${OPTIONS_DIR}/${option}/${option}.sh"
if [ ! -f "$opt_script" ]; then
echo -e "${RED}ERROR: Cannot find the file '$opt_script' ${NC}"
exit 3
fi
# for each parameter in the list
for parameter in $parameters; do
# remove the leading and ending double quotes
param=$(echo "$parameter" | sed -e 's/^"//' -e 's/"$//')
# remove the leading and ending single quotes
par=$(echo "$param" | sed -e 's/^'\''//' -e 's/'\''$//')
# remove the leading and ending square brackets
req_option=$(echo "$par" | sed -e 's/^\[//' -e 's/\]$//')
if [ "$par" == "$req_option" ]; then
# current parameter requires processing
"$opt_script" "$project_root" "$par"
if [ $? -ne 0 ]; then
# errors found, exit
echo "Errors found! The patching has been canceled."
exit 4
fi
# set this option as already installed
echo "$option=\"$par\"" >>"$installed_options"
else
# this is an option requirement that needs validation
found=""
for opt in $options; do
if [ "$opt" == "$req_option" ]; then
found="$opt"
break
fi
done
if [ -z "$found" ]; then
# required option is missing or not enabled
echo -e "${RED}ERROR: Option '$option' requires option '$req_option' which is missing or not enabled. ${NC}"
exit 5
fi
echo -e "${GREEN}Option '$option' requires option '$req_option'. This requirement was successfully validated. ${NC}"
fi
done
done
echo ""
echo -e "${GREEN}DONE! The selected options are successfully processed.${NC}\nYou may do manually more changes in the 'unpacked' folder if needed."
echo ""
exit 0