summaryrefslogtreecommitdiff
path: root/www/tdiary/files/tdiaryinst.rb.in
blob: da176f83bd40b8e3812b84bd6838e53985596f20 (plain) (blame)
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
#!/usr/local/bin/ruby
#
# tdiaryinstall.rb - tDiary user directory copy script
# Date created:			13 July 2003
# Whom:				KAMIYA Satosi  <mimoriso@anet.ne.jp>
#
# $FreeBSD$
#

require 'getoptlong'
require 'etc.so'
require 'fileutils'
require 'find'
require 'tempfile'

# make install�����ִ�����륰�����Х��ѿ� --tdiarymaster, --lang ���ץ����Ǿ�񤭲�ǽ
$OPT_TDIARYMASTER = "@@@@PREFIX@@@@/share/examples/tdiary"
$OPT_LANG         = '@@@@LANG@@@@'

def usage
  STDERR.print "Usage: #{File.basename($0)} [options]\n"
  STDERR.print "Options:\n"
  STDERR.print "    --help                    Display this information\n"
  STDERR.print "    --user=<username>         Specify user's login name\n"
  STDERR.print "    --diarydir=<diarydir>     Specify tDiary data directory default: diary\n"
  STDERR.print "    --httpdir=<httpdir>       Specify apache UserDirectory  default: public_html\n"
  STDERR.print "    --name=<author_name>      Specify author name\n"
  STDERR.print "    --mail=<author_mail>      Specify author mail address\n"
  STDERR.print "    --tdiarymaster=<dir>      Specify tDiary master directory default: @@@@PREFIX@@@@/share/examples/tdiary\n"
  STDERR.print "    --lang=<language>         Specify your language ('en' or 'ja') default: @@@@LANG@@@@\n"
  STDERR.print "    --suexec                  Use suExec for CGI execution\n"
  STDERR.print "    --symlink                 Use symbolic link for tDiary master files\n"
  STDERR.print "    --quiet                   Do not display any information\n"
  STDERR.print "    --noop                    Do not install any file. Use this option with --verbose\n"
  STDERR.print "    --verbose                 Verbose; display verbose debugging messages.\n"
  exit 1
end

# �������
parser = GetoptLong.new
parser.set_options(
  ['--user',    '-u', GetoptLong::REQUIRED_ARGUMENT],
  ['--diarydir','-d', GetoptLong::REQUIRED_ARGUMENT],
  ['--httpdir' ,'-h', GetoptLong::REQUIRED_ARGUMENT],
  ['--name',    '-n', GetoptLong::REQUIRED_ARGUMENT],
  ['--mail',    '-m', GetoptLong::REQUIRED_ARGUMENT],
  ['--tdiarymaster' , GetoptLong::REQUIRED_ARGUMENT],
  ['--lang'         , GetoptLong::REQUIRED_ARGUMENT],
  ['--suexec'       , GetoptLong::NO_ARGUMENT],
  ['--symlink', '-l', GetoptLong::NO_ARGUMENT],
  ['--quiet',   '-q', GetoptLong::NO_ARGUMENT],
  ['--noop'         , GetoptLong::NO_ARGUMENT],
  ['--verbose'      , GetoptLong::NO_ARGUMENT],
  ['--help'         , GetoptLong::NO_ARGUMENT])
begin
  parser.each_option do |name, arg|
    eval "$OPT_#{name.sub(/^--/, '').gsub(/-/, '_').upcase} = '#{arg}'"
  end
rescue
  raise "getoptlong"
end
usage() if defined?($OPT_HELP)

