OSDN Git Service

Fixed a bug that some commands were broken when options are specified.
[portsreinstall/current.git] / lib / libmessage.sh
1 #!/bin/sh -e
2 # ==============================================================================
3 # portsreinstall library script
4 # - Messages -
5 # Copyright (C) 2013-2018 Mamoru Sakaue, MwGhennndo, All Rights Reserved.
6 # This software is distributed under the 2-Clause BSD License.
7 # ==============================================================================
8
9 # ============= Simple query by y (yes) or n (no) where yes by default =============
10 message_query_yn_default_yes ()
11 {
12         local answer
13         [ $opt_batch_mode = yes ] && return 0
14         read answer || :
15         case `echo "$answer" | head -c 1` in
16         y | Y ) message_echo
17                 return 0
18                 ;;
19         n | N ) message_echo
20                 return 1
21                 ;;
22         '' )    return 0
23                 ;;
24         *)      message_echo
25                 message_echo 'Pardon? ([y]/n)'
26                 message_query_yn_default_yes
27                 ;;
28         esac
29 }
30
31 # ============= Simple query by y (yes) or n (no) where no by default =============
32 message_query_yn_default_no ()
33 {
34         local answer
35         [ $opt_batch_mode = yes ] && return 1
36         read answer || :
37         case `echo "$answer" | head -c 1` in
38         y | Y ) message_echo
39                 return 0
40                 ;;
41         n | N ) message_echo
42                 return 1
43                 ;;
44         '' )    return 1
45                 ;;
46         *)      message_echo
47                 message_echo 'Pardon? (y/[n])'
48                 message_query_yn_default_no
49                 ;;
50         esac
51 }
52
53 # ============= Output the version =============
54 message_version ()
55 {
56         [ "x$opt_batch_mode" = xno ] && echo -n "${APPNAME} version "
57         echo "${MYVERSION}"
58 }
59
60 # ============= Output the brief description =============
61 message_brief_description ()
62 {
63         cat << eof
64  -- Ports upgrading utility for massive forced reinstallation
65  -- And for those who are pursuing the perfect packages environment
66 eof
67 }
68
69 # ============= Output a message except for the batch mode =============
70 message_echo ()
71 {
72         [ $opt_batch_mode = yes ] && return
73         echo "$@" | fold -s -w `misc_get_console_column_size`
74 }
75
76 # ============= Output a message from stdin except for the batch mode =============
77 # Use via pipe or redirection without argument is discouraged because it violates the screen size detection.
78 message_cat ()
79 {
80         local ncolumns
81         [ $opt_batch_mode = yes ] && return
82         ncolumns=`misc_get_console_column_size`
83         fold -s -w $ncolumns "$@"
84 }
85
86 # ============= Output a credit =============
87 message_credit ()
88 {
89         local ncolumns
90         [ $opt_batch_mode = yes -o "x$opt_no_opening_message" = xyes ] && return
91         ncolumns=`misc_get_console_column_size`
92         message_version | fold -s -w $ncolumns
93         message_brief_description | fold -s -w $ncolumns
94         fold -s -w $ncolumns << eof
95 Copyright (C) 2010 - 2018 Mamoru Sakaue, MwGhennndo, All Rights Reserved.
96 Email: <sakaue.mamoru@samurai.mwghennn.net>
97 Homepage: <http://www.mwghennndo.com/software/portsreinstall/>
98 eof
99 }
100
101 # ============= Output a time stamp =============
102 message_timestamp ()
103 {
104         env LANG= date
105 }
106
107 # ============= Output an opening notice =============
108 message_opening_notice ()
109 {
110         local ncolumns
111         [ $opt_batch_mode = yes -o $opt_no_opening_message = yes ] && return
112         ncolumns=`misc_get_console_column_size`
113         fold -s -w $ncolumns << eof
114
115  Don't hesitate to terminate by CTRL+C anytime you feel the system is heavy to use because you can restart the operation from the terminated point quickly.
116
117  The current time is `message_timestamp`
118 eof
119 }
120
121 # ============= Show option settings =============
122 message_show_option_settings ()
123 {
124         if [ -e "${DBDIR}/saved_options.sh" \
125                 -a $opt_batch_mode = no -a $opt_no_opening_message = no \
126                 -a \( $COMMAND_MODE = do -o $COMMAND_MODE = redo \) ]
127         then
128                 message_echo "INFO: List of option values:"
129                 message_echo "-----------------------------------------"
130                 message_cat "${DBDIR}/saved_options.sh"
131                 message_echo "-----------------------------------------"
132                 message_echo
133         fi
134 }
135
136 # ============= Report a failure in a process for a port =============
137 message_report_failure_for_a_port ()
138 {
139         local origin
140         origin=$1
141         message_echo "*** Giving up for this port $origin and proceeding to next forcibly..."
142         message_echo
143 }
144
145 # ============= Output the relation of the current port with specified targets =============
146 message_target_relations ()
147 {
148         [ $opt_batch_mode = yes ] && return
149         ( set -e
150                 origin=$1
151                 database_query_get_target_attributes it "$origin"
152                 [ -z "${it_is_all}" -a -n "${it_is_relevant}" ] || return 0
153                 [ -n "${it_is_target}" ] \
154                         && message_echo "(Target port)"
155                 [ -n "${it_is_requires_dependents}" ] \
156                         && message_echo "(Dependent of the target port(s))"
157                 [ -n "${it_is_requires_requirements}" ] \
158                         && message_echo "(Requirement of the target port(s))"
159                 [ -n "${it_is_initial_dependents}" -a -z "${it_is_requires_dependents}" ] \
160                         && message_echo "(Former dependent of the target port(s))"
161                 [ -n "${it_is_initial_requirements}" -a -z "${it_is_requires_requirements}" ] \
162                         && message_echo "(Former requirement of the target port(s))"
163                 [ -n "${it_is_requires_requirements_complement}" ] \
164                         && message_echo "(Requirement of the target port(s) for installing their requirements)"
165                 :
166         )
167 }
168
169 # ============= Notice that the current run is a restarted one =============
170 message_restarted_process ()
171 {
172         message_echo "INFO: Restarting from the previously terminated point."
173 }
174
175 # ============= Output of a section title =============
176 message_section_title ()
177 {
178         message_echo "[`message_timestamp`] $*"
179 }
180
181 # ============= Output of a stage title =============
182 message_stage_title ()
183 {
184         message_echo "========== $* =========="
185         message_echo "[At `message_timestamp`]"
186 }
187
188 # ============= Message of dry run =============
189 message_dry_run ()
190 {
191         message_echo "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
192         message_echo "@@@@@@@@@@@@@@@@@ DRY RUN @@@@@@@@@@@@@@@@@"
193         message_echo "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
194         message_echo
195 }
196
197 # ============= Message of fetch-only mode =============
198 message_fetch_only ()
199 {
200         message_echo "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
201         message_echo "@@@@@@@@@@@@@ FETCH-ONLY MODE @@@@@@@@@@@@@"
202         message_echo "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
203         message_echo
204 }
205
206 # ============= Output a summary of a resource recorded in a list =============
207 message_summary_dependents_of_failed_reinstallation ()
208 {
209         local subject comment tmpmsg ncolumns
210         subject=$1
211         comment=$2
212         tmpmsg=${TMPDIR}/message_summary_dependents_of_failed_reinstallation::message
213         database_query_show_single_list_exec "$subject" \
214                 `options_get_dependency_type` `options_get_dependency_level` > $tmpmsg || return 0
215         [ $opt_batch_mode = yes ] && return 1
216         ncolumns=`misc_get_console_column_size`
217         message_echo '****************'
218         message_cat "$tmpmsg"
219         if [ -n "$comment" -a -e "$comment" ]
220         then
221                 message_echo '****************'
222                 message_cat "$comment"
223         fi
224         message_echo '****************'
225         message_echo
226         return 1
227 }
228
229 # ============= Show a message of a dependency scope =============
230 message_dependency_scope ()
231 {
232         ( set -e
233                 options_parse_dependency_type "$deptag"
234                 options_parse_dependency_level "$level"
235                 message_echo "(Evaluated by `options_get_dependency_msgterm` dependency)"
236         )
237 }
238
239 # ============= Output an advice on manual solution to (re)installation failures =============
240 message_summary_advice_on_manual_solution ()
241 {
242         local tmpcomment command_pkg_updating
243         tmpcomment=${TMPDIR}/message_summary_failed_reinstallation::comment
244         command_pkg_updating=`pkgsys_show_pkg_updating_commands`
245         cat > $tmpcomment << eof
246 ----------------
247 [Advices on manual solutions to (re)installation failures]
248  Simple redo processes may resolve the above problems, but otherwise manual solution is needed.
249  In the latter case, you are recommended to read ${PORTSDIR}/UPDATING to resolve the problems.
250 eof
251         [ -n "$command_pkg_updating" ] && cat >> $tmpcomment << eof
252  $command_pkg_updating will be useful for this purpose.
253
254 eof
255         cat >> $tmpcomment << eof
256  For unnecessary failed ports (with their unnecessary dependents), you can register it by
257         ${APPNAME} noneed add [deleted_port_globs]
258 and execute
259         ${APPNAME}
260
261  For necessary and manually successful ports, after resolving the problems, execute
262         ${APPNAME} ok add [resolved_port_globs]
263 and execute
264         ${APPNAME} redo
265
266  Refer to "Workaround for failed ports" sections in the manual page for details.
267 ----------------
268
269 eof
270         message_cat "$tmpcomment"
271 }
272
273 # ============= Output a warning if no progress was achieved =============
274 message_warn_no_achieved_progress ()
275 {
276         [ $opt_batch_mode = yes ] && return
277         message_echo '****************'
278         message_echo "WARNING: Some problems remain. Manual troubleshooting is required."
279         message_echo '****************'
280         message_echo
281 }