OSDN Git Service

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