#!/bin/sh

# Copyright (C) 2009 onwards Iņaki Arenaza
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details:
#
#          http://www.gnu.org/copyleft/gpl.html

# Name of the secure backup archive directory inside $CFG->dataroot
archivedir="backuparchive"
# Name of the directories to move to the secure backup archive.
backupdatadir="backupdata"
# Name of the file we'll put inside the (now) empty backup directories
# to notify teachers we moved their backups to a new place.
readmefile="README.IMPORTANT.txt"


# Show script usage
# 
usage ()
{
    cat <<EOF

Usage:
   $0 --help 
   $0 moodledataroot

Where:
   --help           Shows this help.

   moodledataroot    Path to the \$CFG->dataroot directory to process.
EOF
}

# Check that the argument is a directory and that it looks like a real
# Moodle dataroot directory, by checking that there is a cache
# subdirectory inside it; we can't use sessions as the test directory
# because we could be using database sessions and the directory is not
# created in that case).
#
check_dataroot() {
    if [ ! -d "$1" ]; then
	# Not a directory, return error
	return 1
    fi
    if [ ! -d "$1/cache" ]; then
	# Not a cache subdirectory inside, return error
	return 1
    fi

    # OK, it looks like a moodle dataroot directory
    return 0
}

# Display error message about dataroot not being a Moodle dataroot
# directory and exit with error.
# 
abort_dataroot() {
    cat <<EOF

'$1' does not look like a Moodle dataroot directory.

Aborting.
EOF
    exit 1
}

######################################################################
#
#        Main script begins here
#
######################################################################

# Check that we have the required params. If not, show usage and
# finish with error.
if [ $# -ne 1 ]; then
    usage
    exit 1
fi

# If asked to show help, display it and finish (without error)
if [ "$1" = '--help' ]; then
    usage
    exit 0
fi

# Check that the param looks like a moodledata directory. Abort if it
# doesn't
check_dataroot "$1" || abort_dataroot "$1"

# Make sure $dataroot is a clean/canonical path. So normalize it
# (remove trailing /, . or .. components, etc.). The easiest way to do
# it is by changing to the directory and then getting the canonical
# name of it with 'pwd', letting the OS do the dirty work. As we later
# need to be inside $dataroot to make some things easier, we don't
# mind changing the current directory to $dataroot here and using
# relative paths from here on.
cd "$1"
dataroot=`pwd`

# Make sure the protected archive directory exists (and it's actually
# a directory). If it's not a directory, error out
if [ \( -e ./$archivedir \) -a \( ! -d ./$archivedir \) ]; then
    cat <<EOF
$dataroot/$archivedir is not a directory.
Aborting.
EOF
    exit 1
fi
mkdir -p ./$archivedir

# Now find all the backupdata directories, but be sure to skip the
# backup archive directory (it can contain backupdata directories
# already, and we don't want to process them).
#
# We can use -print (insted of -print0) because no course directory
# can have spaces, newlines or slashes in its name (they are all
# numerical strings). This makes thing easier.
for i in `find . -type d \( -name $archivedir -prune \) -o \( -name $backupdatadir -print \)`
do
    # Get the parent dir of the backupdata dir we are processing, so
    # we can reconstruct the course directory hierarchy inside the
    # archive directory. This allows us to move backups back to their
    # original course easily.
    archivereldir=`dirname $i`
    # Create the destination directory ('course directory') inside the
    # archive directory and move the backupdata directory there.
    mkdir -p ./$archivedir/$archivereldir
    mv $i ./$archivedir/$archivereldir
    # Create an empty backupdata directory inside the original course,
    # and put a readme file there to notify teachers we moved their
    # backups to a new place.
    mkdir -p $i
    cat > $i/$readmefile <<EOF

The backup files for this course have been moved to a secure archive
directory inside \$CFG->dataroot/$archivedir

If you need to restore them, please get in contact with your site
administrator. 

Sorry for the inconveniences.

EOF
done

exit 0

