Merge branch 'master' into tizen_2.1
[platform/framework/web/web-ui-fw.git] / tools / web-ui-fw-generate-app-tpl.sh
1 #!/bin/bash
2
3 ### Prepare: set global variables ###
4 CWD=`pwd`
5 SCRIPTDIR="`cd \`dirname $0\`/; pwd`"
6 PREFIX="/usr"
7 LIBDIR=/share/tizen-web-ui-fw
8 DATA_FRAMEWORK_ROOT=
9
10
11 # Parse options 
12 # from parseopt.inc
13 # Parse commandline options, and export option variables
14 #
15 # Created by Youmin Ha <youmin.ha@samsung.com>
16 #
17 # Usage:
18 #   - Set option_list array like below:
19 #     option_list=( "--prelink" "--foo=" )
20 #   - Add this inc by following code:
21 #     . ${TOPDIR}/inc/parseopt.inc
22 #
23 #   After including parseopt.inc, argc and argv are set, excluding options in option_list.
24
25 function parseopt
26 {
27         argv=( "$@" )
28         argc=${#argv[@]}
29
30         if test ! -n "${option_list}"; then
31                 local option_list=( );  # available option list, starting --
32         fi
33
34         local idx=0
35         unset __changed
36         while [ "$idx" -lt "$argc" ];
37         do
38                 local arg=${argv[${idx}]}
39                 local tmp_idx=0
40                 
41                 for _valid_option in ${option_list[@]}
42                 do
43                         #echo "...for valid option:$_valid_option"
44                         #echo "   ...check arg:${arg:0:${#_valid_option}}"
45                         if [[ "$_valid_option" == "${arg:0:${#_valid_option}}" ]]; then
46                                 #echo "...found valid option:$_valid_option"
47                                 if [ ${_valid_option:${#_valid_option}-1} == "=" ]; then
48                                         eval "export ${arg#--}"
49                                                 else
50                                         eval "export ${arg#--}=1"
51                                 fi
52
53                                 # pull remained options
54                                 let "tmp_idx = $idx + 1"
55                                 while [ "$tmp_idx" -lt "${#argv[@]}" ]
56                                 do
57                                         argv[$tmp_idx-1]="${argv[$tmp_idx]}"
58                                         let "tmp_idx = $tmp_idx + 1"
59                                 done
60                                 unset argv[${#argv[@]}-1]
61                                 local __changed=1
62                                 break
63                         fi
64                 done
65                 if [ ! -n "$__changed" ]; then
66                         let "idx = $idx + 1"
67                 fi
68                 unset __changed
69         done
70         argc=${#argv[@]}
71 }
72
73 option_list=( "--copylib" "--type=" )
74 parseopt $@
75
76 APP_NAME=${argv[0]}
77 INSTALL_DIR=${argv[1]}
78 if [ ! -n "$INSTALL_DIR" ]; then
79         INSTALL_DIR=.
80 fi
81 DESTDIR="$INSTALL_DIR/$APP_NAME"
82
83
84 # Print usage and exit #
85 function usage
86 {
87         local EXITCODE=1
88         if [ -n "$1" ]; then local ERRMSG=$1; else local ERRMSG="Invalid arguments"; fi
89         local NO_USAGE=$2
90
91         if [ -n "$1" ]; then EXITCODE=1; echo "ERROR: $ERRMSG"; echo ""; fi
92
93         if [ -n "$1" ]; then
94                 echo "Usage: $0 <--copylib> <--type=[w3c|tizen]> <app-name> <install-dir>"
95                 echo ""
96                 echo "       app-name : Your application name. If whitespace is contained, wrap it "
97                 echo "                  by quote mark."
98                 echo "       install-dir : Directory which the template code directory with name of"
99                 echo "                  app-name is created in."
100                 echo "                  <install-dir>/<app-name>/ directory will be created."
101                 echo "       --copylib : When this option is used, all libs and resources will be "
102                 echo "                  copied into template directory, and all templates will refer"
103                 echo "                  those copied libs."
104                 echo "       --type=[w3c|tizen]"
105                 echo "                  Set type of application template. If no --type= option is given,"
106                 echo "                  the type is set to tizen by default."
107                 echo ""
108         fi
109
110         exit $EXITCODE
111 }
112
113
114 ### Check argv ###
115 function check_argv
116 {
117         if [ ! -n "$APP_NAME" ]; then usage "No app-name is given."; fi
118         if [ ! -d "$INSTALL_DIR" ]; then usage "No install-dir is found; $INSTALL_DIR"; fi
119         if [ -e "$DESTDIR" ]; then usage "$DESTDIR already exists."; fi
120 }
121
122
123 ### Copy template files into installation directory ###
124 function copy_template
125 {
126         local libpath="${PREFIX}${LIBDIR}"
127         local tplpath=$libpath/template
128
129         # Check if this script is in src script
130         if [ -e "${SCRIPTDIR}/../build/tizen-web-ui-fw" ]; then
131                 libpath="${SCRIPTDIR}/../build/tizen-web-ui-fw"
132                 tplpath=${SCRIPTDIR}/../src/template
133         fi
134
135         echo "Copying template files into $DESTDIR..."
136         mkdir -p $DESTDIR || usage "ERROR: Failed to create directory: $DESTDIR"
137         find $tplpath/ -maxdepth 1 -type f | xargs -i cp -a {} $DESTDIR/ ||  usage "ERROR: Failed to copy templates" ;
138         if [[ ! -n "$type" ]]; then
139                 type="tizen"
140         fi
141         if [[ -n "$type" && -d "$tplpath/$type" ]]; then        # Copy type-specific files
142                 cp -a $tplpath/$type/* $DESTDIR/ || usage "ERROR: Failed to copy templates"
143         fi
144
145         # copy lib if --copylib option is given
146         if [ -n "$copylib" ]; then
147                 echo "Copying libs into $DESTDIR..."
148                 cp -a ${libpath} ${DESTDIR}/    || usage "ERROR: Failed to copy libs"
149                 DATA_FRAMEWORK_ROOT="data-framework-root=\"tizen-web-ui-fw\""
150                 LIBDIR="tizen-web-ui-fw/latest/js"      # This new value is used by replace_template()
151         else   # otherwise, just set libdir
152                 LIBDIR="file://${PREFIX}${LIBDIR}/template"
153         fi
154 }
155
156
157 ### Replace filename & keywords ###
158 function replace_template
159 {
160         echo "Replacing contents of template files..."
161
162         local replace_keywords=( "APP_NAME" "LIBDIR" "DATA_FRAMEWORK_ROOT")
163
164         local in_file_path_list=`find $DESTDIR -name '*.in' -type f -print`
165         for in_file_path in ${in_file_path_list[@]}; do
166                 for keyword in "${replace_keywords[@]}"; do 
167                         eval "local val=\$$keyword"
168                         #echo "keyword=$keyword, val=$val"
169                         sed -i -e "s#@$keyword@#$val#g" $in_file_path
170                 done
171         done
172         rename 's/\.in//' $DESTDIR/*
173 }
174
175
176 ### main ###
177 check_argv
178 copy_template
179 replace_template
180