+# Check command line options.
+# If any of them is invalid, exit the process.
+#
+def check_command_line
+ if (ARGV.length != 2)
+ usage
+ exit 2
+ end
+
+ if $options["daemon"]
+ $options["daemon"] = File.expand_path($options["daemon"], File.dirname(__FILE__))
+ unless is_writable_dir? $options["daemon"]
+ usage
+ $stderr.puts "Can not create a file in the daemon directory: %s" % [$options["daemon"]]
+ exit 5
+ end
+ end
+
+ $topdir = $options["daemon"] || File.expand_path(File.dirname(__FILE__))
+
+ if $options["player-log-dir"]
+ $options["player-log-dir"] = File.expand_path($options["player-log-dir"], $topdir)
+ unless is_writable_dir?($options["player-log-dir"])
+ usage
+ $stderr.puts "Can not write a file in the player log dir: %s" % [$options["player-log-dir"]]
+ exit 3
+ end
+ end
+
+ if $options["pid-file"]
+ $options["pid-file"] = File.expand_path($options["pid-file"], $topdir)
+ unless ShogiServer::is_writable_file? $options["pid-file"]
+ usage
+ $stderr.puts "Can not create the pid file: %s" % [$options["pid-file"]]
+ exit 4
+ end
+ end
+
+ if $options["floodgate-games"]
+ names = $options["floodgate-games"].split(",")
+ new_names =
+ names.select do |name|
+ ShogiServer::League::Floodgate::game_name?(name)
+ end
+ if names.size != new_names.size
+ $stderr.puts "Found a wrong Floodgate game: %s" % [names.join(",")]
+ exit 6
+ end
+ $options["floodgate-games"] = new_names
+ end
+
+ if $options["floodgate-history"]
+ $stderr.puts "WARNING: --floodgate-history has been deprecated."
+ $options["floodgate-history"] = nil
+ end
+end
+
+# See if a file can be created in the directory.
+# Return true if a file is writable in the directory, otherwise false.
+#
+def is_writable_dir?(dir)
+ unless File.directory? dir
+ return false
+ end
+
+ result = true
+
+ begin
+ temp_file = Tempfile.new("dummy-shogi-server", dir)
+ temp_file.close true
+ rescue
+ result = false
+ end
+
+ return result
+end
+