This file is indexed.

/usr/bin/tdiary-setup is in tdiary-core 5.0.8-1.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
#!/usr/bin/ruby

#
# tdiary-setup --- tDiary Setup Tool
# Author: Daigo Moriwaki <daigo at debian dot org>
# Copyright (c) 2004-2011 Daigo Moriwaki
# License: GPL version 2 or later
#

require 'fileutils'
require 'webrick/httpauth/htpasswd'
require 'debian'

#
# Extend WEBrick
#
class WEBrick::HTTPAuth::Htpasswd
  def path
    @path
  end
end


module TDiarySetup
  # Directory where tdiary is installed on Debian
  TDIARY_DIR = "/usr/share/tdiary"

  #
  # Hold parameters to be used on installation.
  #
  class Parameters
    attr_accessor :user, :home, :data_path, :language
    def initialize(user = ENV['USER'], home = ENV['HOME'])
      @user = user
      @home = home
      @language  = "E"
      @data_path = "#@home/data"
      @target    = "#@home/public_html/diary"
    end

    def language_type
      case @language
      when /e/i
        :en
      when /j/i
        :ja
      when /z/i
        :zh
      else
        raise ArugumentError.new("No such a language type: #@language")
      end
    end

    def target=(t)
      @target = t
    end

    def target
      @target
    end

    def image_dir
      "#@target/images"
    end
  end # Parameters


  #
  # Interact with a user on console.
  #
  module Ask
    def read
      $stdin.gets.strip
    end

    def print(s)
         $stdout.print s
    end

    def read_with_default(default)
      r = read.strip
      return r == "" ? default : r
    end
  end

  #
  # Ask a user to input parameters.
  #
  class Input
    include Ask

    def initialize(params)
      @params = params
      @original = @params.clone
    end

    def ask
      loop do
        ask_data_path()
        ask_language()
        res = confirm()
        break if /^y$/i =~ res
      end
    end

    def ask_language
      loop do
        default = @params.language
        print "Choose English, Japanese, Traditional-Chinese [#{default}/j/z]: "
        @params.language = read_with_default(default)
        break if /^[ejz]$/i =~ @params.language
      end
    end

    def ask_data_path
      default = @params.data_path
      print "Input data_path (default: #{default}): "
      @params.data_path = read_with_default(default)
    end

    def confirm
      print "\ndata_path: %s\nlanguage: %s\n" % [@params.data_path, @params.language]
      res = nil
      loop do
        default = "Y"
        print "Correct? [#{default}/n]: "
        res = read_with_default(default)
        break if /^[yn]$/i =~ res
      end
      if /n/i =~ res
        @params = @original.clone
      end
      return res
    end
  end # Input

  #
  # Install .htpasswd file with id and password.
  #
  class Installhtpasswd
      include Ask

    def initialize(params, htpasswdOfWEBrick)
      @params = params
      @htpasswd = htpasswdOfWEBrick
    end

    def ask
      return if user_exists?(@params.user)

      first, second  = "", ""
      loop do
        print "Input password for #{@params.user} in #{@params.home}/.htpasswd: "
        first = read
        print "Input again to confirm: "
        second = read
        break if first == second
      end

      create_entry(@params.user, first)
      return first
    end

    def user_exists?(user)
       return true & @htpasswd.get_passwd("", user, false)
    end

    def create_entry(user, passwd)
      @htpasswd.set_passwd("", user, passwd)
       @htpasswd.flush
      FileUtils.chmod(0644, @htpasswd.path)
    end
  end # Htpasswd


  #
  # Install tDiary files.
  #
  class AbstractSetup
    def initialize(params)
      @params = params
    end

    def mkdir(mode, dir)
      if !File.directory?(dir)
        FileUtils.mkdir_p dir
        FileUtils.chmod(mode, dir)
      end
    end

    def hasPluginPackage?
      !Debian::Dpkg.status(["tdiary-plugin"]).packages.find_all {|pkg| pkg.installed?}.empty?
    end

    def puts(s)
      $stdout.print "#{s}\n"
    end
  end

  #
  # 'Default' mode
  #
  class DefaultSetup < AbstractSetup
    def install
      mkdir(0701, @params.data_path)
      mkdir(0701, @params.target)
      mkdir(0701, @params.image_dir)
      setup_base()
      if hasPluginPackage?
        setup_with_plugin_package()
        install_makerss
      end
    end

    def setup_base
      puts "Install tdiary files into #{@params.target}."
      install_files( ["index", "update"] )
      FileUtils.ln_sf "#{TDIARY_DIR}/js",    "#{@params.target}/"
      FileUtils.ln_sf "#{TDIARY_DIR}/theme", "#{@params.target}/"
    end

    def setup_with_plugin_package
      install_files( ["squeeze"] )
    end

    def install_files(files)
      files.each do |f|
        FileUtils.install("#{TDIARY_DIR}/debian-tools/#{f}-debian.rb", "#{@params.target}/#{f}.rb", :mode => 0701)
      end
    end

    def install_makerss
      FileUtils::touch(["#{@params.target}/index.rdf"])
      FileUtils::chmod(0701, "#{@params.target}/index.rdf")
    end
  end

  #
  # 'Symlink' mode
  #
  class SymlinkSetup < AbstractSetup
    def install
      mkdir(0777, @params.data_path)
      mkdir(0755, @params.target)
      mkdir(0777, @params.image_dir)
      setup_base()
      if hasPluginPackage?
        setup_with_plugin_package()
        install_makerss()
      end
    end

    def setup_base
      puts "Make symlinks into #{@params.target}."
      FileUtils.ln_sf "#{TDIARY_DIR}/index.rb",    "#{@params.target}/"
      FileUtils.ln_sf "#{TDIARY_DIR}/index.fcgi",  "#{@params.target}/"
      FileUtils.ln_sf "#{TDIARY_DIR}/js",          "#{@params.target}/"
      FileUtils.ln_sf "#{TDIARY_DIR}/theme",       "#{@params.target}/"
      FileUtils.ln_sf "#{TDIARY_DIR}/update.rb",   "#{@params.target}/"
      FileUtils.ln_sf "#{TDIARY_DIR}/update.fcgi", "#{@params.target}/"
    end

    def setup_with_plugin_package
      FileUtils.ln_sf "#{TDIARY_DIR}/misc/plugin/squeeze.rb", "#{@params.target}/"
      FileUtils.ln_sf "#{TDIARY_DIR}/tb.rb",                  "#{@params.target}/"
    end

    def install_makerss
      FileUtils::touch(["#{@params.target}/index.rdf"])
      FileUtils::chmod(0777, "#{@params.target}/index.rdf")
    end
  end

  #
  # 'Copy' mode
  #
  class CopySetup < AbstractSetup
    def install
      mkdir(0777, @params.data_path)
      mkdir(0755, @params.target)
      mkdir(0777, @params.image_dir)
      puts "Copy all flies. You should setup CGI files by yourself."
      FileUtils.cp_r Dir.glob("#{TDIARY_DIR}/*"), "#{@params.target}/"
    end
  end


  #
  # Install a config file.
  #
  class AbstractInstallConf
    def initialize(params)
      @params = params
    end

    # Template mothod
    def install
      lines = File.open(file_name()){|f| f.read}
      lines = sub(lines)
      write(lines)
    end

    def file_name
      raise "Not implemented yet."
    end

    def sub(lines)
      raise "Not implemented yet."
    end

    def write(lines)
      raise "Not implemented yet."
    end
  end

  #
  # Install /usr/share/tdiary/dot.htaccess to the target directory.
  #
  class Installhtaccess < AbstractInstallConf
    def file_name
      "#{TDIARY_DIR}/dot.htaccess"
    end

    def sub(lines)
      lines.gsub!(/^#Options /, "Options ")
      lines.gsub!(/foo/, @params.user)
      return lines
    end

    def write(lines)
      File.open("#{@params.target}/.htaccess", "w") {|f| f.print lines}
    end
  end

  #
  # Install tdiary.conf.sample to the target directory.
  #
  class Installtdiaryconf < AbstractInstallConf
    def write(lines)
      File.open("#{@params.target}/tdiary.conf", "w") {|f| f.print lines}
    end

    def file_name
      path = ""
      case @params.language_type
      when :en
        path << TDIARY_DIR + "/misc/i18n/tdiary.conf.sample-en"
      when :ja
        path << TDIARY_DIR + "/tdiary.conf.sample"
      when :zh
        path << TDIARY_DIR + "/misc/i18n/tdiary.conf.sample-en"
      else
        raise "No such language: #{@params.language}"
      end
      return path
    end

    def sub(lines)
      lines.gsub!(%r!a href="doc/!,     'a href="/doc/tdiary/')
      lines.gsub!(/^@data_path =.*$/,   "@data_path = '#{@params.data_path}'")
      lines.gsub!(/^#@cache_path =.*$/, "@cache_path = '/tmp/#{@params.user}_#{unique_id()}'")
      lines.gsub!(/^@options\['sp\.path'\] =.*$/, "")
      lines.gsub!(/^load_cgi_conf.*$/,  "@options['sp.path'] = '#{TDIARY_DIR}/misc/plugin'\nload_cgi_conf\n")
      case @params.language_type
      when :zh
        lines.gsub!(/^@lang = 'en'.*$/,   "# @lang = 'en'")
        lines.gsub!(/^# @lang = 'zh'.*$/, "@lang = 'zh'")
      end
      return lines
    end

    def unique_id
      return Time.now.strftime("%Y%m%d%H%M%S")
    end
  end

  #
  # Check the installed mode
  #
  class CheckMode
    def initialize(params)
      @index = "#{params.target}/index.rb"
    end

    def mode
      if !FileTest.exist?(@index)
        raise "#{@index} file not found. Is it really the installed directory?"
      end

      if symlink?
        return :symlink
      elsif copy?
        return :copy
      elsif default?
        return :default
      else
        raise "No such a mode. Check #{@index} file."
      end
    end

    def symlink?
      return FileTest.symlink?(@index)
    end

    def copy?
      return FileTest.size?(@index) > 200
    end

    def default?
      return !copy?
    end
  end
end



#
# MAIN
#

def usage
   out = <<EOF
Usage: #{$0} {default|symlink|copy|update} directory
example) #{$0} default /home/#{ENV['USER']}/public_html/diary

directory is an absolute path where CGI files of tDiary will be put.

If you set up tDiary for the fist time, select
  * default if your httpd runs under suEXEC mode, or
  * symlink, or
  * copy for manual installation.

If you update tDiary that you are using already, select update.
EOF
   $stderr.print out
end

def first_install(params, setup)
  input = TDiarySetup::Input.new(params)
  input.ask
  webrick  = WEBrick::HTTPAuth::Htpasswd.new("#{params.home}/.htpasswd")
  htpasswd = TDiarySetup::Installhtpasswd.new(params, webrick)
  htpasswd.ask

  setup.install
  htaccess = TDiarySetup::Installhtaccess.new(params)
  htaccess.install
  tdiaryconf = TDiarySetup::Installtdiaryconf.new(params)
  tdiaryconf.install
end

def update(params)
  check = TDiarySetup::CheckMode.new(params)
  case check.mode
  when :symlink
    TDiarySetup::SymlinkSetup.new(params).install
  when :copy
    TDiarySetup::CopySetup.new(params).install
  when :default
    TDiarySetup::DefaultSetup.new(params).install
  else
    raise "No such a mode: #{check.mode}"
  end
end

def check_args(args)
  if args.length != 2
    usage()
    exit 1
  end
end

def main(args)
  check_args(args)
  mode, target = args[0], args[1]

  params = TDiarySetup::Parameters.new()
  params.target = target
  case mode
  when "default"
    first_install(params, TDiarySetup::DefaultSetup.new(params))
  when "symlink"
    first_install(params, TDiarySetup::SymlinkSetup.new(params))
  when "copy"
    first_install(params, TDiarySetup::CopySetup.new(params))
  when "update"
    update(params)
  else
    usage()
    exit 1
  end
end

if __FILE__ == $0
   main(ARGV)
end