#!/bin/bash if [ "$1" == "-" ] ; then shift callScriptName=$(basename $1) shift else callScriptName=$(basename $0) fi black='\E[30m' red='\E[31m' green='\E[32m' yellow='\E[33m' blue='\E[34m' magenta='\E[35m' cyan='\E[36m' white='\E[37m' # Color-echo. # Argument $1 = message # Argument $2 = color cecho () { local default_msg="No message passed." # Doesn't really need to be a local variable. message=${1:-$default_msg} # Defaults to default message. color=${2:-$black} # Defaults to black, if not specified. echo -ne "$color" echo -ne "$message" echo -ne "\E[0m" return } dirName=$(readlink -f $0) currPath=$(dirname $dirName)/create_project_files devenv=$(echo $currPath | grep /home/) sourceDbName="quick_site_release-6" sourceDbPrefix="qs_" dbHost="127.0.0.1" dbUser="dev" dbPass="devpass" dbPort="3306" dbName="" issueId="" allModules="" removeModules=() removeLibs=() projectId="" issueState="" dbPrefix="" projectName="" repoName="" projectShortName="" userIp=${SSH_CONNECTION%% *} svnUrl="https://svn.orange35.com" qsfRepository="Quick_Site_Framework" currentTag="release-6" qsfUrl="$svnUrl/$qsfRepository/tags/$currentTag" USER_NAME=$(whoami) BASE_PATH=`pwd | xargs dirname | xargs dirname` BRANCHE_PATH=$(echo $BASE_PATH | sed "s/\/home\/$USER_NAME\/public_html\/$qsfRepository\///") if [ "$devenv" != "" ] ; then sourceDbName="quick_site" qsfUrl="$svnUrl/$qsfRepository/$BRANCHE_PATH" cecho "RUNNING IN DEVELOPMENT ENVIRONMENT $devenv\nUSING REPOSITORY: $qsfUrl\n" $yellow fi qsfRev=$(svn info $qsfUrl | grep "Last Changed Rev:" | awk '{print $4}') qsfVer="5-rev-$qsfRev" cm() { # cmd=$1 echo " - Executing $*" # shift initialdir=$(pwd) res=$($*) if [ "$?" != "0" ] ; then echo echo "PWD = $initialdir" printError "Failed to execute $*" fi } function printError { cecho "Error!!! $1!\n" $red; exit 1; } function printHeaders { cecho " *********************** $1 ***********************\n" $yellow } replacespecchars() { echo "$@" | sed 's~`~_~g' | \ sed 's~\!~_~g' | \ sed 's~\@~_~g' | \ sed 's~\#~_~g' | \ sed 's~\$~_~g' | \ sed 's~\%~_~g' | \ sed 's~\^~_~g' | \ sed 's~\&~_~g' | \ sed 's~\*~_~g' | \ sed 's~(~_~g' | \ sed 's~)~_~g' | \ sed 's~\+~_~g' | \ sed 's~\=~_~g' | \ sed 's~|~_~g' | \ sed 's/\\/_/g' | \ sed 's~{~_~g' | \ sed 's~}~_~g' | \ sed 's~\[~_~g' | \ sed 's~\]~_~g' | \ sed 's~\;~_~g' | \ sed 's~\:~_~g' | \ sed "s~'~_~g" | \ sed 's~"~_~g' | \ sed 's~\?~_~g' | \ sed 's~\,~_~g' | \ sed 's~\ ~_~g' | \ sed 's~\/~_~g' | \ sed 's/\~/_/g' } replacespeccharsdb() { replacespecchars "$@" | \ sed 's~\.~_~g' } replacespeccharsall() { replacespeccharsdb "$@" | \ sed 's~_~~g' | \ sed 's~-~~g' } substvars() { sed "s~{qsfRev}~$qsfRev~g" | \ sed "s~{qsfVer}~$qsfVer~g" | \ sed "s~{qsfUrl}~$qsfUrl~g" | \ sed "s~{repoName}~$repoName~g" | \ sed "s~{repoPath}~$repoPath~g" | \ sed "s~{projectUser}~$USER~g" | \ sed "s~{userIp}~$userIp~g" | \ sed "s~{projectName}~$projectName~g" | \ sed "s~{projectShortName}~$projectShortName~g" | \ sed "s~{dbHost}~$dbHost~g"| \ sed "s~$sourceDbName~$dbName~g"| \ sed "s~{dbUser}~$dbUser~g"| \ sed "s~{dbPass}~$dbPass~g"| \ sed "s~{dbPort}~$dbPort~g"| \ sed "s~{dbName}~$dbName~g"| \ sed "s~{dbPrefix}~$dbPrefix~g"| \ sed "s~$sourceDbPrefix~$dbPrefix~g" } prepare_htaccess() { htProjectName=$(echo $projectName | sed "s/\./\\\\\\\\\./g") sed "s~RewriteEngine On~RewriteEngine On\n\nRewriteCond %{HTTP_HOST} !^www\\\.$htProjectName\$ [NC]\nRewriteRule ^(.*)\$ http://www.$projectName/\$1 [R=301,L]~g" } substvarsinfile() { flc=$(cat $1) echo -e "$flc" | substvars > $1 } substvarsinfile0() { cat $1 | substvars > $1 } readCommandLine() { issueId=$1 projectName=$2 if [ "$3" != "" ] ; then dbName=$3 else dbName=$projectName fi if [ "$4" != "" ] ; then dbPrefix=$4 else dbPrefix='' fi if [ "$5" != "" ] ; then repoPath=$5 else repoPath='' fi if [ "$6" != "" ] ; then tcpdf=$6 else tcpdf='No' fi } readStdin() { if [ "$issueId" = "" ] ; then echo -n "Enter YouTrack Issue: " read issueId if [ "$issueId" = "" ] ; then printError "YouTrack Issue is not defined" exit; fi fi projectId=$(echo "$issueId" | sed -r 's/-[0-9]+//') response=$(curl -kI --user svn:postcommit https://youtrack.orange35.com/rest/issue/$issueId 2>/dev/null| grep '200 OK'); if [ ! "$response" ] ; then printError "YouTrack Issue \"$issueId\" is not found" exit; fi issueState=$(curl -k --user svn:postcommit https://youtrack.orange35.com/rest/issue/$issueId 2>/dev/null| xpath -q -e '//field[@name="State"]/value[1]/text()') if [ "$issueState" == "Closed" ] ; then printError "YouTrack Issue \"$issueId\" is $issueState" exit; fi echo if [ "$projectName" = "" ] ; then echo -n "Enter project name: " read projectName if [ "$projectName" = "" ] ; then printError "No project name" exit; fi echo fi if [ "$1" = "" ] ; then echo -n "Enter DB Name (if you want to name DB like you named the project just press 'Enter'): " read tempDBName if [ "$tempDBName" = "" ] ; then dbName=$projectName else dbName=$tempDBName fi echo fi if [ "$1" = "" ] ; then echo -n "Enter DB Table Prefix (example: pr_): " read dbPrefix; echo fi if [ "$1" = "" ] ; then echo -n "Enter Destination Repository Name (example: somereponame): " read repoPath; echo fi echo -n "Do you want to choose what modules to install (Y/N)? (default: y): " read answer; if [ "$answer" = "" ] ; then answer="y" fi echo if [[ $answer =~ ^[Yy]$ ]] ; then modules=$(svn ls $qsfUrl/site/App | grep -vE '(Cms|Admin|BrowserCheck|Captcha|Doc|FormCookie|HtmlBlock|Form|KeepSession|Lock|NotFound|Redirect|Image|Item|Service|SanityCheck|Settings|SystemLog|Sitemap|Test\/|ViewControllerLog)') for form in $(svn ls $qsfUrl/site/App/Form | grep -vE '(Abstract|FormMetaTrait.php)') do modules="${modules} Form/${form}"; done modules=$(echo $modules | tr " " "\n" | sort) for module in $modules do module=${module%/} read -p " - install $module (y/n): " -n 1 echo if [[ $REPLY =~ ^[Nn]$ ]] ; then removeModules+=($module) fi done fi if [ "$1" = "" ] ; then echo -n "Include tcpdf (Y/N): " read tcpdf; tcpdf=$(echo $tcpdf | tr '[A-Z]' '[a-z]') echo fi } # $path $excludeList removeAllExcept () { local path=${1:-''} local excludeFist=${2:-''} params="" for name in $excludeFist ; do if [[ "" == $params ]] ; then params="-name '$name'" else params="$params -o -name '$name'" fi done cmd="find $path -maxdepth 1 -mindepth 1 -not \( $params \)" for dir in $(eval $cmd); do cm rm -Rf "$dir" done } if [ "$1" != "" ] ; then echo Using commandline parameters readCommandLine $* else echo Reading user input echo echo it is possible to specify all the parameters in command line cecho "Use: $callScriptName [database name] [database preffix] [repository path] [y|n]\n" $yellow echo fi readStdin $* projectName=$(replacespecchars $projectName) projectShortName=$(replacespeccharsall $projectName) if [ "$repoPath" != "" ] ; then repoName=$repoPath repoPath=${projectName}/ else repoName=$projectName fi if [ "$tcpdf" = "y" -o "$tcpdf" = "yes" ] ; then tcpdf="Yes" else tcpdf="No" fi if [ "$dbName" != "" ] ; then echo checking for prj_ prefix in the table name $dbName dbName=$(echo $dbName | sed "s/^prj_//") fi dbName="prj_"${dbName} dbName=$(replacespeccharsdb $dbName) dbPrefix=$(replacespecchars $dbPrefix) if [ "$dbPrefix" != "" ] ; then echo checking underscore in table preffix $dbPrefix dbPrefix=$(echo $dbPrefix | sed "s/_$//") dbPrefix="${dbPrefix}_" echo $dbPrefix else cecho "EMPTY PREFFIX\n" $red fi removeModulesList=$(printf ", %s" "${removeModules[@]}") removeModulesList=${removeModulesList:1} cecho "--------------------------------\n" $yellow cecho "The following parameters will be used\n" $yellow cecho "Issue : " $yellow cecho "$issueId [$issueState]\n" $white cecho "Project ID : " $yellow cecho "$projectId\n" $white cecho "Base framework url : " $yellow cecho "$qsfUrl\n" $white cecho "Project name : " $yellow cecho "$projectName\n" $white cecho "Database name : " $yellow cecho "$dbName\n" $white cecho "Source database name : " $yellow cecho "$sourceDbName\n" $white cecho "Database tables preffix : " $yellow cecho "$dbPrefix\n" $white cecho "Repository path : " $yellow cecho "$svnUrl/$repoName/$repoPath\n" $white cecho "Remove Modules : " $yellow cecho "$removeModulesList\n" $white cecho "Include tcpdf : " $yellow cecho "$tcpdf\n" $white cecho "--------------------------------\n" $yellow cecho "Press [Enter] to continue..." $green read dummy printHeaders "Creating repository..." res=$(svn info $svnUrl/$repoName 2>/dev/null) if [ $? != 0 ] ; then echo Unable to get info about repository $repoName echo creating new repository sudo svn_create $repoName else if [ -d ~/public_html/$repoName ] ; then replc="FAILED" replcc=$red else replc="PASSED" replcc=$green fi trwww=$(svn info $svnUrl/$repoName/${repoPath}trunk/www 2>/dev/null| grep Revision) trsite=$(svn info $svnUrl/$repoName/${repoPath}trunk/site 2>/dev/null | grep Revision) if [ "$trwww" == "" ] && [ "$trsite" == "" ] ; then trwwwsite="PASSED" trwwwsitec=$green else trwwwsite="FAILED" trwwwsitec=$red fi echo Repository \'$repoName\' already present echo To create projects in already present repository the following conditions should be true echo -n "- there is no local copy in you public_html folder: " cecho "$replc\n" $replcc echo -n "- the trunk folder doesn\'t contain www and site folders: " cecho "$trwwwsite\n" $trwwwsitec echo echo -e "$res" echo if [ "$replc" == "PASSED" ] && [ "$trwwwsite" == "PASSED" ] ; then cecho "Press Enter to continue or Ctrl+Break to stop the script\n" $green read dummy else printError "Unable to continue (above conditions not meet)" fi fi sudo svn_set_youtrack_project_for_repos $repoName $projectId cm svn co $svnUrl/$repoName ~/public_html/$repoName cd ~/public_html/$repoName if [ "$repoPath" != "" ] ; then cm mkdir -p $repoPath cm svn add $repoPath cd $repoPath fi cm mkdir -p branches tags cm svn export --force $qsfUrl trunk # Removing removeAllExcept 'trunk' '.svn db site www css setup.sh setup-live.sh sync-to-me.sh remote-update.sh .remote-update' removeAllExcept 'trunk/db' 'routinesAndTriggers.sql clearPageDraft.sql emailTemplate.sql' rm trunk/.remote-update/testconfig.sh rm trunk/.remote-update/config* cm mkdir trunk/.updates cm rm -Rf trunk/site/lib/Smarty/Smarty cm rm -Rf trunk/site/lib/tcpdf cm rm -Rf trunk/www/_lib/app-config cm rm -Rf trunk/www/_lib/qs-config cm rm -Rf trunk/www/js/ckeditor cm rm -Rf trunk/www/tests cm rm -Rf trunk/site/App/Test cm rm -Rf trunk/site/tpl/Test printHeaders "Write config..." for inputFile in $(find $currPath/_lib -name '*.php' -print | grep -v '.svn'); do outputFile=$(echo $inputFile | sed "s~$currPath/_lib/~~g") mkdir -p `dirname ./trunk/www/_lib/$outputFile` cat $inputFile | substvars > ./trunk/www/_lib/$outputFile done if [[ "$tcpdf" != "Yes" ]] ; then cm rm -Rf trunk/www/_lib/qs-config/tcPdf.php cm rm -Rf trunk/site/tmp/tcpdf fi printHeaders "Write index.html ..." cat $currPath/index.html | substvars > ./index.html cat $currPath/setup.sh | substvars > ./setup.sh cat $currPath/sitemap.xml | substvars > ./trunk/site/sitemap.xml cat $currPath/remote-update-config.sh | substvars > ./trunk/.remote-update/config.sh cat ./trunk/www/.htaccess | prepare_htaccess > ./trunk/www/.htaccess_tmp cat ./trunk/www/.htaccess_tmp > ./trunk/www/.htaccess rm -f ./trunk/www/.htaccess_tmp cm chmod 755 setup.sh printHeaders "Write SQL scripts ..." cat trunk/db/routinesAndTriggers.sql | substvars > trunk/db/routinesAndTriggers-UPDATED-2014-07-03zz.sql mv -f trunk/db/routinesAndTriggers-UPDATED-2014-07-03zz.sql trunk/db/routinesAndTriggers.sql cat trunk/db/emailTemplate.sql | substvars > trunk/db/emailTemplate.sql.tmp mv -f trunk/db/emailTemplate.sql.tmp trunk/db/emailTemplate.sql cat trunk/db/clearPageDraft.sql | substvars > trunk/db/clearPageDraft-tmp.sql mv -f trunk/db/clearPageDraft-tmp.sql trunk/db/clearPageDraft.sql # Remove unused module files for module in "${removeModules[@]}" do rm -Rf trunk/site/App/$module trunk/site/tpl/$module if [ -f "$currPath/modules/$module/remove.sh" ] ; then . "$currPath/modules/$module/remove.sh" fi done libExternals="" if [ "$tcpdf" = "Yes" ] ; then libExternals="$libExternals./tcpdf $qsfUrl/site/lib/tcpdf\n" fi cm svn_add_all printHeaders "Creating externals..." svn propset svn:externals "$(echo -e $libExternals)" ./trunk/site/lib svn propset svn:externals "./Smarty $qsfUrl/site/lib/Smarty/Smarty" ./trunk/site/lib/Smarty svn propset svn:externals "./ckeditor $qsfUrl/www/js/ckeditor" ./trunk/www/js svn propset svn:externals "./scripts $qsfUrl/scripts" ./trunk svn ps svn:ignore 'hart' ./trunk/site/bin svn propset svn:ignore "userfiles thumbnails" ./trunk/www svn propset svn:ignore "vendor" ./trunk/site svn propset svn:ignore "*" ./trunk/site/tmp svn propset svn:ignore "*" ./trunk/www/images/captcha echo "CREATE DATABASE \`$dbName\` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci" | mysql -h $dbHost -P $dbPort mysqldump -h $dbHost -P $dbPort $sourceDbName | substvars | mysql -h $dbHost -P $dbPort $dbName cat $currPath/fix.sql | substvars | mysql -h $dbHost -P $dbPort $dbName cat trunk/db/routinesAndTriggers.sql | mysql -h $dbHost -P $dbPort $dbName # Remove unused module database tables for module in "${removeModules[@]}" do if [ -f "$currPath/modules/$module/remove.sql" ] ; then cecho "Removing $module module ...\n" $yellow cat $currPath/modules/$module/remove.sql | substvars | mysql -h $dbHost -P $dbPort $dbName fi done echo "Finish." printHeaders "initial import ..." res=$(svn ci -m "Initial project import based on $qsfUrl revision $qsfRev. Issue $issueId") cm svn up cecho "Finish.\n" $green echo echo cecho "Project created successfully!\n" $green