Support https in blog URLs
[dotclear.git] / createBlog.sh
1 #!/bin/bash
2
3 # Usage:
4 # ./createBlog.sh type owner baseurl
5 # type = user | connected | group-member | group-admin
6 # * user: this is a blog for a user
7 # * connected: this is a blog for a group, all connected users can post
8 # * group-member: this is a blog for a group, all the members of the group can post
9 # * group-admin: this is a blog for a group, only group admins can post
10 # owner = name of the owner
11 # * user blog: forlife of the owner of the blog
12 # * group blog: 'diminutif' of the group (from X.net database)
13 # url = full url of the blog (e.g http://group.blog-x.org/).
14
15 # WARNING: The script generates a .htaccess. The rewrite base might be edited to match
16 # the installation. Default value is based on a blog-farm of the form
17 # http://$owner.base.url/
18
19 # Once the blog has been installed and the .htaccess set-up, you can go on the administration
20 # page of the blog at baseurl/admin/
21 #
22 # Their, go to the section 'Widget' and add (by drag-n-drop) the widgets 'Auth X.org' and 'Copyright'.
23 # (should be set up by default in near future).
24
25
26 # HELPERS
27
28 usage() {
29 echo "Usage: $0 -t TYPE -o OWNER -u URL
30
31 where :
32 TYPE = user | connected | group-member | group-admin is the type of the blog
33 OWNER if the owner of the blog (user hruid or group diminutif)
34 URL is the URL of the blog
35 "
36 exit $1;
37 }
38
39 die() {
40 echo $1
41 exit 1
42 }
43
44
45 # GETOPT
46
47 TEMP=`getopt -n $0 -o ht:o:u: -- "$@"`
48
49 RET=$?
50
51 if [ $RET != 0 ]; then
52 usage $RET
53 fi
54
55 eval set -- "$TEMP"
56
57 TYPE=""
58 OWNER=""
59 URL=""
60 VHOST=0
61
62 while true ; do
63 case "$1" in
64 -h)
65 usage 0
66 ;;
67 -t)
68 TYPE=$2;
69 if [[ "${TYPE}" != "user" && "${TYPE}" != "group-member" && "${TYPE}" != "group-admin" && "${TYPE}" != "connected" ]]; then
70 echo -e "ERROR: TYPE must be one of: user | group-member | group-admin | connected\n";
71 usage 1
72 fi
73 shift 2;
74 ;;
75 -o)
76 OWNER=$2;
77 shift 2;
78 ;;
79 -u)
80 URL=$2;
81 if ! echo "${URL}" | grep -E "^https?://[^/]+/" > /dev/null; then
82 echo -e "ERROR: URL must be a full URL, e.g 'http://www.example.org/'\n";
83 usage 1
84 fi
85 shift 2;
86 ;;
87 --)
88 shift;
89 break;
90 ;;
91 *)
92 echo -e "ERROR: Invalid argument '$1'\n"
93 usage 1
94 ;;
95 esac
96 done
97
98 if [[ "x${TYPE}" == "x" || "x${OWNER}" == "x" || "x${URL}" == "x" ]]; then
99 echo -e "ERROR: Missing one of -t, -o or -u options.\n"
100 usage 1
101 fi
102
103 BASEURL=`echo "${URL}" | sed -r 's,https?://[^/]+/,/,'`
104
105 echo "Creating blog with :
106 TYPE = ${TYPE}
107 OWNER = ${OWNER}
108 URL = ${URL}
109 BASEURL = ${BASEURL}
110 "
111
112 apache_group=www-data
113 rootpath=/home/web/blogs
114 templatepath=dotclear
115
116 serviceurl="http://blog.polytechnique.org/xorgservice/createBlog"
117
118 CALLED="$serviceurl?owner=${OWNER}&type=${TYPE}&url=${URL}&baseurl=${BASEURL}"
119 echo "Calling $CALLED"
120 ( wget "$CALLED" -O - 2> /dev/null | grep 'blog created' ) || die "Blog creation failed"
121
122 ( cd $rootpath && mkdir -p $OWNER ) || die "Can't create the repository for the blog ($rootpath/$OWNER)"
123
124 cd $rootpath/$OWNER
125 for i in admin db inc index.php locales plugins themes; do
126 ln -s $rootpath/$templatepath/$i || die "Can't add path to $i"
127 done
128 mkdir -p "$rootpath/$templatepath/public/$OWNER"
129 chgrp -R "$apache_group" "$rootpath/$templatepath/public/$OWNER"
130 chmod g+wr "$rootpath/$templatepath/public/$OWNER"
131 ln -s $rootpath/$templatepath/public/$OWNER public
132
133 ( cat <<EOF
134 RewriteEngine On
135 RewriteBase ${BASEURL}
136 RewriteCond %{REQUEST_FILENAME} !-f
137 RewriteCond %{REQUEST_FILENAME} !-d
138 RewriteRule (.*) index.php/\$1
139 RewriteRule ^index.php\$ index.php/
140 SetEnv DC_BLOG_ID $OWNER
141 EOF
142 ) > .htaccess