summaryrefslogtreecommitdiff
path: root/localikiwikicreatesite
blob: a9d362311c8c57632f273dab0057bd43d5341b70 (plain)
  1. #!/bin/sh
  2. #
  3. # /usr/local/bin/localikiwikicreatesite
  4. # Copyright 2008 Jonas Smedegaard <dr@jones.dk>
  5. #
  6. # $Id: localikiwikicreatesite,v 1.17 2008-02-13 18:55:32 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:fh -l domain:,srcdomain:,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. force="$force"
  29. showhelp() {
  30. cat <<EOF
  31. Usage: $PRG [opts...] PROJECT [HOST [SRCHOST]]
  32. Options:
  33. -d, --domain If HOST is a FQDN: Ignore the DOMAIN value.
  34. If HOST is a shortname: FQDN is HOST.DOMAIN.
  35. If HOST is omitted: FQDN is PROJECT.DOMAIN.
  36. (default: $domain)
  37. -s, --srcdomain If SRCHOST is a FQDN: Ignore the SRCDOMAIN value.
  38. If SRCHOST is a shortname: FQDN is HOST.SRCDOMAIN.
  39. If SRCHOST is omitted: FQDN is source.SRCDOMAIN.
  40. (default: ${srcdomain:-$domain without any leading www.})
  41. -f, --force Update without asking for confirmation
  42. -h, --help This help text
  43. Examples:
  44. $PRG -d example.com wiki
  45. $PRG mysite www.example.com source.example.org
  46. EOF
  47. }
  48. exit1() {
  49. echo >&2 "Error: $1"
  50. echo >&2 "Exiting..."
  51. exit 1
  52. }
  53. git_init_pub() {
  54. repo="$DESTSRCDIR/$1.git"
  55. mkdir -p $repo
  56. (cd $repo
  57. git --bare init --shared
  58. )
  59. }
  60. git_init_work() {
  61. repo="$DESTSRCDIR/$1.git"
  62. targetdir="$2"
  63. shift 2
  64. (cd $targetdir
  65. git init
  66. for ignore in "$@"; do
  67. echo "$1" >> .gitignore
  68. done
  69. git add .
  70. git commit -m "initial commit"
  71. git remote add origin $repo
  72. git config branch.master.remote origin
  73. git config branch.master.merge refs/heads/master
  74. git push --all
  75. )
  76. chmod +x $repo/hooks/post-update
  77. }
  78. while true ; do
  79. case "$1" in
  80. -d|--domain) domain="$1"; shift 2;;
  81. -s|--srcdomain) srcdomain="$1"; shift 2;;
  82. -f|--force) force="1"; shift;;
  83. -h|--help) showhelp; exit 0;;
  84. --) shift; break;;
  85. *) exit1 "Internal error!";;
  86. esac
  87. done
  88. # Resolve FQDNs
  89. project="$1"
  90. [ -n "$project" ] || exit1 "ERROR: Project name must be supplied!"
  91. host="$2"
  92. [ -n "$host" ] || host="$project"
  93. echo "$host" | grep -q '\.' || host="$host.$domain"
  94. srchost="$3"
  95. [ -n "$srchost" ] || srchost="source"
  96. if echo "$srchost" | grep -qv '\.'; then
  97. [ -n "$srcdomain" ] || srcdomain=$(echo "$host" | sed 's/^www\.//')
  98. srchost="$srchost.$srcdomain"
  99. fi
  100. SRCDIR=~/private_webdata/$project/content
  101. CFGDIR=~/private_webdata/$project
  102. DESTDIR=~/public_websites/$host
  103. DESTSRCDIR=~/public_websites/$srchost
  104. ## Init public RCS repositories
  105. git_init_pub "$project"
  106. git_init_pub "${project}_content"
  107. ## Create initial content
  108. mkdir -p $SRCDIR
  109. cat <<'EOF' >$SRCDIR/index.mdwn
  110. Welcome to your new wiki.
  111. All wikis are supposed to have a <a href="../sandbox/">SandBox</a>,
  112. so this one does too.
  113. ----
  114. This wiki is powered by [ikiwiki](http://ikiwiki.info).
  115. EOF
  116. #ikiwiki --verbose $SRCDIR $DESTDIR --url=http://"$host"
  117. ## Create initial backend
  118. mkdir -p $CFGDIR
  119. cp /usr/share/doc/ikiwiki/html/ikiwiki.setup $CFGDIR
  120. ## Init working RCS repositories
  121. git_init_work "$project" "$CFGDIR" "/content"
  122. git_init_work "${project}_content" "$SRCDIR" "/.ikiwiki"
  123. ## Adjust backend to actual paths, and enable Git post-update wrapper
  124. # TODO: Rewrite as semi-generic functions:
  125. #perl_param_enable_set "$CFGDIR/ikiwiki.setup" "srcdir" "$SRCDIR"
  126. #perl_param_enable_set "$CFGDIR/ikiwiki.setup" "destdir" "$DESTDIR"
  127. #perl_param_enable_set "$CFGDIR/ikiwiki.setup" "url" "$host"
  128. #perl_param_disable "$CFGDIR/ikiwiki.setup" "cgiurl"
  129. #perl_param_match_enable "$CFGDIR/ikiwiki.setup" "rcs" "git"
  130. #perl_section_match_enable_param_set "$CFGDIR/ikiwiki.setup" "git post-update wrapper" "wrapper" "$DESTSRCDIR/${project}_content.git/hooks/post-update-$project"
  131. #perl_section_match_enable_param_match_enable "$CFGDIR/ikiwiki.setup" "git post-update wrapper" "wrappermode" "06755"
  132. #shell_line_match_enable_appendlinebefore "$DESTSRCDIR/${project}_content.git/hooks/post-update" "^exec" "hooks/post-update-$project\n"
  133. perl -pi -e '
  134. s,^(\s*)#?(srcdir\s*=>\s*")[^"]*("\,\s*),$1$2'$SRCDIR'$3,;
  135. s,^(\s*)#?(destdir\s*=>\s*")[^"]*("\,\s*),$1$2'$DESTDIR'$3,;
  136. s,^(\s*)#?(url\s*=>\s*")[^"]*("\,\s*),$1$2http://'$host'$3,;
  137. s,^(\s*)#?(cgiurl\s*=>\s*")([^"]*)("\,\s*),$1#$2$3$4,;
  138. s,^(\s*)#?(rcs\s*=>\s*")(git)("\,\s*),$1$2$3$4,;
  139. ' $CFGDIR/ikiwiki.setup
  140. perl -0 -pi -e '
  141. 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},;
  142. ' $CFGDIR/ikiwiki.setup
  143. perl -0 -pi -e '
  144. s,\n(exec\s[^\n]*),\nhooks/post-update-'"$project"'\n\n$1,;
  145. ' $DESTSRCDIR/${project}_content.git/hooks/post-update
  146. (cd $CFGDIR
  147. git add ikiwiki.setup
  148. git commit -m "Adjust ikiwiki.setup to use actual paths, and add+enable Git post-update hooks"
  149. )
  150. ## Add Makefile for further customization
  151. cat <<EOF >$CFGDIR/Makefile
  152. #underlays = basewiki smiley templates
  153. underlays = templates
  154. #locale = danish
  155. master = master
  156. #master = master-LOCALE
  157. all: \$(underlays)
  158. \$(underlays):
  159. mkdir \$@
  160. cd \$@ && git init
  161. cd \$@ && git remote add -f -t \$(master:LOCALE=\$(locale)) -m \$(master:LOCALE=\$(locale)) origin $gitsrcuri/${project}_\$@.git
  162. cd \$@ && git merge origin
  163. EOF
  164. (cd $CFGDIR
  165. git add Makefile
  166. git commit -m "Setup local paths"
  167. )
  168. ikiwiki --verbose $SRCDIR $DESTDIR --url=http://"$host" --setup $CFGDIR/ikiwiki.setup
  169. (cd $CFGDIR
  170. git push
  171. )