OSDN Git Service

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