class TdiaryInstall
  attr_accessor :tdiarymaster
  attr_accessor :tdconfig
  attr_accessor :lang
  attr_reader   :euid # tdiaryinstall��¹Ԥ��Ƥ���桼��ID
  attr_accessor :username
  attr_accessor :diarydir
  attr_accessor :httpdir
  attr_reader   :passwd
  attr_accessor :fileutilOptions
  attr_accessor :author_name
  attr_accessor :author_mail
  attr_accessor :author_host
  def initialize # ����ͤ�����
    @passwd   = Etc.getpwuid() # ����ͤϥ�������桼��
    @euid     = @passwd.uid
    @username = (@passwd.name) # username=(value) �᥽�åɤǺ�������Ƥ���
    @diarydir = 'diary'
    @httpdir  = 'public_html'
    @fileutilOptions = []
    @author_name = @passwd.gecos # F.Kimura
    @author_host = "#{`hostname`.chomp}" # F.Kimura
    @author_mail = "#{@username}@#{`hostname`.chomp}"
  end

  def username=(value) # username ����������ݤ� passwd�����ѿ��⹹������
    @username = value
    @passwd = Etc.getpwnam(@username) # getpwnam(3) �ˤ�� passwd ��¤�Τ��������
    # �桼��̾��¸�ߤ��ʤ��ä���硢Etc.getpwnam() ���㳰��ȯ�����롣
    @author_name = @passwd.gecos
    @author_mail = "#{@username}@#{`hostname`.chomp}"
  end

  def lang=(value)
    case value
    when 'tdiary.conf-en' , 'en'
      @lang = 'en'
      @tdconfig = 'tdiary.conf-en'
    when 'tdiary.conf-ja' , 'ja'
      @lang = 'ja'
      @tdconfig = 'tdiary.conf-ja'
    else
      raise "Unknown Language : #{value}"
    end
  end

  def installAll
    raise "You can not use tDiary for superuser." if @passwd.uid == 0

    echo "************************************************************\n"
    echo "Starting tDiary for FreeBSD  user directory installation ...\n"
    prepareDirs()

    echo "Copy tDiary ...\n"
    if $OPT_SYMLINK then
      linkBaseFile()
    else
      copyBaseFile()
    end

    installConfig()
    setPermissions() if ! defined?($OPT_NOOP)

    echo "***\n"
    echo "You have to execute the following commands:\n"
    echo "    % /usr/local/sbin/htpasswd -c #{@passwd.dir}/.htpasswd #{@username}\n\n"
    echo "Please read #{@tdiarymaster}/README\n"
    echo "    for additional information.\n"
    echo "************************************************************\n"
  end

  def prepareDirs
    # ���󥹥ȡ�����ǥ��쥯�ȥ���Ѱ�
    if ! FileTest.exist?("#{@passwd.dir}/#{@diarydir}")
      FileUtils.mkdir_p("#{@passwd.dir}/#{@diarydir}", *@fileutilOptions)
    end
    if ! FileTest.exist?("#{@passwd.dir}/#{@httpdir}/#{@diarydir}")
      FileUtils.mkdir_p("#{@passwd.dir}/#{@httpdir}/#{@diarydir}", *@fileutilOptions)
    end
  end

  def linkBaseFile # tDiary�����ۥե�����ϥ��ԡ����ʤ�
    FileUtils.cp_r("#{@tdiarymaster}/plugin", "#{@passwd.dir}/#{@httpdir}/#{@diarydir}", :preserve, *@fileutilOptions)
    FileUtils.ln_s("#{@tdiarymaster}/theme", "#{@passwd.dir}/#{@httpdir}/#{@diarydir}", *@fileutilOptions)
    FileUtils.ln_s("#{@tdiarymaster}/doc", "#{@passwd.dir}/#{@httpdir}/#{@diarydir}", *@fileutilOptions)
    tempfile = Tempfile.new("index.rb")
    tempfile.write "#!/usr/local/bin/ruby\nrequire '#{@tdiarymaster}/index'\n"
    tempfile.close
    FileUtils.cp(tempfile.path, "#{@passwd.dir}/#{@httpdir}/#{@diarydir}/index.rb", *@fileutilOptions)
    FileUtils.chmod(0755, "#{@passwd.dir}/#{@httpdir}/#{@diarydir}/index.rb", *@fileutilOptions)
    tempfile = Tempfile.new("update.rb")
    tempfile.write "#!/usr/local/bin/ruby\nrequire '#{@tdiarymaster}/update'\n"
    tempfile.close
    FileUtils.cp(tempfile.path, "#{@passwd.dir}/#{@httpdir}/#{@diarydir}/update.rb", *@fileutilOptions)
    FileUtils.chmod(0755, "#{@passwd.dir}/#{@httpdir}/#{@diarydir}/update.rb", *@fileutilOptions)
  end

  def copyBaseFile # tDiary�����ۥե�����򤹤٤ƥ��ԡ�
    FileUtils.cp_r("#{@tdiarymaster}/", "#{@passwd.dir}/#{@httpdir}/#{@diarydir}", :preserve, *@fileutilOptions)
  end

  def installConfig
    # ����ե�������������ƥ��󥹥ȡ���
    tempfile = Tempfile.new("tdiary.conf-ja") # ���ܸ�Ķ�����ץ�
    tempfile.write tdiaryConfReplace("#{@tdiarymaster}/tdiary.conf.sample")
    tempfile.close
    FileUtils.cp(tempfile.path, "#{@passwd.dir}/#{@httpdir}/#{@diarydir}/tdiary.conf-ja", *@fileutilOptions)

    tempfile = Tempfile.new("tdiary.conf-en") # sample configuration for English Environment
    tempfile.write tdiaryConfReplace("#{@tdiarymaster}/misc/i18n/tdiary.conf.sample-en")
    tempfile.close
    FileUtils.cp(tempfile.path, "#{@passwd.dir}/#{@httpdir}/#{@diarydir}/tdiary.conf-en", *@fileutilOptions)

    if ! FileTest.exist?("#{@passwd.dir}/#{@httpdir}/#{@diarydir}/tdiary.conf") # tdiary.conf ���ʤ��������
      FileUtils.cp("#{@passwd.dir}/#{@httpdir}/#{@diarydir}/#{@tdconfig}", "#{@passwd.dir}/#{@httpdir}/#{@diarydir}/tdiary.conf", *@fileutilOptions)
    end
    # TODO: @lang���ͤˤ�ä� plugin/00lang.en.rb ���ԡ�/���������⤷����

    tempfile = Tempfile.new("dot.htaccess")
    tempfile.write dothtaccessReplace("#{@tdiarymaster}/dot.htaccess")
    tempfile.close
    FileUtils.cp(tempfile.path, "#{@passwd.dir}/#{@httpdir}/#{@diarydir}/dot.htaccess", *@fileutilOptions)
    if ! FileTest.exist?("#{@passwd.dir}/#{@httpdir}/#{@diarydir}/.htaccess")
      FileUtils.cp("#{@passwd.dir}/#{@httpdir}/#{@diarydir}/dot.htaccess", "#{@passwd.dir}/#{@httpdir}/#{@diarydir}/.htaccess", *@fileutilOptions)
    end
  end

  def setPermissions # �ե����륳�ԡ��������ʳ��ν���
    FileUtils.chmod(0777, "#{@passwd.dir}/#{@diarydir}", *@fileutilOptions) if ! defined?($OPT_SUEXEC)
    FileUtils.chmod(0777, "#{@passwd.dir}/#{@httpdir}/#{@diarydir}", *@fileutilOptions) if ! defined?($OPT_SUEXEC)
    FileUtils.rm("#{@passwd.dir}/#{@httpdir}/#{@diarydir}/tdiary-FreeBSD.sh", :force, *@fileutilOptions)

    if @euid == 0 then # superuser ���¤Ǥ��Υ��󥹥ȡ����¹Ԥ��Ƥ�����
      # ���٤ƤΥǥ��쥯�ȥꡦ�ե������ chown �ǽ�ͭ���ѹ�
      Find.find("#{@passwd.dir}/#{@diarydir}", "#{@passwd.dir}/#{@httpdir}/#{@diarydir}") do |f|
        File.chown(@passwd.uid, @passwd.gid, f)
      end
      if File::Stat.new("#{@passwd.dir}/#{@httpdir}").uid == 0 # ~/public_html �Υ����ʡ���superuser
        File.chown(@passwd.uid, @passwd.gid, "#{@passwd.dir}/#{@httpdir}")
      end
    end
  end

  def echo(s) # --quiet �����ꤵ��Ƥ��ʤ��ä����˥�å���������Ϥ���
    STDOUT.print s if ! defined?($OPT_QUIET)
  end

  def tdiaryConfReplace(filename) # ����ץ� tdiary.conf ��񤭴�����᥽�å�
    s = ''
    File.open(filename) { |fp|
      fp.each { |line|
        line = "@data_path = '#{@passwd.dir}/#{@diarydir}'\n" if line =~ /^\@data_path\s/
        line = "@author_name = '#{@author_name}'\n" if line =~ /^\@author_name\s/
        line = "@author_mail = '#{@author_mail}'\n" if line =~ /^\@author_mail\s/
        line = "@html_title = '#{@author_name} diary'\n" if line =~ /^\@html_title\s/
        line = "@index_page = 'http://#{@author_host}/~#{@username}\/'" if line =~ /^\@index_page\s/ # F.Kimura
        s += line
      }
    }
    s
  end

  def dothtaccessReplace(filename) # ����ץ� dot.htaccess ��񤭴�����᥽�å�
    s = ''
    File.open(filename) { |fp|
      fp.each { |line|
        line = "\tAuthUserFile  #{@passwd.dir}/.htpasswd\n" if line =~ /^\s*AuthUserFile\s/
        line = "\tRequire user  #{@username}\n" if line =~ /^\s*Require user\s/
        line = "Options +FollowSymLinks\n" if line =~ /^\#Options \+FollowSymLinks/ && $OPT_SYMLINK
        s += line
      }
    }
    s
  end
end

tdiaryinst = TdiaryInstall.new
tdiaryinst.tdiarymaster = $OPT_TDIARYMASTER
tdiaryinst.lang = $OPT_LANG
tdiaryinst.username = $OPT_USER if defined?($OPT_USER) # $OPT_NAME�����������Ǥʤ��Ȥ����ʤ�
tdiaryinst.diarydir = $OPT_DIARYDIR if defined?($OPT_DIARYDIR)
tdiaryinst.httpdir = $OPT_HTTPDIR if defined?($OPT_HTTPDIR)
tdiaryinst.author_name = $OPT_NAME if defined?($OPT_NAME)
tdiaryinst.author_mail = $OPT_MAIL if defined?($OPT_MAIL)
tdiaryinst.fileutilOptions.push(:noop) if defined?($OPT_NOOP)
tdiaryinst.fileutilOptions.push(:verbose) if defined?($OPT_VERBOSE)

tdiaryinst.installAll

exit 0