max.sh 6.01 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
11

display_usage(){
    echo ""
12
    echo "   MaX - Utilities"
13
    echo ""
14
15
16
17
18
19
20
21
    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."
22
23
24
    echo ""
}

25
26
27
28
29
30
31
32
33
34
35
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(){
36
  for i in `find $1/* -maxdepth 1 -type d`
37
38
39
40
41
42
43
44
45
  do
    if [ ! $i == '..' ] && [ ! $i == '.' ]
    then
      if [ -d $ENABLED_PLUGINS_DIR/$i ]
      then
        status="ENABLED"
      else
        status="DISABLED"
      fi
46
47
      dir=`basename $i`
      echo '   - '$dir' -> '$status
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
    fi
  done
}

enable_plugin(){
  echo -e ''
  if [ -d $ENABLED_PLUGINS_DIR/$1 ]
  then
      echo -e "Oups ! Plugin $1 already enabled."
      echo -e ''
      exit 1
  fi

  if [ -d $MAX_PLUGINS_DIR/$1 ]
  then
    target=$MAX_PLUGINS_DIR/$1
  else

    if [ -d $CUSTOM_PLUGINS_DIR/$1 ]
    then
      target=$CUSTOM_PLUGINS_DIR/$1
    else
      echo "Oups ! Plugin $1 does not exist."
      exit 1
    fi
  fi

  ln -s $target $ENABLED_PLUGINS_DIR/$1
  echo -e "Plugin $1 successfully enabled."
  echo -e ''
}

disable_plugin(){
  echo -e ''
  if [ ! -d $ENABLED_PLUGINS_DIR/$1 ]
  then
      echo -e "Oups ! Plugin $1 was not enabled."
      echo -e ''
      exit 1
  fi

  rm $ENABLED_PLUGINS_DIR/$1

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

# 1 argument required
if [  $# -lt 1 ]
then
    display_usage
    exit 1
fi


NEW_EDITION_MODE='-n'
DEMO_MODE='-d'
INIT_MODE='-i'
106
107
108

CHOOSEN_MODE=''

109
110
111
112
113
114
115
116
117
118
while [[ "$#" -gt 0 ]]; do
    case $1 in
        -h) display_usage
            exit 0;;
        -p) PORT="$2"; shift ;;
        -n|-d|-i) CHOOSEN_MODE="$1";;
        --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 ;;
119
    esac
120
    shift
121
122
done

123

124
125
126
127
128
129
if [ -z $CHOOSEN_MODE ]
then
  display_usage
  exit 1
fi

130
db_demo_feed(){
131
    db_project_feed max_demo_lorem $DIRECTORY/demo/demo_data/
132
133
134
}
#adds xml datas to a new db
db_project_feed(){
135
    a=$(command -v basexclient)
136

137
    if [ -z $a ] && [ -z $BASEX_PATH ]
138
    then
139
140
	     echo "Please install BaseX or set \$BASEX_PATH environment variable"
	     exit 1
141
    else
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
	     if [ ! -z $a ]
	     then
	        feed_cmd="basexclient -p$PORT -w -cfeed.txt"
	     else
	        feed_cmd="$BASEX_PATH/bin/basexclient -p$PORT -w -cfeed.txt"
	     fi
	     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
159
    fi
160
161
}

dje's avatar
dje committed
162
demo_edition_build(){
163
  if [ ! -d $DIRECTORY/../editions ]
dje's avatar
dje committed
164
    then
165
166
      echo 'Creates "editions" directory.'
      mkdir $DIRECTORY/../editions
dje's avatar
dje committed
167
    fi
168
169

    if [ -d $DIRECTORY/../editions/demo_lorem ]; then
170
171
      echo "Removes existing demo edition."
      rm -rf $DIRECTORY/../editions/demo_lorem
chauveau's avatar
chauveau committed
172
    fi
173
174
175

    cp -r $DIRECTORY/demo/demo_edition $DIRECTORY/../editions/demo_lorem
    cp $DIRECTORY/demo/demo_lorem_config_inc.xml $DIRECTORY/../editions/demo_lorem
176
    echo "  -> resource files copy: DONE"
177
    node $DIRECTORY/edition_manager.js -demo
dje's avatar
dje committed
178
179
    if [ $? -ne 0 ]
    then
180
181
      echo 'Process failed'
      exit 1
dje's avatar
dje committed
182
    else
183
      format_configuration_file
dje's avatar
dje committed
184
    fi
185
186
187
}

new_edition_build(){
dje's avatar
dje committed
188
189
  if [ ! -d $DIRECTORY/../editions ]
  then
190
    echo 'Creates "editions" directory.'
dje's avatar
dje committed
191
192
    mkdir $DIRECTORY/../editions
  fi
193
  node $DIRECTORY/edition_manager.js -new $1 $2 $3
dje's avatar
dje committed
194
  format_configuration_file
195
196
197
}

format_configuration_file(){
198
    FILE=$DIRECTORY"/../configuration/configuration.xml"
199
200
201
202
203
204
    mv $FILE $FILE".bak" || exit 1
    xmllint --format $FILE".bak" > $FILE
    rm $FILE".bak"
}

init_max(){
205
    cd $DIRECTORY/..
206
    echo " -> Fetches node dependencies."
207
    npm install
208
    echo " -> Adds nodes_modules/.ignore file."
209
    #creates a .ignore file in nodejs dependencies folder
210
211
    touch $DIRECTORY"/../node_modules/.ignore"
    echo "
212
      *** MaX initialization is finished :) ***
213
";
214
215
216
}


217

218

219

220
#creates config file if not exists (.dist copy)
221
if [ ! -f $DIRECTORY/../configuration/configuration.xml ]
222
223
then
    echo "Configuration file does not exists: copying the .dist one"
224
    cp  $DIRECTORY/../configuration/configuration.dist.xml $DIRECTORY/../configuration/configuration.xml
225
226
fi

227

228
if [ $CHOOSEN_MODE == $NEW_EDITION_MODE ]
229
then
230
231
232
    read -e -p "Project ID ? " project_id
    read -e -p "XML Project type (tei, ead, ...) ? " xmlns
    read -e -p "Database path ? " db_path
233
    new_edition_build $project_id $db_path $xmlns
234
    read -e -p "XML sources path ? " data_path
235
    db_project_feed $db_path $data_path
236
    #mkdir $PWD/../editions/$project_id
237
238
239
240
241
242
    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
243
244
245
    exit 0
fi

246
if [ $CHOOSEN_MODE == $DEMO_MODE ]
247
248
249
250
251
252
then
    db_demo_feed
    demo_edition_build
    exit 0
fi

253
if [ $CHOOSEN_MODE == $INIT_MODE ]
254
255
256
257
258
then
    init_max
    exit 0
fi