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