summaryrefslogtreecommitdiff
path: root/localikiwikicreatesite
blob: 5602456f56d17f4bba5a265e1bfd8f5e5aa472b5 (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 option
  15. # TODO: Check for name collision and ask (except when --force is enabled)
  16. #
  17. set -e
  18. PRG=$(basename "$0")
  19. TEMP=$(getopt -s sh -o d:s:o:b:fh -l domain:,srcdomain:,origin:,branch:,force,help -n "$PRG" -- "$@")
  20. if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
  21. eval set -- "$TEMP"
  22. if [ -f /etc/local/ikiwiki.cfg ]; then
  23. . /etc/local/ikiwiki.cfg
  24. fi
  25. gitsrcuri="http://source.jones.dk"
  26. domain="${domain:-example.com}"
  27. srcdomain="$srcdomain"
  28. origin="$origin"
  29. branch="$branch"
  30. force="$force"
  31. showhelp() {
  32. cat <<EOF
  33. Usage: $PRG [opts...] PROJECT [HOST [SRCHOST]]
  34. Options:
  35. -d, --domain If HOST is a FQDN: Ignore the DOMAIN value.
  36. If HOST is a shortname: FQDN is HOST.DOMAIN.
  37. If HOST is omitted: FQDN is PROJECT.DOMAIN.
  38. (default: $domain)
  39. -s, --srcdomain If SRCHOST is a FQDN: Ignore the SRCDOMAIN value.
  40. If SRCHOST is a shortname: FQDN is HOST.SRCDOMAIN.
  41. If SRCHOST is omitted: FQDN is source.SRCDOMAIN.
  42. (default: ${srcdomain:-$domain without any leading www.})
  43. -o, --origin Clone Git repositories from origin as skeleton
  44. (default: ${origin:-do not clone.})
  45. -b, --branch Use this branch of cloned Git repository
  46. (default: ${branch:-Git default})
  47. -f, --force Update without asking for confirmation
  48. -h, --help This help text
  49. Examples:
  50. $PRG -d example.com wiki
  51. $PRG mysite www.example.com source.example.org
  52. $PRG -d example.com -o git://source.jones.dk/ikiwiki wiki
  53. EOF
  54. }
  55. exit1() {
  56. echo >&2 "Error: $1"
  57. echo >&2 "Exiting..."
  58. exit 1
  59. }
  60. git_setenv_work() {
  61. set -e
  62. targetdir="$1"
  63. shift
  64. export GIT_WORK_TREE="$targetdir"
  65. export GIT_DIR="$targetdir/.git"
  66. }
  67. git_unsetenv() {
  68. unset GIT_WORK_TREE
  69. unset GIT_DIR
  70. }
  71. git_init_pub() {
  72. repo="$DESTSRCDIR/$1.git"
  73. # export GIT_WORK_TREE="$repo"
  74. export GIT_DIR="$repo"
  75. mkdir -p $repo
  76. git --bare init --shared
  77. chmod +x $repo/hooks/post-update
  78. git_unsetenv
  79. }
  80. git_init_work() {
  81. set -e
  82. repo="$DESTSRCDIR/$1.git"
  83. targetdir="$2"
  84. shift 2
  85. git_setenv_work "$targetdir"
  86. git init
  87. for ignore in "$@"; do
  88. echo "$1" >> $targetdir/.gitignore
  89. done
  90. git add .
  91. git commit -m "initial commit"
  92. git remote add origin $repo
  93. git config branch.master.remote origin
  94. git config branch.master.merge refs/heads/master
  95. git push --all
  96. git_unsetenv
  97. }
  98. while true ; do
  99. case "$1" in
  100. -d|--domain) domain="$2"; shift 2;;
  101. -s|--srcdomain) srcdomain="$2"; shift 2;;
  102. -o|--origin) origin="$2"; shift 2;;
  103. -b|--branch) branch="$2"; shift 2;;
  104. -f|--force) force="1"; shift;;
  105. -h|--help) showhelp; exit 0;;
  106. --) shift; break;;
  107. *) exit1 "Internal error!";;
  108. esac
  109. done
  110. # Resolve FQDNs
  111. project="$1"
  112. [ -n "$project" ] || exit1 "Project name must be supplied!"
  113. host="$2"
  114. [ -n "$host" ] || host="$project"
  115. echo "$host" | grep -q '\.' || host="$host.$domain"
  116. srchost="$3"
  117. [ -n "$srchost" ] || srchost="source"
  118. if echo "$srchost" | grep -qv '\.'; then
  119. [ -n "$srcdomain" ] || srcdomain=$(echo "$host" | sed 's/^www\.//')
  120. srchost="$srchost.$srcdomain"
  121. fi
  122. SRCDIR=~/private_webdata/$project/content
  123. CFGDIR=~/private_webdata/$project
  124. DESTDIR=~/public_websites/$host
  125. DESTSRCDIR=~/public_websites/$srchost
  126. ## Init public RCS repositories
  127. git_init_pub "$project"
  128. git_init_pub "${project}_content"
  129. ## Create initial basedir
  130. if [ -n "$origin" ]; then
  131. git clone $origin $CFGDIR
  132. git_setenv_work "$CFGDIR"
  133. if [ -n "$branch" ]; then
  134. git branch -f "$branch" "origin/$branch"
  135. git checkout "$branch"
  136. fi
  137. git config remote.origin.url "$DESTSRCDIR/$project.git"
  138. git push --all
  139. git_unsetenv
  140. else
  141. mkdir -p $CFGDIR
  142. cp /usr/share/doc/ikiwiki/html/ikiwiki.setup $CFGDIR
  143. fi
  144. ## Init working RCS repositories
  145. if [ -n "$origin" ]; then
  146. make -C $CFGDIR
  147. else
  148. git_init_work "$project" "$CFGDIR" "/content"
  149. fi
  150. ## Create initial content
  151. if ! [ -e $SRCDIR ]; then
  152. mkdir -p $SRCDIR
  153. cat <<'EOF' >$SRCDIR/index.mdwn
  154. Welcome to your new wiki.
  155. All wikis are supposed to have a <a href="../sandbox/">SandBox</a>,
  156. so this one does too.
  157. ----
  158. This wiki is powered by [ikiwiki](http://ikiwiki.info).
  159. EOF
  160. git_init_work "${project}_content" "$SRCDIR" "/.ikiwiki"
  161. fi
  162. ## Adjust backend to actual paths, and enable Git post-update wrapper
  163. # TODO: Rewrite as semi-generic functions:
  164. #perl_param_enable_set "$CFGDIR/ikiwiki.setup" "srcdir" "$SRCDIR"
  165. #perl_param_enable_set "$CFGDIR/ikiwiki.setup" "destdir" "$DESTDIR"
  166. #perl_param_enable_set "$CFGDIR/ikiwiki.setup" "url" "$host"
  167. #perl_param_disable "$CFGDIR/ikiwiki.setup" "cgiurl"
  168. #perl_param_match_enable "$CFGDIR/ikiwiki.setup" "rcs" "git"
  169. #perl_section_match_enable_param_set "$CFGDIR/ikiwiki.setup" "git post-update wrapper" "wrapper" "$DESTSRCDIR/${project}_content.git/hooks/post-update-$project"
  170. #perl_section_match_enable_param_match_enable "$CFGDIR/ikiwiki.setup" "git post-update wrapper" "wrappermode" "06755"
  171. #shell_line_match_enable_appendlinebefore "$DESTSRCDIR/${project}_content.git/hooks/post-update" "^exec" "hooks/post-update-$project\n"
  172. perl -pi -e '
  173. s,^(\s*)#?(srcdir\s*=>\s*")[^"]*("\,\s*),$1$2'$SRCDIR'$3,;
  174. s,^(\s*)#?(destdir\s*=>\s*")[^"]*("\,\s*),$1$2'$DESTDIR'$3,;
  175. s,^(\s*)#?(url\s*=>\s*")[^"]*("\,\s*),$1$2http://'$host'$3,;
  176. s,^(\s*)#?(cgiurl\s*=>\s*")([^"]*)("\,\s*),$1#$2$3$4,;
  177. s,^(\s*)#?(rcs\s*=>\s*")(git)("\,\s*),$1$2$3$4,;
  178. ' $CFGDIR/ikiwiki.setup
  179. perl -0 -pi -e '
  180. 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},;
  181. ' $CFGDIR/ikiwiki.setup
  182. perl -0 -pi -e '
  183. s,\n(exec\s[^\n]*),\nhooks/post-update-'"$project"'\n\n$1,;
  184. ' $DESTSRCDIR/${project}_content.git/hooks/post-update
  185. git_setenv_work "$CFGDIR"
  186. git add ikiwiki.setup
  187. git commit -m "Adjust ikiwiki.setup to use actual paths, and add+enable Git post-update hooks"
  188. git_unsetenv
  189. ## Add Makefile for further customization
  190. if ! [ -e $CFGDIR/Makefile ]; then
  191. cat <<EOF >$CFGDIR/Makefile
  192. #underlays = basewiki smiley templates
  193. underlays = templates
  194. #locale = danish
  195. master = master
  196. #master = master-LOCALE
  197. all: \$(underlays)
  198. \$(underlays):
  199. mkdir \$@
  200. cd \$@ && git init
  201. cd \$@ && git remote add -f -t \$(master:LOCALE=\$(locale)) -m \$(master:LOCALE=\$(locale)) origin $gitsrcuri/${project}_\$@.git
  202. cd \$@ && git merge origin
  203. EOF
  204. git_setenv_work "$CFGDIR"
  205. git add Makefile
  206. git commit -m "Setup local paths"
  207. git_unsetenv
  208. fi
  209. if [ -n "$origin" ]; then
  210. make -C $CFGDIR install
  211. else
  212. ikiwiki --verbose $SRCDIR $DESTDIR --url=http://"$host" --setup $CFGDIR/ikiwiki.setup
  213. fi
  214. git_setenv_work "$CFGDIR"
  215. git push
  216. git_unsetenv