summaryrefslogtreecommitdiff
path: root/localikiwikicreatesite
blob: 525564a6a24765ffcc54031a63958d14a88b34ec (plain)
  1. #!/bin/sh
  2. #
  3. # /usr/local/bin/localikiwikicreatesite
  4. # Copyright 2008 Jonas Smedegaard <dr@jones.dk>
  5. #
  6. # $Id: localikiwikicreatesite,v 1.18 2008-06-03 11:20:16 jonas Exp $
  7. #
  8. # Initialize ikiwiki site
  9. #
  10. # Origin: http://ikiwiki.info/setup/
  11. #
  12. # TODO: Explicitly replace ~ with $HOME for shell use, and $ENV{'HOME'} for Perl
  13. # TODO: Quote variables everywhere
  14. # TODO: Implement --verbose and --quiet options (and fail if both are set)
  15. # TODO: Implement --force option
  16. # TODO: check for required helper commands (like make for originated sites)
  17. #
  18. set -e
  19. PRG=$(basename "$0")
  20. TEMP=$(getopt -s sh -o d:s:o:b:fh -l domain:,srcdomain:,origin:,branch:,force,help -n "$PRG" -- "$@")
  21. if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
  22. eval set -- "$TEMP"
  23. if [ -f /etc/local/ikiwiki.cfg ]; then
  24. . /etc/local/ikiwiki.cfg
  25. fi
  26. gitsrcuri="http://source.jones.dk"
  27. domain="${domain:-example.com}"
  28. srcdomain="$srcdomain"
  29. origin="$origin"
  30. branch="$branch"
  31. force="$force"
  32. showhelp() {
  33. cat <<EOF
  34. Usage: $PRG [opts...] PROJECT [HOST [SRCHOST]]
  35. Options:
  36. -d, --domain If HOST is a FQDN: Ignore the DOMAIN value.
  37. If HOST is a shortname: FQDN is HOST.DOMAIN.
  38. If HOST is omitted: FQDN is PROJECT.DOMAIN.
  39. (default: $domain)
  40. -s, --srcdomain If SRCHOST is a FQDN: Ignore the SRCDOMAIN value.
  41. If SRCHOST is a shortname: FQDN is HOST.SRCDOMAIN.
  42. If SRCHOST is omitted: FQDN is source.SRCDOMAIN.
  43. (default: ${srcdomain:-$domain without any leading www.})
  44. -o, --origin Clone Git repositories from origin as skeleton
  45. (default: ${origin:-do not clone.})
  46. -b, --branch Use this branch of cloned Git repository
  47. (default: ${branch:-Git default})
  48. -f, --force Update without asking for confirmation
  49. -h, --help This help text
  50. Examples:
  51. $PRG -d example.com wiki
  52. $PRG mysite www.example.com source.example.org
  53. $PRG -d example.com -o git://source.jones.dk/ikiwiki -b clean wiki
  54. EOF
  55. }
  56. exit1() {
  57. echo >&2 "Error: $1"
  58. echo >&2 "Exiting..."
  59. exit 1
  60. }
  61. ask() {
  62. echo -n "$1 (y/N)? "
  63. read response
  64. case "$response" in
  65. y|Y)
  66. :
  67. ;;
  68. *)
  69. return 1
  70. ;;
  71. esac
  72. return
  73. }
  74. git_setenv_work() {
  75. set -e
  76. targetdir="$1"
  77. shift
  78. export GIT_WORK_TREE="$targetdir"
  79. export GIT_DIR="$targetdir/.git"
  80. }
  81. git_unsetenv() {
  82. unset GIT_WORK_TREE
  83. unset GIT_DIR
  84. }
  85. git_init_pub() {
  86. repo="$DESTSRCDIR/$1.git"
  87. # export GIT_WORK_TREE="$repo"
  88. export GIT_DIR="$repo"
  89. mkdir -p $repo
  90. git --bare init --shared
  91. chmod +x $repo/hooks/post-update
  92. git_unsetenv
  93. }
  94. git_init_work() {
  95. set -e
  96. repo="$DESTSRCDIR/$1.git"
  97. targetdir="$2"
  98. shift 2
  99. git_setenv_work "$targetdir"
  100. git init
  101. for ignore in "$@"; do
  102. echo "$1" >> $targetdir/.gitignore
  103. done
  104. git add .
  105. git commit -m "initial commit"
  106. git remote add origin $repo
  107. git config branch.master.remote origin
  108. git config branch.master.merge refs/heads/master
  109. git push --all
  110. git_unsetenv
  111. }
  112. while true ; do
  113. case "$1" in
  114. -d|--domain) domain="$2"; shift 2;;
  115. -s|--srcdomain) srcdomain="$2"; shift 2;;
  116. -o|--origin) origin="$2"; shift 2;;
  117. -b|--branch) branch="$2"; shift 2;;
  118. -f|--force) force="1"; shift;;
  119. -h|--help) showhelp; exit 0;;
  120. --) shift; break;;
  121. *) exit1 "Internal error!";;
  122. esac
  123. done
  124. # Resolve FQDNs
  125. project="$1"
  126. [ -n "$project" ] || exit1 "Project name must be supplied!"
  127. host="$2"
  128. [ -n "$host" ] || host="$project"
  129. echo "$host" | grep -q '\.' || host="$host.$domain"
  130. srchost="$3"
  131. [ -n "$srchost" ] || srchost="source"
  132. if echo "$srchost" | grep -qv '\.'; then
  133. [ -n "$srcdomain" ] || srcdomain=$(echo "$host" | sed 's/^www\.//')
  134. srchost="$srchost.$srcdomain"
  135. fi
  136. SRCDIR=~/private_webdata/$project/content
  137. CFGDIR=~/private_webdata/$project
  138. DESTDIR=~/public_websites/$host
  139. DESTSRCDIR=~/public_websites/$srchost
  140. # Create parent if missing / remove if already existing
  141. # (eg. leave out SRCDIR if subdir of CFGDIR)
  142. mkdirchildlist="CFGDIR DESTDIR DESTSRCDIR"
  143. rmdirlist="CFGDIR DESTDIR DESTSRCDIR"
  144. mkdirchildfoundlist=''
  145. for item in $mkdirchildlist; do
  146. eval "childdir=\"\$$item\""
  147. parentdir="`dirname "$childdir"`"
  148. if [ ! -d "$parentdir" ]; then
  149. eval "mkdirchild_$item='(will create parent dir)'"
  150. mkdirchildfoundlist="$mkdirchildfoundlist $item"
  151. fi
  152. done
  153. rmdirfoundlist=''
  154. for item in $rmdirlist; do
  155. eval "dir=\"\$$item\""
  156. if [ -d "$dir" ]; then
  157. eval "rmdir_$item='(will replace existing!)'"
  158. rmdirfoundlist="$rmdirfoundlist $item"
  159. fi
  160. done
  161. if [ "$quiet" != 'yes' ]; then
  162. cat <<EOF
  163. You chose to make an iki site with the following settings:
  164. Project: $project
  165. Host: $host
  166. Sourcehost: $srchost
  167. The following directories will be added to your system:
  168. Project dir: $CFGDIR $rmdir_CFGDIR$mkdir_CFGDIR
  169. Content dir: $SRCDIR $rmdir_SRCDIR$mkdir_SRCDIR
  170. Central Git: $DESTSRCDIR $rmdir_DESTSRCDIR$mkdir_DESTSRCDIR
  171. Website dir: $DESTDIR $rmdir_DESTDIR$mkdir_DESTDIR
  172. EOF
  173. fi
  174. if [ -n "$mkdirchildfoundlist$rmdirfoundlist" ] || [ "$verbose" = 'yes' ]; then
  175. [ "$force" = 'yes' ] \
  176. || ask 'Continue' \
  177. || exit1 "User aborted (no changes made)"
  178. fi
  179. for item in $mkdirchildfoundlist; do
  180. eval "childdir=\"\$$item\""
  181. parentdir="`dirname "$childdir"`"
  182. if [ ! -d "$parentdir" ]; then
  183. mkdir -p "$parentdir"
  184. fi
  185. done
  186. for item in $rmdirfoundlist; do
  187. eval "dir=\"\$$item\""
  188. if [ -d "$dir" ]; then
  189. rm -rf "$dir"
  190. fi
  191. done
  192. ## Init public RCS repositories
  193. git_init_pub "$project"
  194. git_init_pub "${project}_content"
  195. ## Create initial basedir
  196. if [ -n "$origin" ]; then
  197. git clone $origin $CFGDIR
  198. git_setenv_work "$CFGDIR"
  199. if [ -n "$branch" ]; then
  200. git branch -f "$branch" "origin/$branch"
  201. git checkout "$branch"
  202. fi
  203. git config remote.origin.url "$DESTSRCDIR/$project.git"
  204. git push --all
  205. git_unsetenv
  206. else
  207. mkdir -p $CFGDIR
  208. cp /usr/share/doc/ikiwiki/html/ikiwiki.setup $CFGDIR
  209. fi
  210. ## Init working RCS repositories
  211. if [ -n "$origin" ]; then
  212. make -C $CFGDIR
  213. else
  214. git_init_work "$project" "$CFGDIR" "/content"
  215. fi
  216. ## Create initial content
  217. if ! [ -e $SRCDIR ]; then
  218. mkdir -p $SRCDIR
  219. cat <<'EOF' >$SRCDIR/index.mdwn
  220. Welcome to your new wiki.
  221. All wikis are supposed to have a <a href="../sandbox/">SandBox</a>,
  222. so this one does too.
  223. ----
  224. This wiki is powered by [ikiwiki](http://ikiwiki.info).
  225. EOF
  226. git_init_work "${project}_content" "$SRCDIR" "/.ikiwiki"
  227. fi
  228. ## Adjust backend to actual paths, and enable Git post-update wrapper
  229. # TODO: Rewrite as semi-generic functions:
  230. #perl_param_enable_set "$CFGDIR/ikiwiki.setup" "srcdir" "$SRCDIR"
  231. #perl_param_enable_set "$CFGDIR/ikiwiki.setup" "destdir" "$DESTDIR"
  232. #perl_param_enable_set "$CFGDIR/ikiwiki.setup" "url" "$host"
  233. #perl_param_disable "$CFGDIR/ikiwiki.setup" "cgiurl"
  234. #perl_param_match_enable "$CFGDIR/ikiwiki.setup" "rcs" "git"
  235. #perl_section_match_enable_param_set "$CFGDIR/ikiwiki.setup" "git post-update wrapper" "wrapper" "$DESTSRCDIR/${project}_content.git/hooks/post-update-$project"
  236. #perl_section_match_enable_param_match_enable "$CFGDIR/ikiwiki.setup" "git post-update wrapper" "wrappermode" "06755"
  237. #shell_line_match_enable_appendlinebefore "$DESTSRCDIR/${project}_content.git/hooks/post-update" "^exec" "hooks/post-update-$project\n"
  238. perl -pi -e '
  239. s,^(\s*)#?(srcdir\s*=>\s*")[^"]*("\,\s*),$1$2'$SRCDIR'$3,;
  240. s,^(\s*)#?(destdir\s*=>\s*")[^"]*("\,\s*),$1$2'$DESTDIR'$3,;
  241. s,^(\s*)#?(url\s*=>\s*")[^"]*("\,\s*),$1$2http://'$host'$3,;
  242. s,^(\s*)#?(cgiurl\s*=>\s*")([^"]*)("\,\s*),$1#$2$3$4,;
  243. s,^(\s*)#?(rcs\s*=>\s*")(git)("\,\s*),$1$2$3$4,;
  244. ' $CFGDIR/ikiwiki.setup
  245. perl -0 -pi -e '
  246. s,#{([\s#]*The git post-update wrapper[^}]*\s*)#(\s*wrapper =>\s*")[^"]*("\,\s*[^}]*)#(\s*wrappermode =>[^}]*)#},{$1$2'$DESTSRCDIR/${project}_content.git/hooks/post-update-$project'$3$4},;
  247. ' $CFGDIR/ikiwiki.setup
  248. perl -0 -pi -e '
  249. s,\n(exec\s[^\n]*),\nhooks/post-update-'"$project"'\n\n$1,;
  250. ' $DESTSRCDIR/${project}_content.git/hooks/post-update
  251. git_setenv_work "$CFGDIR"
  252. git add ikiwiki.setup
  253. git commit -m "Adjust ikiwiki.setup to use actual paths, and add+enable Git post-update hooks"
  254. git_unsetenv
  255. ## Add Makefile for further customization
  256. if ! [ -e $CFGDIR/Makefile ]; then
  257. cat <<EOF >$CFGDIR/Makefile
  258. #underlays = basewiki smiley templates
  259. underlays = templates
  260. #locale = danish
  261. master = master
  262. #master = master-LOCALE
  263. all: \$(underlays)
  264. \$(underlays):
  265. mkdir \$@
  266. cd \$@ && git init
  267. cd \$@ && git remote add -f -t \$(master:LOCALE=\$(locale)) -m \$(master:LOCALE=\$(locale)) origin $gitsrcuri/${project}_\$@.git
  268. cd \$@ && git merge origin
  269. EOF
  270. git_setenv_work "$CFGDIR"
  271. git add Makefile
  272. git commit -m "Setup local paths"
  273. git_unsetenv
  274. fi
  275. if [ -n "$origin" ]; then
  276. make -C $CFGDIR install
  277. else
  278. ikiwiki --verbose $SRCDIR $DESTDIR --url=http://"$host" --setup $CFGDIR/ikiwiki.setup
  279. fi
  280. git_setenv_work "$CFGDIR"
  281. git push
  282. git_unsetenv