#!/bin/bash
#
# version: 0.92    2004-11-1
#
# packup_wwwofflebook --copy-all
# packup_wwwofflebook --create-new
# packup_wwwofflebook -h | --help
#
# Collect the installed wwwofflebook related files into the originally 
# tarballs directory structure, to create a new tarball (including your
# modifications) that can be shipped again.
# Soucres wwwofflebook.conf.
# You MUST CONFIGURE this script in the CONFIG section below !
#
# Note that if you follow the installation policy and install anything
# into /opt, you won't need this script. Instead, just tar.bz2 it up with:
# tar -cvj  --directory=/opt --file /tmp/wwwofflebook wwwofflebook
# This will create a tarball in /tmp, with a relative root dir 'wwwofflebook'.
#
#
# The script copies actually installed (running) files into the 'installation'
# directory extracted of the original tarball, and creates a new tarball that 
# now includes any modifications you might have made, so you can ship it again.
# Files not defined in the script (e.g. your own READMEs) will be preserved.
# Let's call them 'static files'.
# If you create a new dir structure from the scratch, of course, there are
# no static files at all, in the beginning. You may copy or create some 
# manually then. 
# Creating the tarball will not delete the original directory structure.
# The tarball destination also is configured below.
#
# Options: 
#
# This script reads only one option !
#
# --create-new  (Implies --copy-all)
# Create the hardcoded directory structure from the scratch at the location
# $topdir, as defined in the configuration header.
# Note that creating directories may need adequate permissions.
# Newly created directories' access permissions are set to owner:group 
# as defined below (for debian, my proposal is proxy:proxy). 
# Note that there are hardcoded exceptions ! See marker 'PERMISSIONS'. 
# Apparently the caller of the script must have the right to
# write to the target directory and to change ownerships to the configured 
# values. This most probably means to invoke the script as root !
# The option --create-new won't overwrite existing directories (i hope :). 
#
# --copy-all 
# Copy all files. Implied in --create-new. 
# By default (without any commandline option) these files are omitted:
# (1) Your real purge.conf.
# If you want to ship an example purge.conf based on your private one, probably
# you want to modify the example manually (eg. for privacy), and then this file
# shouldn't not be overwritten again, if not requested explicitly with this 
# option.  All --copy-all items are hardcoded and marked COPY-ALL in the script.


# =================== CONFIG BEGIN =========================
#
# Generally, uppecase are read pathes, lowercase are write destinations.
#
CONFIG=/home/michl/.wwwoffle/wwwofflebook.conf
source $CONFIG
# This is derived from the wwwofflebook.conf environment:
README=$userwwwoffle/README__wwwofflebook
#
# Only for --create-new: Some pathes from where to get the stuff.
# (1)  Directory where the actually running scripts are located.
SCRIPTS=/opt/wwwofflebook/scripts
# (2) The original 'source' library:
LIBRARY=/opt/wwwofflebook/library
# (3) that's all :)
#
# ---------- Destinations
#
# The directory containing the subdirectories where to copy the files. 
# This will automatically be the tarball name and its directory-root 
# after extraction - e.g. if you extract the tarball in /tmp, 
# it will become /tmp/wwwofflebook.
tardir_name=wwwofflebook
#
# Top *target* directory where to copy files into.
# This is also where to create the new tree with --create-new ! 
topdir=/tmp/$tardir_name      # example for --create-new
# topdir=/opt/$tardir_name        # example for default (no copy-all)
#
# Subdirectories matching the original tarball structure.
# This list is also assumed at the points marked 'CREATE-NEW' below.
userdir=$topdir/userdir
userdirwwwoffle=$topdir/userdir/.wwwoffle
wwwoffledir=$topdir/wwwoffledir
scripts=$topdir/scripts
library=$topdir/library
import=$topdir/library/import
import=$topdir/library/locales

# Full pathname of the new tarball
tarball=/tmp/wwwofflebook.tar.bz2
#
# ---------- Access rights of new created directories
# Directories (and in some cases their file content) will be set to this.
# There are lots of exceptions hardcoded, see marker 'PERMISSIONS' !
DAEMON_GROUP=`getval $WWWOFFLE_CONF "run-gid"` # need this more often.
#
owner=$DAEMON_GROUP    # alternativly e.g. root
group=$DAEMON_GROUP    # alternativly e.g. staff (a debian group)
# Please specify access permissions octal. Examples:
# (1) 750 = rwx r-x --- (2) 770 = rwx rwx --- (3) 775 = rwx rwx r-x
dir_perm=770
#
# =================== CONFIG END =====================

clear; [ "$1" = "-h" ] && exec less $0

# HACKING THIS SCRIPT:
# Some useful 'cp' options:
# v=verbose, f=force overwrite, p=preserve attributes and r=recursive.
# Use $savecopy (defined in preconfig section below) if you want to copy 
# symliks as file content rather than as links, and no recursion. 
# Use cp -vfa if you want to preserve anything possible including symlinks, 
# and copy recursivly.

# make proper
unalias cp ls 2>/dev/null
safecopy="cp -vf --preserve=mode,ownership"
#
create_new=false; [ "$1" = "--create-new" ] && create_new=true
copy_all=false; [ "$1" = "--copy-all" ] || $create_new && copy_all=true


# ==================  MAIN =========================

# Creating new top directory tree (CREATE-NEW):
if $create_new; then 

    echo "========== Creating directories: --------------------------"
    for dir in $topdir $userdir $userdirwwwoffle $wwwoffledir $scripts $library $import $locales; 
    do
       mkdir -v $dir -m $dir_perm
       chown -v $owner:$group $dir
    done

fi

# topdir
echo "========== Copy to top directory: --------------------------"
$safecopy $README $topdir/

 
# userdir (PERMISSIONS)
echo "========== Copy to userdir/.wwwoffle: --------------------------"
$safecopy $CONFIG $userdirwwwoffle/
# We already know $user from wwwofflebook.conf, so...
chown -v $user:$group $userdirwwwoffle/*  $userdirwwwoffle  
# provide an example symlink to purge.conf:
ln -sv $WWWOFFLE_PURGE_CONF $userdirwwwoffle


# wwwoffledir (COPY-ALL)
echo "========== Copy to wwwoffledir: --------------------------"
if $copy_all; then $safecopy $WWWOFFLE_PURGE_CONF $wwwoffledir/; fi
# permissions should be 770 proxy:proxy

# scripts (CREATE-NEW) (PERMISSIONS)
echo "========== Copy to script directory: --------------------------"
if $create_new; then 
    for FILE in `ls -1 $SCRIPTS/wwwofflebook*`; do
      $safecopy $FILE $scripts/
    done
fi
chmod -v 750 $scripts     # rwxr-x--- (only root can modify contents)


# library (CREATE-NEW)
echo "========== Copy to library: --------------------------"
if $create_new; then 
    # we use -r to copy anything including subdirectories:
    $safecopy -r $LIBRARY/*  $library/
fi


# tarball
echo "========== Create tarball $tarball: --------------------------"
tar -cvj  --directory=$topdir/.. --file $tarball $tardir_name
#
#
echo done.
exit 0

