1
#!/bin/bash
2
3
# Script to create a new project and trac instance
4
#
5
# 1. Create Mysql database with collate utf-8
6
#    > CREATE DATABASE <trac_project> DEFAULT CHARACTER SET utf8 COLLATE utf8_bin;
7
# 2. then execute trac-admin /path/to/trac/env initenv
8
#    > supply the db string as mysql://user:password@localhost/dbname
9
# 3. Deploy the project
10
#    > trac-admin /path/to/trac/env deploy /path/to/trac/env/deploy/
11
# 4. chmod to add group permissions
12
#    > chmod g+rwx /path/to/trac/env -R
13
# 5. Copy the template trac.ini file to be the trac.ini file and copy some
14
# config from the old trac.ini file to the new one
15
16
readConfig() {
17
  configFile='trac.config'
18
19
  if [ ! -e $configFile ]; then
20
    echo "$configFile does not exist. Make sure you have a config file in the \
21
    same path."
22
    exit 1;
23
  elif [ ! -r $configFile ]; then
24
    echo "Cannot read file $configFile. Permission denied."
25
    exit 1;
26
  fi
27
28
  tuser=$(sed -n 's/tuser=\(.*\)/\1/p' $configFile)
29
  tpassword=$(sed -n 's/tpassword=\(.*\)/\1/p' $configFile)
30
  ruser=$(sed -n 's/ruser=\(.*\)/\1/p' $configFile)
31
  rpassword=$(sed -n 's/rpassword=\(.*\)/\1/p' $configFile)
32
  rpassword=$(sed -n 's/rpassword=\(.*\)/\1/p' $configFile)
33
  trac_project=$(sed -n 's/trac_project=\(.*\)/\1/p' $configFile)
34
}
35
36
createDB() {
37
  echo "Creating DB trac_$project for $project"
38
  /usr/bin/mysql -u $ruser -p$rpassword -e "create database trac_$project \
39
  default character set utf8 collate utf8_bin;grant all privileges on \
40
  trac_$project.* to '$tuser'@'localhost' identified by '$tpassword';"
41
}
42
43
initTracEnv() {
44
  echo "Initing Env: $project"
45
  /usr/local/bin/trac-admin $trac_project$project initenv "$project" \
46
  "mysql://$tuser:$tpassword@localhost:3306/trac_$project" "git" \
47
  "/vol/www/gitorious/repositories/$project"
48
}
49
50
deployTracEnv() {
51
  echo "Deploying to $trac_project$project/deploy/"
52
  /usr/local/bin/trac-admin $trac_project$project deploy $trac_project$project"/deploy/"
53
}
54
55
chmodTracEnv() {
56
  echo "Changing permissions of $trac_project$project"
57
  chmod g+rwx $trac_project$project -R
58
}
59
60
writeTracConfig() {
61
  oldconfig="$trac_project$project/conf/trac.ini"
62
  newconfig='trac.ini.template'
63
  echo "Writing template config to $oldconfig"
64
  if [ ! -e $newconfig ]; then
65
    echo "File: $newconfig does not exist. Could not write template \
66
    configuration. You have to manually configure the config file now.\
67
    Everything else is setup."
68
    exit 0;
69
  fi
70
  name=$(sed -n 's/^name = \(.*\)$/\1/p' $oldconfig)
71
  db=$(sed -n 's/^database = \(.*\)$/\1/p' $oldconfig)
72
  db=${db//\//\\/}
73
  $(cp $newconfig $oldconfig)
74
  newconfig=$oldconfig
75
  $(sed -i "s/^name =$/name = $name/" $newconfig)
76
  $(sed -i "s/^database =$/database = $db/" $newconfig)
77
78
  # The Trac Environment needs to be upgraded.
79
  /usr/local/bin/trac-admin $trac_project$project upgrade
80
}
81
82
usage() {
83
  echo "  "
84
  echo "Usage:  "
85
	echo "$0 --name <name of the project> : Create new trac project"
86
	echo "$0 --help : Show this help"
87
  echo "  "
88
  echo "-------"
89
  echo "Trac Tools v0.1 (CC-SA) Servelots. 2013."
90
}
91
92
setup() {
93
	createDB && initTracEnv && deployTracEnv && chmodTracEnv && writeTracConfig;
94
  echo "Done.."
95
  exit 0;
96
}
97
98
if [ "$1" == "--help" ]; then
99
  usage
100
  exit 0;
101
elif [ "$1" == "--name" ]; then
102
  project=$2
103
  if [ -z $project ]; then
104
    echo "Please specify a project name."
105
    usage
106
    exit 1;
107
  fi
108
  readConfig
109
  setup
110
else
111
  echo "Invalid option."
112
  usage
113
  exit 1;
114
fi