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