max.sh 6.39 KB
Newer Older
1
2
#!/bin/bash

3
PORT=1984
4
DIRECTORY=$(cd `dirname $0` && pwd)
5
6
7
8
AVAILABLE_PLUGINS_DIR=$DIRECTORY/../plugins-available
ENABLED_PLUGINS_DIR=$DIRECTORY/../plugins-enabled
MAX_PLUGINS_DIR=$AVAILABLE_PLUGINS_DIR/default
CUSTOM_PLUGINS_DIR=$AVAILABLE_PLUGINS_DIR/custom
9
10
RELATIVE_MAX_PLUGINS_DIR=../plugins-available/default
RELATIVE_CUSTOM_PLUGINS_DIR=../plugins-available/custom
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
BASEX_CLIENT_BIN="basexclient"
BASEX_BIN="basex"


#checks and set basex bin
a=$(command -v basexclient)

if [ -z $a ] && [ -z $BASEX_PATH ]
then
   echo "Please install BaseX or set \$BASEX_PATH environment variable"
   exit 1
else
   if [ -z $a ]
   then
      BASEX_CLIENT_BIN=$BASEX_PATH"/bin/basexclient"
      BASEX_BIN=$BASEX_PATH"/bin/basex"
   fi
fi
29
30
31

display_usage(){
    echo ""
32
    echo "   MaX - Utilities"
33
    echo ""
34
35
36
37
38
39
40
41
    echo "   -h: Display help."
    echo "   -p: Specify BaseX port to use for db feed. Default one is "$PORT"."
    echo "   -i: Max Javascript env initialization."
    echo "   -d: Deploy the demo edition project."
    echo "   -n: Deploy new edition with its XML sources."
    echo "   --list-plugins: show plugins and status."
    echo "   --enable-plugin [plugin_name]: Enable [plugin_name] plugin."
    echo "   --disable-plugin [plugin_name]: Disable [plugin_name] plugin."
42
43
44
    echo ""
}

45
46
47
48
49
50
51
52
53
54
55
list_plugins(){
  echo -e ''
  echo -e 'MaX default plugins : '
  list_plugins_dir $MAX_PLUGINS_DIR
  echo -e ''
  echo -e 'MaX custom plugins : '
  list_plugins_dir $CUSTOM_PLUGINS_DIR
  echo -e ''
}

list_plugins_dir(){
56
  for i in `find $1/* -maxdepth 1 -type d`
57
  do
58
59
    dir=`basename $i`
    if [ -d $ENABLED_PLUGINS_DIR/$dir ]
60
    then
61
62
63
      status="ENABLED"
    else
      status="DISABLED"
64
    fi
65
    echo '   - '$dir' -> '$status
66
67
68
69
70
71
72
  done
}

enable_plugin(){
  echo -e ''
  if [ -d $ENABLED_PLUGINS_DIR/$1 ]
  then
73
74
      echo -e "Plugin $1 was already enabled."
      return
75
76
77
78
  fi

  if [ -d $MAX_PLUGINS_DIR/$1 ]
  then
79
    target=$RELATIVE_MAX_PLUGINS_DIR/$1
80
81
  else

82
    if [ -d $RELATIVE_CUSTOM_PLUGINS_DIR/$1 ]
83
    then
84
      target=$RELATIVE_CUSTOM_PLUGINS_DIR/$1
85
86
    else
      echo "Oups ! Plugin $1 does not exist."
87
      return
88
89
    fi
  fi
90
91
92
  cd $ENABLED_PLUGINS_DIR
  ln -s $target $1
  cd $DIRECTORY
93
  node $DIRECTORY/edition_manager.js -plugin $1
94
95
96
97
98
99
100
101
  echo -e "Plugin $1 successfully enabled."
  echo -e ''
}

disable_plugin(){
  echo -e ''
  if [ ! -d $ENABLED_PLUGINS_DIR/$1 ]
  then
102
      echo -e "Plugin $1 was not enabled."
103
      echo -e ''
104
      return
105
106
107
108
109
110
111
112
  fi

  rm $ENABLED_PLUGINS_DIR/$1

  echo -e "Plugin $1 successfully disabled."
  echo -e ''
}

113
114
enable_project_plugins(){
  config_file=$DIRECTORY/../editions/$1/$1_config_inc.xml
115
  plugins=$($BASEX_BIN -i$config_file "for \$i in //plugins/plugin/@name return string(\$i)")
116
117
118
119
120
  for p in $plugins
    do enable_plugin $p
  done
}

121
122
123
124
125
126
127
128
129
130
131
# 1 argument required
if [  $# -lt 1 ]
then
    display_usage
    exit 1
fi


NEW_EDITION_MODE='-n'
DEMO_MODE='-d'
INIT_MODE='-i'
132
133
134

CHOOSEN_MODE=''

135
136
137
138
139
while [[ "$#" -gt 0 ]]; do
    case $1 in
        -h) display_usage
            exit 0;;
        -p) PORT="$2"; shift ;;
Jerome Chauveau's avatar
Jerome Chauveau committed
140
        -n|-d|-i) CHOOSEN_MODE="$1"; shift ;;
