#!/bin/sh # # /usr/local/bin/localikiwikicreatesite # Copyright 2008 Jonas Smedegaard # # $Id: localikiwikicreatesite,v 1.18 2008-06-03 11:20:16 jonas Exp $ # # Initialize ikiwiki site # # Origin: http://ikiwiki.info/setup/ # # TODO: Explicitly replace ~ with $HOME for shell use, and $ENV{'HOME'} for Perl # TODO: Quote variables everywhere # TODO: Implement --verbose option # TODO: Check for name collision and ask (except when --force is enabled) # set -e PRG=$(basename "$0") TEMP=$(getopt -s sh -o d:s:o:b:fh -l domain:,srcdomain:,origin:,branch:,force,help -n "$PRG" -- "$@") if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi eval set -- "$TEMP" if [ -f /etc/local/ikiwiki.cfg ]; then . /etc/local/ikiwiki.cfg fi gitsrcuri="http://source.jones.dk" domain="${domain:-example.com}" srcdomain="$srcdomain" origin="$origin" branch="$branch" force="$force" showhelp() { cat <&2 "Error: $1" echo >&2 "Exiting..." exit 1 } git_setenv_work() { set -e targetdir="$1" shift export GIT_WORK_TREE="$targetdir" export GIT_DIR="$targetdir/.git" } git_unsetenv() { unset GIT_WORK_TREE unset GIT_DIR } git_init_pub() { repo="$DESTSRCDIR/$1.git" # export GIT_WORK_TREE="$repo" export GIT_DIR="$repo" mkdir -p $repo git --bare init --shared chmod +x $repo/hooks/post-update git_unsetenv } git_init_work() { set -e repo="$DESTSRCDIR/$1.git" targetdir="$2" shift 2 git_setenv_work "$targetdir" git init for ignore in "$@"; do echo "$1" >> $targetdir/.gitignore done git add . git commit -m "initial commit" git remote add origin $repo git config branch.master.remote origin git config branch.master.merge refs/heads/master git push --all git_unsetenv } while true ; do case "$1" in -d|--domain) domain="$2"; shift 2;; -s|--srcdomain) srcdomain="$2"; shift 2;; -o|--origin) origin="$2"; shift 2;; -b|--branch) branch="$2"; shift 2;; -f|--force) force="1"; shift;; -h|--help) showhelp; exit 0;; --) shift; break;; *) exit1 "Internal error!";; esac done # Resolve FQDNs project="$1" [ -n "$project" ] || exit1 "Project name must be supplied!" host="$2" [ -n "$host" ] || host="$project" echo "$host" | grep -q '\.' || host="$host.$domain" srchost="$3" [ -n "$srchost" ] || srchost="source" if echo "$srchost" | grep -qv '\.'; then [ -n "$srcdomain" ] || srcdomain=$(echo "$host" | sed 's/^www\.//') srchost="$srchost.$srcdomain" fi SRCDIR=~/private_webdata/$project/content CFGDIR=~/private_webdata/$project DESTDIR=~/public_websites/$host DESTSRCDIR=~/public_websites/$srchost ## Init public RCS repositories git_init_pub "$project" git_init_pub "${project}_content" ## Create initial basedir if [ -n "$origin" ]; then git clone $origin $CFGDIR git_setenv_work "$CFGDIR" if [ -n "$branch" ]; then git branch -f "$branch" "origin/$branch" git checkout "$branch" fi git config remote.origin.url "$DESTSRCDIR/$project.git" git push --all git_unsetenv else mkdir -p $CFGDIR cp /usr/share/doc/ikiwiki/html/ikiwiki.setup $CFGDIR fi ## Init working RCS repositories if [ -n "$origin" ]; then make -C $CFGDIR else git_init_work "$project" "$CFGDIR" "/content" fi ## Create initial content if ! [ -e $SRCDIR ]; then mkdir -p $SRCDIR cat <<'EOF' >$SRCDIR/index.mdwn Welcome to your new wiki. All wikis are supposed to have a SandBox, so this one does too. ---- This wiki is powered by [ikiwiki](http://ikiwiki.info). EOF git_init_work "${project}_content" "$SRCDIR" "/.ikiwiki" fi ## Adjust backend to actual paths, and enable Git post-update wrapper # TODO: Rewrite as semi-generic functions: #perl_param_enable_set "$CFGDIR/ikiwiki.setup" "srcdir" "$SRCDIR" #perl_param_enable_set "$CFGDIR/ikiwiki.setup" "destdir" "$DESTDIR" #perl_param_enable_set "$CFGDIR/ikiwiki.setup" "url" "$host" #perl_param_disable "$CFGDIR/ikiwiki.setup" "cgiurl" #perl_param_match_enable "$CFGDIR/ikiwiki.setup" "rcs" "git" #perl_section_match_enable_param_set "$CFGDIR/ikiwiki.setup" "git post-update wrapper" "wrapper" "$DESTSRCDIR/${project}_content.git/hooks/post-update-$project" #perl_section_match_enable_param_match_enable "$CFGDIR/ikiwiki.setup" "git post-update wrapper" "wrappermode" "06755" #shell_line_match_enable_appendlinebefore "$DESTSRCDIR/${project}_content.git/hooks/post-update" "^exec" "hooks/post-update-$project\n" perl -pi -e ' s,^(\s*)#?(srcdir\s*=>\s*")[^"]*("\,\s*),$1$2'$SRCDIR'$3,; s,^(\s*)#?(destdir\s*=>\s*")[^"]*("\,\s*),$1$2'$DESTDIR'$3,; s,^(\s*)#?(url\s*=>\s*")[^"]*("\,\s*),$1$2http://'$host'$3,; s,^(\s*)#?(cgiurl\s*=>\s*")([^"]*)("\,\s*),$1#$2$3$4,; s,^(\s*)#?(rcs\s*=>\s*")(git)("\,\s*),$1$2$3$4,; ' $CFGDIR/ikiwiki.setup perl -0 -pi -e ' 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},; ' $CFGDIR/ikiwiki.setup perl -0 -pi -e ' s,\n(exec\s[^\n]*),\nhooks/post-update-'"$project"'\n\n$1,; ' $DESTSRCDIR/${project}_content.git/hooks/post-update git_setenv_work "$CFGDIR" git add ikiwiki.setup git commit -m "Adjust ikiwiki.setup to use actual paths, and add+enable Git post-update hooks" git_unsetenv ## Add Makefile for further customization if ! [ -e $CFGDIR/Makefile ]; then cat <$CFGDIR/Makefile #underlays = basewiki smiley templates underlays = templates #locale = danish master = master #master = master-LOCALE all: \$(underlays) \$(underlays): mkdir \$@ cd \$@ && git init cd \$@ && git remote add -f -t \$(master:LOCALE=\$(locale)) -m \$(master:LOCALE=\$(locale)) origin $gitsrcuri/${project}_\$@.git cd \$@ && git merge origin EOF git_setenv_work "$CFGDIR" git add Makefile git commit -m "Setup local paths" git_unsetenv fi if [ -n "$origin" ]; then make -C $CFGDIR install else ikiwiki --verbose $SRCDIR $DESTDIR --url=http://"$host" --setup $CFGDIR/ikiwiki.setup fi git_setenv_work "$CFGDIR" git push git_unsetenv