#!/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 cd $currPath | grep /home/) sourceDbName="orangecmf_release-1" sourceDbPrefix="qs_" dbHost="127.0.0.1" dbUser="dev" dbPass="devpass" dbPort="3306" dbName="" issueId="" projectId="" issueState="" dbPrefix="" projectName="" repoName="" projectShortName="" userIp=${SSH_CONNECTION%% *} svnUrl="http://svn.adaptainc.com" frameworkRepository="OrangeCMF" currentTag="release-1" frameworkUrl="$svnUrl/$frameworkRepository/tags/$currentTag" USER_NAME=$(whoami) BASE_PATH=`pwd | xargs dirname | xargs dirname` BRANCHE_PATH=$(echo $BASE_PATH | sed "s/\/home\/$USER_NAME\/public_html\/$frameworkRepository\///") if [ "$devenv" != "" ] ; then sourceDbName="orangecmf" frameworkUrl="$svnUrl/$frameworkRepository/$BRANCHE_PATH" cecho "RUNNING IN DEVELOPMENT ENVIRONMENT $devenv\nUSING REPOSITORY: $frameworkUrl\n" $yellow fi frameworkRev=$(svn info $frameworkUrl | grep "Last Changed Rev:" | awk '{print $4}') frameworkVer="5-rev-$frameworkRev" 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~{frameworkRev}~$frameworkRev~g" | \ sed "s~{frameworkVer}~$frameworkVer~g" | \ sed "s~{frameworkUrl}~$frameworkUrl~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() { projectName=$1 if [ "$2" != "" ] ; then dbName=$2 else dbName=$projectName fi if [ "$3" != "" ] ; then dbPrefix=$3 else dbPrefix='' fi if [ "$4" != "" ] ; then repoPath=$4 else repoPath='' fi if [ "$5" != "" ] ; then tcpdf=$5 else tcpdf='No' fi } readStdin() { echo -n "Enter YouTrack Issue: " read issueId if [ "$issueId" = "" ] ; then printError "YouTrack Issue is not defined" exit; 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 echo -n "Enter project name: " read projectName if [ "$projectName" = "" ] ; then printError "No project name" exit; fi echo 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 echo -n "Enter DB Table Prefix (example: pr_): " read dbPrefix; echo echo -n "Enter Destination Repository Name (example: somereponame): " read repoPath; echo echo -n "Include tcpdf (Y/N): " read tcpdf; tcpdf=$(echo $tcpdf | tr '[A-Z]' '[a-z]') echo } # $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 readStdin $* fi 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 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 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 "$frameworkUrl\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 "$repoPath\n" $white cecho "$svnUrl/$repoName/$repoPath\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 sudo svn_set_youtrack_project_for_repos $repoName $projectId 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 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 $frameworkUrl trunk # Removing removeAllExcept 'trunk' '.svn db site www css setup.sh setup-live.sh sync-to-me.sh remote-update.sh template-remote-checkout.sh template-remote-update.sh' removeAllExcept 'trunk/db' 'routinesAndTriggers.sql' cm mkdir trunk/.updates cm rm -Rf trunk/site/_incubator cm rm -Rf trunk/site/lib/Smarty/Smarty cm rm -Rf trunk/site/lib/AuthorizeNet 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 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 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 cm svn_add_all printHeaders "Creating externals..." libExternals="./AuthorizeNet $frameworkUrl/site/lib/AuthorizeNet" if [ "$tcpdf" = "Yes" ]; then libExternals="$libExternals\n./tcpdf $frameworkUrl/site/lib/tcpdf" fi svn propset svn:externals "$(echo -e $libExternals)" ./trunk/site/lib svn propset svn:externals "./Smarty $frameworkUrl/site/lib/Smarty/Smarty" ./trunk/site/lib/Smarty svn propset svn:externals "./ckeditor $frameworkUrl/www/js/ckeditor" ./trunk/www/js svn propset svn:externals "./scripts $frameworkUrl/scripts" ./trunk svn propset svn:ignore "userfiles thumbnails" ./trunk/www 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 echo "Finish." printHeaders "initial import ..." res=$(svn ci -m "Initial project import based on $frameworkUrl revision $frameworkRev. Issue $issueId") cm svn up cecho "Finish.\n" $green echo echo cecho "Project created successfully!\n" $green