#!/usr/bin/env ruby require 'fileutils' SRVS=["dyn-srv","dyn-html","dyn-http"] CMD="dyn-ctl" DYNCTL_PLIST=File.expand_path("~/Library/LaunchAgents/local.dyn-ctl.plist") case (ARGV[0] || "start").downcase when "init" rodasrv=File.expand_path("~/RodaSrv") unless Dir.exists? rodasrv FileUtils.mkdir_p rodasrv FileUtils.mkdir File.join(rodasrv,"edit") FileUtils.mkdir File.join(rodasrv,"public") FileUtils.mkdir File.join(rodasrv,"public","pages") FileUtils.mkdir File.join(rodasrv,"public","users") FileUtils.mkdir_p File.expand_path("~/dyndoc/etc") File.open(File.expand_path("~/dyndoc/etc/dyn-html.yml"),"w") do |f| f << "---" << "\n" f << "root: " << rodasrv << "\n" f << "---" << "\n" end end user=ARGV[1] if user FileUtils.mkdir_p File.join(rodasrv,"edit",user) unless Dir.exists? File.join(rodasrv,"edit",user) FileUtils.mkdir_p File.join(rodasrv,"public","users",user) unless Dir.exists? File.join(rodasrv,"public","users",user) ["dynlib","preload","tools"].each do |dir| FileUtils.mkdir_p File.join(rodasrv,"public","users",user,dir) unless Dir.exists? File.join(rodasrv,"public","users",user,dir) end end when "browser" browser=ARGV[1] require 'dyndoc-html-servers' if browser browser=browser.downcase Dyndoc::Browser.set browser if ["firefox","safari","chrome"].include? browser else puts "Current browser is #{Dyndoc::Browser.get}" end when "unwatch" action=ARGV[1] || "ls" path=ARGV[2] require 'dyndoc-html-servers' case action when "ls" Dyndoc::HtmlServers.unwatch_ls when "add" Dyndoc::HtmlServers.unwatch_add path when "rm" Dyndoc::HtmlServers.unwatch_rm path end when "start" status=SRVS.map do |srv| `#{srv} status`.empty? end if status.any? message="at #{Time.now}: " + status.each_with_index.map { |e,i| e ? "["+SRVS[i]+"]" : nil}.compact.join("+") + " stopped and restarted!" if RUBY_PLATFORM =~ /darwin/ ##p lint_error cmd_to_display="display notification \"#{message}\" with title \"dyn status\"" `osascript -e '#{cmd_to_display}'` end puts message status.each_with_index { |e,i| `#{SRVS[i]} start` if e } end when "stop" SRVS.each {|srv| `#{srv} stop`} when "status" SRVS.each {|srv| puts srv+(`#{srv} status`.empty? ? "stopped" : "running")} when "new" if RUBY_PLATFORM =~ /darwin/ ## launchctl seems to work but actually: dyn-srv, dyn-html and dyn-http started but noit properly DYNCTL_ERR_FILE = File.join(ENV["HOME"],"dyndoc","log","")+CMD+".err" DYNCTL_OUT_FILE = File.join(ENV["HOME"],"dyndoc","log","")+CMD+".out" FileUtils.mkdir_p File.join(ENV["HOME"],"dyndoc",'log') unless File.exists? DYNCTL_PLIST plist= <<-END.sub(/CMD/,CMD).sub(/DYN_CMD/,`which #{CMD}`.strip).sub(/ERR_FILE/,DYNCTL_ERR_FILE).sub(/OUT_FILE/,DYNCTL_OUT_FILE).sub(/DYN_PATH/,ENV["PATH"]).sub(/DYN_LANG/,ENV["LANG"]).sub(/DYN_JULIA_DIR/,ENV["JULIA_DIR"]) EnvironmentVariables PATH DYN_PATH LANG DYN_LANG JULIA_DIR DYN_JULIA_DIR Label CMD ProgramArguments DYN_CMD RunAtLoad StartInterval 10 StandardErrorPath ERR_FILE StandardOutPath OUT_FILE END File.open(DYNCTL_PLIST,"w") do |f| f << plist end end end when "load" `launchctl load #{DYNCTL_PLIST}` if RUBY_PLATFORM =~ /darwin/ and File.exists? DYNCTL_PLIST when "unload" `launchctl unload #{DYNCTL_PLIST}` if RUBY_PLATFORM =~ /darwin/ and File.exists? DYNCTL_PLIST when "delete" if RUBY_PLATFORM =~ /darwin/ and File.exists? DYNCTL_PLIST `launchctl unload #{DYNCTL_PLIST}` FileUtils.rm DYNCTL_PLIST end end