wize.rb 2.44 KB
#!/usr/bin/env ruby

require 'optparse'

# Variables globales $version, $auteur et $date
$version = "0.1"
$auteur = "William Daniau"
$date = "2019 10 11"

# Parse arguments class
class ParseOptions
  def self.parse(args)
    options = Hash.new

    opt_parser = OptionParser.new do |opts|
      opts.banner = "\nUtilisation : wize.rb [options]"
      opts.separator ""
      whatIDo = <<-AFP

      liste ou applique une configuartion de template wize pour zim

            AFP

      opts.separator whatIDo

      opts.on("-l",
              "--list",
              "Liste les configurations disponibles") do |l|
        options[:list] = l
      end

      opts.on("-a",
              "--apply CONF",
              "applique la config CONF") do |conf|
        options[:conf] = conf
      end

      opts.separator ""
      opts.separator "       Options standard:"
      opts.on_tail("-h", "--help", "Affiche ce message") do
        puts opts
        puts ""
        exit
      end
      opts.on_tail("-v", "--version", "Affiche la version") do
        puts "Version : " + $version
        puts "Auteur : " + $auteur
        puts "Date : " + $date
        puts ""
        exit
      end
    end

    opt_parser.parse!(args)
    options
  end
end

def listConfigs
  a = `ls`.split(/\n/)
  names = []
  r = /^common-(.+)$/
  a.each { |f|
    if f =~ r
      names.push($1)
    end
  }
  names
end

def applyConfig(name)
  list = ["Wize", "Wize_with_index", "Wize_with_index_sod"]
  list.each { |l|
    com = "rm -f #{l}/common-spec"
    system(com)
    com = "ln -s ../common-#{name} #{l}/common-spec"
    system(com)
  }
  system("rm -f print-common")
  system("ln -s common-#{name} print-common")
end

def configInList?(name, names)
  names.include? name
end

# Parse arguments using the class and get results in a hash
options = ParseOptions.parse(ARGV)
if options.length == 0
  puts "Utiliser -h pour l'aide"
  exit
end

# Directory definitions
zimShareDir = "#{ENV['HOME']}/.local/share/zim"
zimHtmlTemplateDir = "#{zimShareDir}/templates/html"
zimConfigDir = "#{ENV['HOME']}/.config/zim"

# go into template dir
Dir.chdir(zimHtmlTemplateDir)

# get existing configs
configs = listConfigs

# List
if options[:list]
  configs.each { |c|
    puts c
  }
  exit
end

# Apply
if options[:conf]
  conf = options[:conf]
  # is it a valid config?
  if !configInList?(conf, configs)
    puts "#{conf} n'est pas une configuration valide"
    exit
  end
  # we have a valid config
  applyConfig(conf)
end