b5751d6617bfdf07704132f0b3802dfab193e018
[software/sisu] / bin / sisu
1 #!/usr/bin/env ruby
2 # encoding: utf-8
3 #SiSU: copyright (C) 1997 - 2015 Ralph Amissah; License GPL 3, see appended program information
4 class Orient
5 require 'singleton'
6 include Singleton
7 def initialize #(called_as_,argv)
8 super()
9 @called_as,@argv=$0,$*
10 end
11 def version
12 def stable
13 '5'
14 end
15 def unstable
16 '6'
17 end
18 def default
19 stable
20 end
21 def current
22 'current'
23 end
24 def next
25 'next'
26 end
27 self
28 end
29 def sisu_called_from_directory
30 @@sisu_called_from_directory ||= Dir.pwd
31 end
32 def sisu_run_from?
33 if processing.called_as == processing.file_full_path \
34 and File.dirname(processing.file_full_path) != RbConfig::CONFIG['bindir']
35 :full_path_to_sisu_bin_in_sisu_dir_tree
36 elsif processing.file_full_path =~/\/gems\// \
37 and processing.file_full_path \
38 =~ /\/sisu-[#{version.stable}#{version.unstable}]\.(?:\d+|[a-z])\.(?:\d+|[a-z])\//
39 :gem_install
40 else
41 :system_install
42 end
43 end
44 def processing
45 def called_as
46 @called_as
47 end
48 def argv
49 @argv
50 end
51 def argv_str
52 argv.inspect
53 end
54 def file_full_path
55 __FILE__
56 end
57 def stable
58 'v' + version.stable
59 end
60 def unstable
61 'v' + version.unstable
62 end
63 def default
64 'v' + version.default
65 end
66 def current
67 'current'
68 end
69 def next
70 'develop'
71 end
72 def stable_bin_file_regex
73 %r{\bsisu#{version.stable}$}
74 end
75 def unstable_bin_file_regex
76 %r{\bsisu#{version.unstable}$}
77 end
78 def stable_gem_regex
79 %r{\/sisu-#{version.stable}\.(?:\d+|[a-z])\.(?:\d+|[a-z])\/}
80 end
81 def unstable_gem_regex
82 %r{\/sisu-#{version.unstable}\.(?:\d+|[a-z])\.(?:\d+|[a-z])\/}
83 end
84 def stable_modifier_regex
85 %r{"(?:--v#{version.stable}|--current|--stable|--release)"}
86 end
87 def unstable_modifier_regex
88 %r{"(?:--v#{version.unstable}|--next|--unstable|--dev)"}
89 end
90 self
91 end
92 def sisu_lib
93 "sisu/#{branch_name_}"
94 end
95 def sisu_path_lib
96 if sisu_path_specified_lib \
97 && FileTest.file?("#{sisu_path_specified_lib}/#{sisu_lib}/hub.rb")
98 "#{sisu_path_specified_lib}/#{sisu_lib}"
99 else sisu_lib
100 end
101 end
102 def sisu_version_info_path
103 "#{sisu_path_base_system_data}/version/#{branch_name?}.yml"
104 end
105 def branch_name_
106 if sisu_run_from? == :full_path_to_sisu_bin_in_sisu_dir_tree \
107 or sisu_run_from? == :system_install
108 case processing.called_as
109 when processing.stable_bin_file_regex then processing.current
110 when processing.unstable_bin_file_regex then processing.next
111 else
112 case processing.argv_str
113 when processing.stable_modifier_regex then processing.current
114 when processing.unstable_modifier_regex then processing.next
115 else processing.current
116 end
117 end
118 elsif sisu_run_from? == :gem_install
119 case processing.file_full_path
120 when processing.stable_gem_regex then processing.current
121 when processing.unstable_gem_regex then processing.next
122 else processing.current
123 end
124 else processing.current
125 end
126 end
127 def branch_version_
128 if sisu_run_from? == :full_path_to_sisu_bin_in_sisu_dir_tree \
129 or sisu_run_from? == :system_install
130 case processing.called_as
131 when processing.stable_bin_file_regex then processing.stable
132 when processing.unstable_bin_file_regex then processing.unstable
133 else
134 case processing.argv_str
135 when processing.stable_modifier_regex then processing.stable
136 when processing.unstable_modifier_regex then processing.unstable
137 else processing.stable
138 end
139 end
140 elsif sisu_run_from? == :gem_install
141 case processing.file_full_path
142 when processing.stable_gem_regex then processing.stable
143 when processing.unstable_gem_regex then processing.unstable
144 else processing.stable
145 end
146 else processing.stable
147 end
148 end
149 def paths_set
150 if sisu_run_from? == :system_install \
151 and processing.called_as \
152 =~ /^#{RbConfig::CONFIG['bindir']}\/sisu[#{version.stable}#{version.unstable}]?$/
153 :set_path_system
154 elsif sisu_run_from? == :full_path_to_sisu_bin_in_sisu_dir_tree \
155 and processing.called_as \
156 !~ /^#{RbConfig::CONFIG['bindir']}\/sisu[#{version.stable}#{version.unstable}]?$/ \
157 && File.expand_path(processing.called_as) \
158 =~ /bin\/sisu[#{version.stable}#{version.unstable}]?$/ \
159 && FileTest.file?(processing.called_as)
160 :set_dir_tree_full_path
161 elsif sisu_run_from? ==:gem_install
162 :set_gem_path
163 else nil
164 end
165 end
166 def sisu_path_base
167 case paths_set
168 when :set_path_system
169 RbConfig::CONFIG['datadir'] + '/sisu'
170 when :set_dir_tree_full_path
171 File.expand_path(processing.called_as).
172 sub(/\/bin\/sisu[#{version.stable}#{version.unstable}]?$/,'')
173 when :set_gem_path
174 Gem::Specification.find_by_name("sisu").gem_dir
175 else nil
176 end
177 end
178 def sisu_path_base_system_data
179 case paths_set
180 when :set_path_system
181 RbConfig::CONFIG['datadir'] + '/sisu'
182 when :set_dir_tree_full_path
183 File.expand_path(processing.called_as).
184 sub(/\/bin\/sisu[#{version.stable}#{version.unstable}]?$/,'/data/sisu')
185 when :set_gem_path
186 Gem::Specification.find_by_name("sisu").gem_dir + '/data/sisu'
187 else nil
188 end
189 end
190 def sisu_path_specified_lib
191 if (processing.called_as \
192 !~ /^#{RbConfig::CONFIG['bindir']}\/sisu[#{version.stable}#{version.unstable}]?$/)
193 File.expand_path(processing.called_as).
194 sub(/bin\/sisu[#{version.stable}#{version.unstable}]?$/,'lib')
195 else nil
196 end
197 end
198 def language_list
199 def codes
200 %w[am bg bn br ca cs cy da de el en eo es et eu fi fr ga gl he hi hr hy ia is it ja ko la lo lt lv ml mr nl nn no oc pl pt pt_BR ro ru sa se sk sl sq sr sv ta te th tk tr uk ur us vi zh]
201 end
202 def regex
203 codes.join('|')
204 end
205 self
206 end
207 def sisu_is
208 def run_from?
209 sisu_run_from?
210 end
211 def branch_name?
212 branch_name_
213 end
214 def branch_version?
215 branch_version_
216 end
217 def path_base?
218 sisu_path_base
219 end
220 def path_base_system_data?
221 sisu_path_base_system_data
222 end
223 def path_lib?
224 sisu_path_lib
225 end
226 def version_info_path?
227 sisu_version_info_path
228 end
229 def language_list?
230 language_list.codes
231 end
232 def language_list_regex?
233 language_list.regex
234 end
235 def called_from?
236 sisu_called_from_directory
237 end
238 self
239 end
240 def get_processing_info
241 def sortout
242 unless @clm_pf
243 processing_files={}
244 command_line_modifiers=[]
245 processing.argv.each do |x|
246 if x =~/^-+/
247 command_line_modifiers << x
248 elsif x =~/\.ss[tm]/
249 if x =~/\//
250 markup_path,markup_file =
251 (/(\S+\/)((?:#{language_list.regex})?[^\/]+)/).
252 match(x)[1,2]
253 if not processing_files[markup_path]
254 processing_files[markup_path]=[]
255 end
256 processing_files[markup_path] << markup_file
257 else
258 if not processing_files['./']
259 processing_files['./']=[]
260 end
261 processing_files['./'] << x
262 end
263 end
264 end
265 end
266 @clm_pf ||={
267 command_line_modifiers: command_line_modifiers,
268 processing_files: processing_files,
269 }
270 end
271 def command_line_modifiers
272 sortout[:command_line_modifiers]
273 end
274 def processing_files
275 sortout[:processing_files]
276 end
277 self
278 end
279 def control
280 require "#{sisu_is.path_lib?}/hub"
281 sisu_called_from_directory
282 begin
283 if get_processing_info.processing_files.length > 0
284 get_processing_info.processing_files.each_pair do |markup_dir,markup_file|
285 begin
286 $sisu_document_markup_directory=markup_dir.gsub(/\/$/,'')
287 argv_sub=get_processing_info.command_line_modifiers + markup_file
288 if FileTest.directory?(markup_dir)
289 system("cd #{markup_dir}")
290 Dir.chdir(markup_dir)
291 $sisu_document_markup_directory_base_fixed_path=
292 Dir.pwd.gsub(/\/(?:#{language_list.regex})$/,'')
293 SiSU::HubMaster.new(argv_sub,sisu_called_from_directory)
294 elsif markup_dir =~/https?:/
295 markup_file.each do |mf|
296 (FileTest.file?(mf)) \
297 ? (puts 'requested remote file already exists in current directory ' \
298 + Dir.pwd + ' using ' + mf)
299 : (system("wget #{markup_dir}/#{mf}"))
300 end
301 $sisu_document_markup_directory_base_fixed_path=
302 Dir.pwd.gsub(/\/(?:#{language_list.regex})$/,'')
303 argv_sub=command_line_modifiers + markup_file
304 SiSU::HubMaster.new(argv_sub,sisu_called_from_directory)
305 else p "Error directory specified #{markup_dir} not found"
306 end
307 ensure
308 Dir.chdir(sisu_called_from_directory)
309 system("cd #{sisu_called_from_directory}")
310 end
311 end
312 else
313 $sisu_document_markup_directory=Dir.pwd.gsub(/\/$/,'')
314 $sisu_document_markup_directory_base_fixed_path=
315 Dir.pwd.gsub(/(^|\/)(?:#{language_list.regex})$/,'')
316 SiSU::HubMaster.new(processing.argv,sisu_called_from_directory)
317 end
318 rescue
319 ensure
320 SiSU::HubClose.new(sisu_called_from_directory,@argv)
321 Dir.chdir(sisu_called_from_directory)
322 system("cd #{sisu_called_from_directory}")
323 end
324 end
325 end
326 #% sisu select source & branch, begin
327 $VERBOSE=nil
328 require 'rbconfig'
329 require 'rubygems'
330 begin
331 require 'singleton'
332 s=Orient.instance
333 SiSU_is=s.sisu_is
334 s.control
335 rescue #% -
336 STDERR.puts 'ERROR'
337 STDERR.puts $!
338 STDERR.puts $@
339 puts %{
340 #{%x{ruby -v}.strip.to_s}
341 #{RUBY_VERSION}
342 ruby version: #{RbConfig::CONFIG['RUBY_PROGRAM_VERSION']}p#{RbConfig::CONFIG['PATCHLEVEL']}
343 ruby named: #{RbConfig::CONFIG['ruby_version']}
344 rubylib: #{RbConfig::CONFIG['rubylibdir']}
345 rubylib local: #{RbConfig::CONFIG['sitelibdir']}
346 gem dir: #{Gem.dir}
347 gem path: #{Gem.path}
348 }
349 system(%{echo "
350 system RUBYPATH: ${RUBYPATH}
351 system RUBYLIB: ${RUBYLIB}
352 "})
353 end
354 __END__
355
356 * Name: SiSU
357
358 ** Description: documents, structuring, processing, publishing, search
359
360 ** Author: Ralph Amissah
361 <ralph@amissah.com>
362 <ralph.amissah@gmail.com>
363
364 ** Copyright: (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
365 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 Ralph Amissah,
366 All Rights Reserved.
367
368 ** License: GPL 3 or later:
369
370 SiSU, a framework for document structuring, publishing and search
371
372 Copyright (C) Ralph Amissah
373
374 This program is free software: you can redistribute it and/or modify it
375 under the terms of the GNU General Public License as published by the Free
376 Software Foundation, either version 3 of the License, or (at your option)
377 any later version.
378
379 This program is distributed in the hope that it will be useful, but WITHOUT
380 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
381 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
382 more details.
383
384 You should have received a copy of the GNU General Public License along with
385 this program. If not, see <http://www.gnu.org/licenses/>.
386
387 If you have Internet connection, the latest version of the GPL should be
388 available at these locations:
389 <http://www.fsf.org/licensing/licenses/gpl.html>
390 <http://www.gnu.org/licenses/gpl.html>
391
392 <http://www.sisudoc.org/sisu/en/manifest/gpl.fsf.html>
393
394 ** SiSU uses:
395 * Standard SiSU markup syntax,
396 * Standard SiSU meta-markup syntax, and the
397 * Standard SiSU object citation numbering and system
398
399 ** Hompages:
400 <http://www.jus.uio.no/sisu>
401 <http://www.sisudoc.org>
402
403 ** Download:
404 <http://www.sisudoc.org/sisu/en/SiSU/download.html>
405
406 ** Git
407 <http://git.sisudoc.org/gitweb/?p=code/sisu.git;a=summary>
408 <http://git.sisudoc.org/gitweb/?p=code/sisu.git;a=blob;f=bin/sisu;hb=HEAD>