#! /bin/sh -e
#
#  setup-uupc-mirror.sh - setups up the user account and SSH keys for an
#    Ubuntu-UK (http://www.ubuntu-uk.org) podcast (http://podcast.ubuntu-uk.org)
#    mirror automagically.
#
#  Copyright © 2008, 2009 Jonathan Davies <jpds@ubuntu.com>.
#
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions
#  are met:
#  1. Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer.
#  2. Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in the
#     documentation and/or other materials provided with the distribution.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
#  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
#  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
#  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
#  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
#  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
#  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
#  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
#  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
#  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#

# Check that we are running as root or with sudo privileges.
if [ $(whoami) != "root" ]; then
    echo "This script must be run as root or with 'sudo' privileges." >&2
    exit 1
fi

# Check that a directory has been specified.
if [ ! "$1" ]; then
    echo "Need a directory to set up mirror at." >&2
    echo "Usage: $0 <mirror directory>."
    exit 1
fi

# Check that the directory specified is actually a directory.
if [ ! -d "$1" ]; then
    echo "$1 is not a valid directory." >&2
    echo "Usage: $0 <mirror directory>."
    exit 1
fi

# Check that rsync is installed - this is needed for the sync process.
if [ -x "rsync" ]; then
    echo "'rsync' is not installed on this host." >&2
    echo "Please install it as it is needed for the file sync process."
    exit 1
fi

# Assign our first argument as the mirror's directory.
MIRRORDIR=$1

# Create the 'uupc' (Ubuntu-UK Podcast) group.
echo -n "Creating group 'uupc'... "
groupadd uupc
echo "done."

# Create the 'uupc' user.
echo -n "Adding user 'uupc'... "
useradd -c uupc -d /home/uupc/ -s /bin/sh -m -g uupc uupc
echo "done."

# Create an ~/.ssh/ directory for the new user.
mkdir -p /home/uupc/.ssh

# Download the Ubuntu-UK podcast mirror sync key.
wget http://podcast.ubuntu-uk.org/~daviey/authorized_keys \
    -O /home/uupc/.ssh/authorized_keys

# Verify the key we just downloaded. The key's sha1sum should be:
# 8c6cd286a82a49cb050f43ffbf442115394354cc  authorized_keys
echo "8c6cd286a82a49cb050f43ffbf442115394354cc  /home/uupc/.ssh/authorized_keys" | sha1sum -c > /dev/null 2>&1 || {
    echo "Warning: SHA1 sum mismatch on ~uupc/.ssh/authorized_keys." >&2
    exit 1
}

echo "Ubuntu-UK podcast SSH sync key fetched and validated."

# Set permissions.
echo -n "Setting permissions for ~uupc/.ssh/... "
chown -R uupc:uupc /home/uupc/.ssh
chmod 700 /home/uupc/.ssh
chmod 600 /home/uupc/.ssh/authorized_keys
echo "done."

# sed requires options that have a "/" to be "\/" - make those changes here:
MIRRORDIRSED=$(echo $MIRRORDIR | sed "s/\//\\\\\//g")

# Replace the /var/www/ text in the key file for our mirror directory.
sed -i s/"\/var\/www\/"/"$MIRRORDIRSED"/ /home/uupc/.ssh/authorized_keys

echo -n "Setting permissions for the mirror directories... "
chown -R uupc:uupc $MIRRORDIR
echo "done."

# Success.
echo "All done, please see https://wiki.ubuntu.com/UKTeam/PodcastMirroring for all other steps."
