Blame view

wize.rb 2.44 KB
17b522613   William Daniau   Ajout d'un script...
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
  #!/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 = "
  Utilisation : 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(/
  /)
    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