OSDN Git Service

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