#!/usr/bin/env ruby # -*- coding: utf-8 -*- require 'digest/md5' require 'fileutils' require 'rack' require 'uri' require File.expand_path(File.dirname(__FILE__) + "/../lib/sekkaconfig") require File.expand_path(File.dirname(__FILE__) + "/../lib/sekka/sekkaversion") DICTDIR = File.expand_path( "~/.sekka-server" ) DICTURL = "http://sumibi.org/sekka/dict/" + SekkaVersion.version PIDFILE = DICTDIR + "/pid" TC_FILE = DICTDIR + "/SEKKA-JISYO.SMALL.tch" SUMFILE = DICTDIR + "/SEKKA-JISYO.SMALL.md5" TC_URL = DICTURL + "/SEKKA-JISYO.SMALL.tch" SUMURL = DICTURL + "/SEKKA-JISYO.SMALL.md5" TC_FILE_LIST = [ DICTDIR + "/SEKKA-JISYO.CUSTOM.tch", DICTDIR + "/SEKKA-JISYO.LARGE.tch", DICTDIR + "/SEKKA-JISYO.SMALL.tch" ] MEMCACHED = "localhost:11211" # memcahced def main if not File.directory?( DICTDIR ) Dir.mkdir( DICTDIR ) STDERR.printf( "Info: created directory [%s]\n", DICTDIR ) end # sekka-server自身のpidを書きこむ(デーモン化したときの停止用) open( PIDFILE, "w" ) {|f| f.printf( "%d\n", Process.pid ) } # 環境変数からHTTPプロキシサーバーの情報を取得する proxyHost = nil proxyPort = nil if ENV.key?( 'http_proxy' ) uri = URI.parse ENV[ 'http_proxy' ] proxyPort = uri.port proxyHost = uri.host end if not File.exist?( TC_FILE ) STDERR.printf( "Info: Downloading SEKKA-JISYO\n" ) # 辞書をダウンロードする cmd = sprintf( "curl -o %s %s", TC_FILE, TC_URL ) STDERR.printf( "Command : %s\n", cmd ) system( cmd ) cmd = sprintf( "curl -o %s %s", SUMFILE, SUMURL ) STDERR.printf( "Command : %s\n", cmd ) system( cmd ) # チェックサムを確認する downloadSum = "" open( TC_FILE ) { |f| dataBody = f.read downloadSum = Digest::MD5.hexdigest( dataBody ) } open( SUMFILE ) { |f| correctSum = f.readline.chomp.split[0] STDERR.printf( " downloaded file's MD5 : %s\n", downloadSum ) STDERR.printf( " correct MD5 : %s\n", correctSum ) if downloadSum == correctSum STDERR.printf( "Info: downloaded file [%s] verify OK.\n", TC_FILE ) else STDERR.printf( "Error: downloaded file [%s] verify NG.\n", TC_FILE ) File.unlink( TC_FILE ) exit( 1 ) end } end # 辞書ディレクトリに存在している辞書ファイルリストを作る list = TC_FILE_LIST.select { |name| File.exist?( name ) } # 設定項目をConfigオブジェクトに代入 SekkaServer::Config.setup( list[0], MEMCACHED, 12929, proxyHost, proxyPort ) # サーバースクリプトのrootディレクトリへ移動 FileUtils.cd(File.dirname(__FILE__) + "/../") # サーバー起動 Rack::Server.start( :environment => "development", :pid => nil, :Port => SekkaServer::Config.listenPort, :Host => "0.0.0.0", :AccessLog => [], :config => "./lib/sekka.ru" ) end main()