OSDN Git Service

Version 3.0.3+toward_3.0.4_20131115074959
[portsreinstall/current.git] / lib / libconf.sh
1 #!/bin/sh -e
2 # ==============================================================================
3 # portsreinstall library script
4 # - Parsing of configuration files -
5 # Copyright (C) 2013 Mamoru Sakaue, MwGhennndo, All Rights Reserved.
6 # This software is distributed under the 2-Clause BSD License.
7 # ==============================================================================
8
9 # ============= Set up environment variables =============
10 conf_setup_ports_envs ()
11 {
12         PORTS_MOVED_DB=${PORTSDIR}/MOVED
13         PORTS_INDEX_DB=${PORTSDIR}/INDEX-`uname -r | cut -d . -f 1`
14         if [ ! -e "${PORTS_INDEX_DB}" ]
15         then
16                 message_echo "INFO: The ports index for the current OS version is not found, so fetching it now."
17                 if ! make -C "${PORTSDIR}" fetchindex || [ ! -e "${PORTS_INDEX_DB}" ]
18                 then
19                         message_echo "WARNING: Failed to fetch the ports index, so trying to build it locally." >&2
20                         make -C "${PORTSDIR}" index
21                 fi
22         fi
23 }
24
25 # ============= Set up environment variables =============
26 conf_setup_envs ()
27 {
28         local tmp_work
29         tmp_work=${TMPDIR}/conf_setup_envs:work
30         rm -rf "$tmp_work"
31         mkdir "$tmp_work"
32         PORTSDIR=${PORTSDIR:-`[ -e /etc/make.conf ] && make -C "$tmp_work" -f /etc/make.conf -V PORTSDIR 2> /dev/null`} || :
33         PORTSDIR=${PORTSDIR:-/usr/ports}
34         for var in LOCALBASE PORT_DBDIR LINUXBASE DISTDIR PACKAGES PKGREPOSITORYSUBDIR PKGREPOSITORY
35         do
36                 val=`pkgsys_sysvar $var`
37                 eval $var=\$val
38         done
39         PKGREPOSITORY=${PKG_PATH:-${PKGREPOSITORY}}
40         PACKAGECHECKSUMROOTS=${PACKAGECHECKSUMROOTS:-'ftp://ftp.FreeBSD.org/|'\
41 'ftp://ftp1.FreeBSD.org/|ftp://ftp2.FreeBSD.org/|ftp://ftp3.FreeBSD.org/|'\
42 'ftp://ftp4.FreeBSD.org/|ftp://ftp5.FreeBSD.org/|ftp://ftp6.FreeBSD.org/|'\
43 'ftp://ftp7.FreeBSD.org/|ftp://ftp8.FreeBSD.org/|ftp://ftp9.FreeBSD.org/|'\
44 'ftp://ftp10.FreeBSD.org/|ftp://ftp11.FreeBSD.org/|ftp://ftp12.FreeBSD.org/|'\
45 'ftp://ftp13.FreeBSD.org/|ftp://ftp14.FreeBSD.org/'}
46         PACKAGECHECKSUMDIR=${PACKAGEDIR:-'pub/FreeBSD/ports/%s/packages-%s/All/'}
47         PACKAGEROOTS=${PACKAGEROOTS:-${PACKAGECHECKSUMROOTS}}
48         PACKAGEDIR=${PACKAGEDIR:-${PACKAGECHECKSUMDIR}}
49         conf_setup_ports_envs
50 }
51
52 # ============= Parse configuration variables given for each port glob =============
53 conf_parse_vars_for_each_port_glob ()
54 {
55         local section
56         section=$1
57         misc_get_all_vardefs | grep -e "^_CONF_${section}_" | cut -d = -f 1 | while read var
58         do
59                 eval glob_pattern=\${$var}
60                 pkgsys_eval_ports_glob "$glob_pattern"
61         done > ${DBDIR}/conf/${section}:PORTS.parsed
62 }
63
64 # ============= Parse configuration variables given for each port glob with a bound value =============
65 conf_parse_vars_for_each_port_glob_with_bound_val ()
66 {
67         local section tag_target tag_val
68         section=$1
69         tag_target=$2
70         tag_val=$3
71         misc_get_all_vardefs | grep -e "^_CONF_${section}_${tag_target}_" | cut -d = -f 1 | while read var
72         do
73                 eval glob_pattern=\${$var}
74                 eval val=\$\{`echo "$var" | sed "s/^_CONF_${section}_${tag_target}_/_CONF_${section}_${tag_val}_/"`\}
75                 pkgsys_eval_ports_glob "$glob_pattern" | while read origin
76                 do
77                         path=${DBDIR}/conf/each_port/$origin
78                         [ -d "$path" ] || mkdir -p "$path"
79                         echo "$val" > $path/${section}.conf
80                 done
81         done
82 }
83
84 # ============= Parse pkgtools.conf(5) =============
85 conf_parse_pkgtools_conf ()
86 {
87         local PORTUPGRADE tmp_script_crop portupgrade_pkg
88         tmp_script_crop=${TMPDIR}/conf_parse_pkgtools_conf:crop
89         PORTUPGRADE=`which portupgrade | head -n 1 || :`
90         if [ ! -f "$PORTUPGRADE" ]
91         then
92                 message_echo "WARNING: portupgrade is not found, pkgtools.conf is ignored" >&2
93                 return
94         fi
95         portupgrade_pkg=`pkg_info_qO ports-mgmt/portupgrade`
96         [ -n "$portupgrade_pkg" ] || portupgrade_pkg=`pkg_info_qO ports-mgmt/portupgrade-devel`
97         [ `expr "$portupgrade_pkg" : '^portupgrade-devel-'` -eq 0 ] \
98                 || message_echo "WARNING: Combination with portupgrade-devel-* has not tested." >&2
99         sed -n '/^def init_global$/,/^end$/p' "$PORTUPGRADE" > $tmp_script_crop
100         [ `wc -l < $tmp_script_crop` -gt 0 ] || \
101                 { message_echo "ERROR: The current installed version of portupgrade is unsupported." >&2; }
102         ruby > ${DBDIR}/conf/pkgtools.parsed << eof
103 MYNAME = 'portupgrade'
104 require 'pkgtools'
105 `cat "$tmp_script_crop"`
106 init_global
107 init_pkgtools_global
108 load_config
109 alt_moved = config_value(:ALT_MOVED)
110 hold_pkgs = config_value(:HOLD_PKGS)
111 ignore_moved = config_value(:IGNORE_MOVED)
112 alt_pkgdep = config_value(:ALT_PKGDEP)
113 make_args = config_value(:MAKE_ARGS)
114 make_env = config_value(:MAKE_ENV)
115 beforebuild = config_value(:BEFOREBUILD)
116 beforedeinstall = config_value(:BEFOREDEINSTALL)
117 afterinstall = config_value(:AFTERINSTALL)
118
119 keys = ['LOCALBASE', 'LINUXBASE', 'PORT_DBDIR', 'PORTSDIR', 'DISTDIR', 'PACKAGES', 'PKG_PATH', 'PKGREPOSITORY']
120 keys.each{|key|
121         if ENV.has_key?(key) then
122                 printf("ENV_%s=%s\n", key, ENV[key])
123         end
124 }
125
126 i = 0
127 alt_moved.each do |val|
128         printf("ALT_MOVED_pkgtoolsconf_%d_='%s'\n", i, val)
129         i = i + 1
130 end
131
132 i = 0
133 hold_pkgs.each do |val|
134         printf("HOLD_pkgtoolsconf_%d_='%s'\n", i, val)
135         i = i + 1
136 end
137 ignore_moved.each do |val|
138         printf("HOLD_pkgtoolsconf_%d_='%s'\n", i, val)
139         i = i + 1
140 end
141
142 i = 0
143 alt_pkgdep.each do |pat, alt|
144         printf("REPLACE_FROM_pkgtoolsconf_%d_='%s'\n", i, pat)
145         printf("REPLACE_TO_pkgtoolsconf_%d_='%s'\n", i, alt)
146         i = i + 1
147 end
148
149 i = 0
150 make_args.each do |target, definition|
151         printf("MARG_TARGET_pkgtoolsconf_%d_='%s'\n", i, target)
152         printf("MARG_DEF_pkgtoolsconf_%d_='%s'\n", i, definition)
153         i = i + 1
154 end
155
156 i = 0
157 make_env.each do |target, definition|
158         printf("MENV_TARGET_pkgtoolsconf_%d_='%s'\n", i, target)
159         printf("MENV_DEF_pkgtoolsconf_%d_='%s'\n", i, definition)
160         i = i + 1
161 end
162
163 i = 0
164 beforebuild.each do |target, command|
165         printf("BEFOREBUILD_TARGET_pkgtoolsconf_%d_='%s'\n", i, target)
166         printf("BEFOREBUILD_COMMAND_pkgtoolsconf_%d_='%s'\n", i, command)
167         i = i + 1
168 end
169
170 i = 0
171 beforedeinstall.each do |target, command|
172         printf("BEFOREDEINSTALL_TARGET_pkgtoolsconf_%d_='%s'\n", i, target)
173         printf("BEFOREDEINSTALL_COMMAND_pkgtoolsconf_%d_='%s'\n", i, command)
174         i = i + 1
175 end
176
177 i = 0
178 afterinstall.each do |target, command|
179         printf("AFTERINSTALL_TARGET_pkgtoolsconf_%d_='%s'\n", i, target)
180         printf("AFTERINSTALL_COMMAND_pkgtoolsconf_%d_='%s'\n", i, command)
181         i = i + 1
182 end
183 eof
184 }
185
186 # ============= Get complete configuration variable definitions by importing pkgtools.conf(5) if available =============
187 conf_get_complete_var_defs ()
188 {
189         case `options_get_effective_opt_load_pkgtoolsconf` in
190         default)
191                 conf_parse_pkgtools_conf
192                 cat "${DBDIR}/conf/pkgtools.parsed" "${CONFFILE}" 2> /dev/null || :
193                 ;;
194         override)
195                 conf_parse_pkgtools_conf
196                 cat "${CONFFILE}" "${DBDIR}/conf/pkgtools.parsed" 2> /dev/null || :
197                 ;;
198         no)
199                 cat "${CONFFILE}" 2> /dev/null || :
200                 ;;
201         esac
202 }
203
204 # ============= Manipulate available variable definitions =============
205 conf_manipulate_available_var_defs ()
206 {
207         (set -e
208                 tmpfile_unsetvars=${TMPDIR}/conf_manipulate_available_var_defs::unsetvars.sh
209                 _CONFVARS='ENV ALT_MOVED HOLD TABOO REPLACE_FROM REPLACE_TO MARG_TARGET MARG_DEF MENV_TARGET MENV_DEF BEFOREBUILD BEFOREDEINSTALL AFTERINSTALL'
210                 for item in ${_CONFVARS}
211                 do
212                         misc_get_all_vardefs | grep -e "^${item}_" | cut -d = -f 1 | sed 's/^/unset /'
213                 done > $tmpfile_unsetvars
214                 . "$tmpfile_unsetvars"
215                 . "${DBDIR}/conf/complete_setup.sh"
216                 for item in ${_CONFVARS}
217                 do
218                         misc_get_all_vardefs | grep -e "^${item}_" | sed 's/^/_CONF_/'
219                 done | sort -u > ${DBDIR}/conf/manipulated_defs.sh
220         ) || { message_echo "ERROR: Failed to manipulate available variable definitions from the configuration file." >&2; exit 1; }
221 }
222
223 # ============= Build an effective MOVED database =============
224 conf_build_effective_MOVED ()
225 {
226         grep -v -e '^[[:space:]]*#' -e '^[[:space:]]*$' "${PORTS_MOVED_DB}" > ${DBDIR}/conf/MOVED.parsed
227         misc_get_all_vardefs | grep -e '^_CONF_ALT_MOVED_' | cut -d = -f 1 | while read var
228         do
229                 eval movedsb_path=\${$var}
230                 cat "$movedsb_path"
231         done | grep -v -e '^[[:space:]]*#' -e '^[[:space:]]*$' > ${DBDIR}/conf/MOVED_ALT.parsed || :
232         cat "${DBDIR}/conf/MOVED_ALT.parsed" >> ${DBDIR}/conf/MOVED.parsed
233 }
234
235 # ============= Set up effective environment values =============
236 conf_setup_effective_env ()
237 {
238         (
239                 for key in LOCALBASE LINUXBASE PORT_DBDIR PORTSDIR DISTDIR PACKAGES PKG_PATH PKGREPOSITORY PACKAGECHECKSUMROOTS PACKAGECHECKSUMDIR PACKAGEROOTS PACKAGEDIR
240                 do
241                         eval $key=\$\{_CONF_ENV_$key:-\$\{$key\}\}
242                 done
243                 misc_get_all_vardefs | grep -e '^LOCALBASE=' -e '^LINUXBASE=' -e '^PORT_DBDIR=' -e '^PORTSDIR=' -e '^DISTDIR=' -e 'PACKAGES=' -e '^PKG_PATH=' -e '^PKGREPOSITORY=' -e '^PACKAGECHECKSUMROOTS='  -e '^PACKAGECHECKSUMDIR=' -e '^PACKAGEROOTS=' -e '^PACKAGEDIR='
244         )
245         echo '[ -n "$PKG_PATH" ] && PKGREPOSITORY=$PKG_PATH'
246         echo 'conf_setup_ports_envs'
247         [ "${DBDIR}/conf/MOVED.parsed" -nt "${PORTS_MOVED_DB}" ] || return 0
248         echo PORTS_MOVED_DB=${DBDIR}/conf/MOVED.parsed
249 }
250
251 # ============= Build replacement patterns from REPLACE variables =============
252 conf_build_replacement_patterns_from_REPLACE ()
253 {
254         local tmp_evaluated_origins
255         tmp_matching_origins=${TMPDIR}/conf_build_replacement_patterns_from_REPLACE:origins
256         cp /dev/null "${DBDIR}/conf/REPLACE.grep_from_pattern"
257         cp /dev/null "${DBDIR}/conf/REPLACE.sed_pattern"
258         cp /dev/null "${DBDIR}/conf/REPLACE.csv"
259         misc_get_all_vardefs | grep -e '^_CONF_REPLACE_FROM_' | cut -d = -f 1 | while read var
260         do
261                 eval glob_pattern=\$\{$var\}
262                 eval to=\$\{`echo "$var" | sed 's/^_CONF_REPLACE_FROM_/_CONF_REPLACE_TO_/'`\}
263                 pkgsys_eval_ports_glob_even_if_nonexistent "$glob_pattern" > $tmp_matching_origins || :
264                 if [ `wc -l < $tmp_matching_origins` -eq 0 ]
265                 then
266                         message_echo "WARNING: Original package to be replaced [$glob_pattern] is obsolete." >&2
267                         message_echo "If still required, use a pattern for port origins instead." >&2
268                         continue
269                 fi
270                 str_escape_regexp_filter < $tmp_matching_origins \
271                         | sed 's|^|^|; s|$|$|' >> ${DBDIR}/conf/REPLACE.grep_from_pattern
272                 if [ -z "$to" -o "$to" = delete ]
273                 then
274                         to=
275                 else
276                         [ -d "${PORTSDIR}/$to" ] || message_echo "WARNING: replacement port [$to] is obsolete." >&2
277                 fi
278                 str_escape_regexp_filter < $tmp_matching_origins \
279                         | sed -E "s|^|s:^|; s/$/$:`str_escape_replaceval \"$to\"`:/" \
280                         >> ${DBDIR}/conf/REPLACE.sed_pattern
281                 while read from
282                 do
283                         printf '%s\t%s\n' "$from" "$to"
284                 done < $tmp_matching_origins >> ${DBDIR}/conf/REPLACE.csv
285         done
286 }
287