141
142
143
144
        --list-plugins) list_plugins;exit 0;;
        --enable-plugin) enable_plugin $2;exit 0;;
        --disable-plugin) disable_plugin $2;exit 0;;
         *) echo "Unknown parameter passed: $1"; exit 1 ;;
145
    esac
146
    shift
147
148
done

149

150
151
152
153
154
155
if [ -z $CHOOSEN_MODE ]
then
  display_usage
  exit 1
fi

156
db_demo_feed(){
157
    db_project_feed max_demo_lorem $DIRECTORY/demo/demo_data/
158
159
160
}
#adds xml datas to a new db
db_project_feed(){
161
162
163
164
165
166
167
168
169
170
171
172
   feed_cmd=$BASEX_CLIENT_BIN" -p$PORT -w -cfeed.txt"
   echo "Please type your BaseX login/password :"
   echo "CREATE DATABASE "$1 > feed.txt
   echo "ADD "$2 >> feed.txt
   eval $feed_cmd
   ret_code=$?
   if [ $ret_code -gt 0 ]
    then echo "Cannot insert data in DB. Is yout BaseX running on "$PORT" ?"
   else
    rm feed.txt
    echo "INFO: The "$1" DB was successfully created."
   fi
173
174
}

dje's avatar
dje committed
175
demo_edition_build(){
176
  if [ ! -d $DIRECTORY/../editions ]
dje's avatar
dje committed
177
    then
178
179
      echo 'Creates "editions" directory.'
      mkdir $DIRECTORY/../editions
dje's avatar
dje committed
180
    fi
181
182

    if [ -d $DIRECTORY/../editions/demo_lorem ]; then
183
184
      echo "Removes existing demo edition."
      rm -rf $DIRECTORY/../editions/demo_lorem
chauveau's avatar
chauveau committed
185
    fi
186
187
188

    cp -r $DIRECTORY/demo/demo_edition $DIRECTORY/../editions/demo_lorem
    cp $DIRECTORY/demo/demo_lorem_config_inc.xml $DIRECTORY/../editions/demo_lorem
189
    echo "  -> resource files copy: DONE"
190
    enable_project_plugins demo_lorem
191
    node $DIRECTORY/edition_manager.js -demo
dje's avatar
dje committed
192
193
    if [ $? -ne 0 ]
    then
194
195
      echo 'Process failed'
      exit 1
dje's avatar
dje committed
196
    else
197
      format_configuration_file
dje's avatar
dje committed
198
    fi
199
200
201
}

new_edition_build(){
dje's avatar
dje committed
202
203
  if [ ! -d $DIRECTORY/../editions ]
  then
204
    echo 'Creates "editions" directory.'
dje's avatar
dje committed
205
206
    mkdir $DIRECTORY/../editions
  fi
207
  node $DIRECTORY/edition_manager.js -new $1 $2 $3
dje's avatar
dje committed
208
  format_configuration_file
209
210
211
}

format_configuration_file(){
212
    FILE=$DIRECTORY"/../configuration/configuration.xml"
213
214
215
216
217
218
    mv $FILE $FILE".bak" || exit 1
    xmllint --format $FILE".bak" > $FILE
    rm $FILE".bak"
}

init_max(){
219
    cd $DIRECTORY/..
220
    echo " -> Fetches node dependencies."
221
    npm install
222
    echo " -> Adds nodes_modules/.ignore file."
223
    #creates a .ignore file in nodejs dependencies folder
224
225
    touch $DIRECTORY"/../node_modules/.ignore"
    echo "
226
      *** MaX initialization is finished :) ***
227
";
228
229
230
}


231

232

233

234
#creates config file if not exists (.dist copy)
235
if [ ! -f $DIRECTORY/../configuration/configuration.xml ]
236
237
then
    echo "Configuration file does not exists: copying the .dist one"
238
    cp  $DIRECTORY/../configuration/configuration.dist.xml $DIRECTORY/../configuration/configuration.xml
239
240
fi

241

242
if [ $CHOOSEN_MODE == $NEW_EDITION_MODE ]
243
then
244
245
246
    read -e -p "Project ID ? " project_id
    read -e -p "XML Project type (tei, ead, ...) ? " xmlns
    read -e -p "Database path ? " db_path
247
    new_edition_build $project_id $db_path $xmlns
248
    read -e -p "XML sources path ? " data_path
249
    db_project_feed $db_path $data_path
250
    #mkdir $PWD/../editions/$project_id
251
252
253
254
255
256
    mkdir $DIRECTORY/../editions/$project_id/ui
    mkdir $DIRECTORY/../editions/$project_id/ui/css
    mkdir $DIRECTORY/../editions/$project_id/ui/js
    mkdir $DIRECTORY/../editions/$project_id/ui/images
    touch $DIRECTORY/../editions/$project_id/ui/css/$project_id.css
    cp $DIRECTORY/menu_default.xml $DIRECTORY/../editions/$project_id/menu.xml
257
258
259
    exit 0
fi

260
if [ $CHOOSEN_MODE == $DEMO_MODE ]
261
262
263
264
265
266
then
    db_demo_feed
    demo_edition_build
    exit 0
fi

267
if [ $CHOOSEN_MODE == $INIT_MODE ]
268
269
270
271
then
    init_max
    exit 0
fi