c&d: pass info needed make run better without install
[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.sisu_runtime_path \
34 and File.dirname(processing.sisu_runtime_path) != RbConfig::CONFIG['bindir']
35 :full_path_to_sisu_bin_in_sisu_dir_tree
36 elsif processing.sisu_runtime_path =~/\/gems\// \
37 and processing.sisu_runtime_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 sisu_runtime_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.sisu_runtime_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.sisu_runtime_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 runtime_path?
209 sisu_runtime_path
210 end
211 def runtime_type?
212 sisu_run_from?
213 end
214 def called_from?
215 sisu_called_from_directory
216 end
217 def branch_name?
218 branch_name_
219 end
220 def branch_version?
221 branch_version_
222 end
223 def path_base?
224 sisu_path_base
225 end
226 def path_base_system_data?
227 sisu_path_base_system_data
228 end
229 def path_lib?
230 sisu_path_lib
231 end
232 def version_info_path?
233 sisu_version_info_path
234 end
235 def language_list?
236 language_list.codes
237 end
238 def language_list_regex?
239 language_list.regex
240 end
241 def runtime?
242 {
243 call_path: called_from?,
244 runtime_path: runtime_path?,
245 runtime_type: runtime_type?,
246 }
247 end
248 self
249 end
250 def get_processing_info
251 def sortout
252 unless @clm_pf
253 processing_files={}
254 command_line_modifiers=[]
255 processing.argv.each do |x|
256 if x =~/^-+/
257 command_line_modifiers << x
258 elsif x =~/\.ss[tm]/
259 if x =~/\//
260 markup_path,markup_file =
261 (/(\S+\/)((?:#{language_list.regex})?[^\/]+)/).
262 match(x)[1,2]
263 if not processing_files[markup_path]
264 processing_files[markup_path]=[]
265 end
266 processing_files[markup_path] << markup_file
267 else
268 if not processing_files['./']
269 processing_files['./']=[]
270 end
271 processing_files['./'] << x
272 end
273 end
274 end
275 end
276 @clm_pf ||={
277 command_line_modifiers: command_line_modifiers,
278 processing_files: processing_files,
279 }
280 end
281 def command_line_modifiers
282 sortout[:command_line_modifiers]
283 end
284 def processing_files
285 sortout[:processing_files]
286 end
287 self
288 end
289 def control
290 require "#{sisu_is.path_lib?}/hub"
291 sisu_called_from_directory
292 begin
293 if get_processing_info.processing_files.length > 0
294 get_processing_info.processing_files.each_pair do |markup_dir,markup_file|
295 begin
296 $sisu_document_markup_directory=markup_dir.gsub(/\/$/,'')
297 argv_sub=get_processing_info.command_line_modifiers + markup_file
298 if FileTest.directory?(markup_dir)
299 system("cd #{markup_dir}")
300 Dir.chdir(markup_dir)
301 $sisu_document_markup_directory_base_fixed_path=
302 Dir.pwd.gsub(/\/(?:#{language_list.regex})$/,'')
303 SiSU::HubMaster.new(argv_sub,sisu_is.runtime?)
304 elsif markup_dir =~/https?:/
305 markup_file.each do |mf|
306 (FileTest.file?(mf)) \
307 ? (puts 'requested remote file already exists in current directory ' \
308 + Dir.pwd + ' using ' + mf)
309 : (system("wget #{markup_dir}/#{mf}"))
310 end
311 $sisu_document_markup_directory_base_fixed_path=
312 Dir.pwd.gsub(/\/(?:#{language_list.regex})$/,'')
313 argv_sub=command_line_modifiers + markup_file
314 SiSU::HubMaster.new(argv_sub,sisu_is.runtime?)
315 else p "Error directory specified #{markup_dir} not found"
316 end
317 ensure
318 Dir.chdir(sisu_called_from_directory)
319 system("cd #{sisu_called_from_directory}")
320 end
321 end
322 else
323 $sisu_document_markup_directory=Dir.pwd.gsub(/\/$/,'')
324 $sisu_document_markup_directory_base_fixed_path=
325 Dir.pwd.gsub(/(^|\/)(?:#{language_list.regex})$/,'')
326 SiSU::HubMaster.new(processing.argv,sisu_is.runtime?)
327 end
328 rescue
329 ensure
330 SiSU::HubClose.new(sisu_called_from_directory,@argv)
331 Dir.chdir(sisu_called_from_directory)
332 system("cd #{sisu_called_from_directory}")
333 end
334 end
335 end
336 #% sisu select source & branch, begin
337 $VERBOSE=nil
338 require 'rbconfig'
339 require 'rubygems'
340 begin
341 require 'singleton'
342 s=Orient.instance
343 SiSU_is=s.sisu_is
344 s.control
345 rescue #% -
346 STDERR.puts 'ERROR'
347 STDERR.puts $!
348 STDERR.puts $@
349 puts %{
350 #{%x{ruby -v}.strip.to_s}
351 #{RUBY_VERSION}
352 ruby version: #{RbConfig::CONFIG['RUBY_PROGRAM_VERSION']}p#{RbConfig::CONFIG['PATCHLEVEL']}
353 ruby named: #{RbConfig::CONFIG['ruby_version']}
354 rubylib: #{RbConfig::CONFIG['rubylibdir']}
355 rubylib local: #{RbConfig::CONFIG['sitelibdir']}
356 gem dir: #{Gem.dir}
357 gem path: #{Gem.path}
358 }
359 system(%{echo "
360 system RUBYPATH: ${RUBYPATH}
361 system RUBYLIB: ${RUBYLIB}
362 "})
363 end
364 __END__
365
366 * Name: SiSU
367
368 ** Description: documents, structuring, processing, publishing, search
369
370 ** Author: Ralph Amissah
371 <ralph@amissah.com>
372 <ralph.amissah@gmail.com>
373
374 ** Copyright: (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
375 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 Ralph Amissah,
376 All Rights Reserved.
377
378 ** License: GPL 3 or later:
379
380 SiSU, a framework for document structuring, publishing and search
381
382 Copyright (C) Ralph Amissah
383
384 This program is free software: you can redistribute it and/or modify it
385 under the terms of the GNU General Public License as published by the Free
386 Software Foundation, either version 3 of the License, or (at your option)
387 any later version.
388
389 This program is distributed in the hope that it will be useful, but WITHOUT
390 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
391 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
392 more details.
393
394 You should have received a copy of the GNU General Public License along with
395 this program. If not, see <http://www.gnu.org/licenses/>.
396
397 If you have Internet connection, the latest version of the GPL should be
398 available at these locations:
399 <http://www.fsf.org/licensing/licenses/gpl.html>
400 <http://www.gnu.org/licenses/gpl.html>
401
402 <http://www.sisudoc.org/sisu/en/manifest/gpl.fsf.html>
403
404 ** SiSU uses:
405 * Standard SiSU markup syntax,
406 * Standard SiSU meta-markup syntax, and the
407 * Standard SiSU object citation numbering and system
408
409 ** Hompages:
410 <http://www.jus.uio.no/sisu>
411 <http://www.sisudoc.org>
412
413 ** Download:
414 <http://www.sisudoc.org/sisu/en/SiSU/download.html>
415
416 ** Git
417 <http://git.sisudoc.org/gitweb/?p=code/sisu.git;a=summary>
418 <http://git.sisudoc.org/gitweb/?p=code/sisu.git;a=blob;f=bin/sisu;hb=HEAD>