noosfero-plugins
3.87 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/bin/sh
set -e
program_name=$(basename $0)
if [ -e /etc/default/noosfero ]; then
  . /etc/default/noosfero
else
  NOOSFERO_DIR=$(readlink -f `dirname $0`/..)
fi
# data
available_plugins_dir="$NOOSFERO_DIR/plugins"
enabled_plugins_dir="$NOOSFERO_DIR/config/plugins"
available_plugins=$(find "$available_plugins_dir" -maxdepth 1 -mindepth 1 -type d -not -name 'template' -printf '%f\n' | sort)
enabled_plugins=$(find -L "$enabled_plugins_dir" -maxdepth 1 -mindepth 1 -type d -printf '%f\n' | sort)
disabled_plugins=$(printf "%s\n" $available_plugins $enabled_plugins_dir | sort | uniq -u)
# operation defaults
read_only=true
quiet=false
# initialization
if [ -w "$enabled_plugins_dir" ]; then
  read_only=false
fi
_list() {
  for plugin in $available_plugins; do
    echo "$plugin"
  done
}
_status() {
  for plugin in $available_plugins; do
    if [ -h "$enabled_plugins_dir/$plugin" ]; then
      status="*"
    else
      status=" "
    fi
    echo "[$status] $plugin"
  done
}
_usage() {
  echo "$program_name: manages Noosfero plugins system-wide"
  echo
  echo "Usage:"
  echo "  $program_name [OPTIONS] list"
  echo "  $program_name [OPTIONS] status"
  echo "  $program_name [OPTIONS] enable PLUGIN [PLUGIN ...]"
  echo "  $program_name [OPTIONS] disable PLUGIN [PLUGIN ...]"
  echo "  $program_name [OPTIONS] enableall"
  echo "  $program_name [OPTIONS] disableall"
  echo "  $program_name [OPTIONS] new PLUGIN [PLUGIN ...]"
  echo
  echo "Options:"
  echo "  --quiet|-q"
  echo "    Run quietly"
  echo "  --version|-v"
  echo "    Prints version information and exits"
  echo
}
_say(){
  if [ "$quiet" = 'false' ]; then
    echo $@
  fi
}
_enable(){
  plugin="$1"
  source="$available_plugins_dir/$plugin"
  target="$enabled_plugins_dir/$plugin"
  if [ -h "$target" ]; then
    _say "$plugin already enabled"
  else
    ln -s "$source" "$target"
    plugins_public_dir="$NOOSFERO_DIR/public/plugins"
    test -d "$target/public/" && ln -s "$target/public" "$plugins_public_dir/$plugin"
    _say "$plugin enabled"
  fi
}
_disable(){
  plugin="$1"
  target="$enabled_plugins_dir/$plugin"
  plugins_public_dir="$NOOSFERO_DIR/public/plugins"
  if [ -h "$target" ]; then
    rm "$target"
    test -h "$plugins_public_dir/$plugin" && rm "$plugins_public_dir/$plugin"
    _say "$plugin disabled"
  else
    _say "$plugin already disabled"
  fi
}
_new(){
  plugin=$(echo "$1" | tr '[:upper:]' '[:lower:]')
  target="$available_plugins_dir/$plugin"
  if [ -d "$target" ]; then
    _say "There is already a plugin called $plugin"
    exit 1
  else
    template="$available_plugins_dir/template"
    mkdir "$target"
    plugin_name=$(echo "$plugin" | sed -e 's/^./\u&/; s/_\(.\)/\u\1/')
    for source_file in $(find "$template" -type f); do
      target_file=$(echo "$source_file" | sed -e "s/template/$plugin/g")
      mkdir -p $(dirname "$target_file")
      sed "s/TemplatePlugin/${plugin_name}Plugin/g" "$source_file" > "$target_file"
    done
    _enable "$plugin"
  fi
}
_enableall(){
  for plugin in $available_plugins; do
    _enable "$plugin"
  done
}
_disableall() {
  for plugin in $enabled_plugins; do
    _disable "$plugin"
  done
}
if [ $# -eq 0 ]; then
  _usage
  exit 0
fi
while [ ! -z "$1" ] && [ "${1##-}" != "$1" ]; do
  opt="$1"
  shift
  case "$opt" in
    -q|--quiet)
      quiet=true
      ;;
    -v|--version)
      ruby "-I$NOOSFERO_DIR/lib" -rnoosfero -e "puts \"$program_name version #{Noosfero::VERSION}\""
      exit 0
      ;;
    *)
      echo "Unknown option: $opt"
      _usage
      exit 1
      ;;
  esac
done
command="$1"
if [ -z "$command" ]; then
  _usage
  exit 1
fi
shift
case "$command" in
  list|status|usage|enableall|disableall)
    if [ ! -z "$1" ]; then
      _usage
      exit 1
    fi
    _$command
    exit 0
    ;;
  enable|disable|new)
    for plugin in $@; do
      _$command "$plugin"
    done
    exit 0
    ;;
  *)
    echo "Unknown command: $command"
    _usage
    exit 1
    ;;
esac