#!/bin/sh
##
##  monotone-setup.sh -- Monotone server setup
##

#   minimum command line argument handling
if [ $# -eq 0 ]; then
    echo "Usage: $0 install [<server-mail-address> [<server-password>]]"
    echo "Usage: $0 uninstall"
    exit 1
fi
cmd="$1"
shift

#   dispatch command
case "$cmd" in
    install )
        #   determine server address
        server_address="$1"
        if [ ".$server_address" = . ]; then
            server_address=`@l_shtool@ echo -e "mtn-serve@%h%d"`
        fi
        echo "++ server address: \"$server_address\""

        #   determine server key password
        server_password="$2"
        if [ ".$server_password" = . ]; then
            server_password="`@l_prefix@/bin/apg -Mncl -a1 -m12 -n1`"
        fi
        echo "++ server password: \"$server_password\""

        #   generate database
        echo "++ generating database \"@l_prefix@/var/monotone/monotone.db\""
        @l_prefix@/bin/mtn \
            db init --db="@l_prefix@/var/monotone/monotone.db"
        chown @l_rusr@:@l_rgrp@ @l_prefix@/var/monotone/monotone.db
        chmod 664 @l_prefix@/var/monotone/monotone.db

        #   generate server key
        echo "++ generating server key \"@l_prefix@/etc/monotone/monotone-server/keys/$server_address\""
        mkdir @l_prefix@/etc/monotone/monotone-server 2>&1 || true
        mkdir @l_prefix@/etc/monotone/monotone-server/keys 2>&1 || true
        chown @l_rusr@:@l_rgrp@ @l_prefix@/etc/monotone/monotone-server/keys
        chmod 775 @l_prefix@/etc/monotone/monotone-server/keys
        ( echo "$server_password"
          echo "$server_password"
        ) | @l_prefix@/bin/mtn \
            --keydir="@l_prefix@/etc/monotone/monotone-server/keys/" \
            genkey "$server_address" >/dev/null 2>&1
        chown @l_rusr@:@l_rgrp@ @l_prefix@/etc/monotone/monotone-server/keys/$server_address.*
        chmod 664 @l_prefix@/etc/monotone/monotone-server/keys/$server_address.*

        #   setting up ACLs
        echo "++ setting up server ACLs \"@l_prefix@/etc/monotone/monotone-server/{read,write}-permissions\""
        ( echo "comment \"provide read access to everyone except anonymous/nil\""
          echo "pattern \"*\""
          echo "deny \"nil\""
          echo "allow \"*\""
        ) >@l_prefix@/etc/monotone/monotone-server/read-permissions
        ( echo "*"
        ) >@l_prefix@/etc/monotone/monotone-server/write-permissions
        chown @l_rusr@:@l_rgrp@ @l_prefix@/etc/monotone/monotone-server/read-permissions
        chown @l_rusr@:@l_rgrp@ @l_prefix@/etc/monotone/monotone-server/write-permissions
        chmod 600 @l_prefix@/etc/monotone/monotone-server/read-permissions
        chmod 600 @l_prefix@/etc/monotone/monotone-server/write-permissions

        #   setting up configuration script
        echo "++ setting up server configuration script \"@l_prefix@/etc/monotone/monotone-server/monotonerc\""
        ( echo "function get_passphrase(keypair_id)"
          echo "    return \"$server_password\""
          echo "end"
        ) >@l_prefix@/etc/monotone/monotone-server/monotonerc
        chown @l_rusr@:@l_rgrp@ @l_prefix@/etc/monotone/monotone-server/monotonerc
        chmod 600 @l_prefix@/etc/monotone/monotone-server/monotonerc
        ;;

    uninstall )
        #   remove server and database related files
        echo "++ removing server and database related files"
        rm -f @l_prefix@/etc/monotone/monotone-server/monotonerc 2>&1 || true
        rm -f @l_prefix@/etc/monotone/monotone-server/read-permissions 2>&1 || true
        rm -f @l_prefix@/etc/monotone/monotone-server/write-permissions 2>&1 || true
        rm -f @l_prefix@/etc/monotone/monotone-server/keys/* 2>&1 || true
        rmdir @l_prefix@/etc/monotone/monotone-server/keys 2>&1 || true
        rmdir @l_prefix@/etc/monotone/monotone-server 2>&1 || true
        rm -f @l_prefix@/var/monotone/monotone.db 2>&1 || true
        ;;
esac

