#!/bin/sh
#----------------------------------------------------------------------------------------
# /var/install/bin/backup-file - create backup file
#
# Copyright (c) 2003-2004 Frank Meyer <frank(at)eisfair(dot)org>
#
# Creation:     25.12.2003  jed
# Last Update:  $Id$
#
# Parameters:
#
#     backup   
#       or
#     backup [-quiet] file-to-backup [backup-suffix]
#
# 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.
#----------------------------------------------------------------------------------------

# include eislib
. /var/install/include/eislib

# include function library
. /var/install/bin/config_shlib

#debug='true'
#trace='true'

pgmname=`basename $0`

#
# move_old_backup_files
# =====================
#
# Parameters:      $1    source file
#                  $2    'quiet' - disable screen output
#
# Returns:         0     move Ok.
#                  1     failure
#
move_old_backup_files ()
{
    [ "$debug" = 'true' ] && echo "call move_old_backup_files: $*"
    [ "$trace" = 'true' ] && set -x
    source_file=$1
    quiet=$2

    # check if old backup files exist
    ls "$source_file".* > /dev/null 2>/dev/null

    if [ $? -eq 0 ]
    then
        if [ "$quiet" != "quiet" ]
        then
            # move old backup files to new location
            mecho "move old backup file(s) to new directory ..."
        fi

        for inline in $source_file.*
        do
            # Thu Dec 25 14:45:39 2003 -> 2003.12.25.14.45.39
            datestr=`ls --full-time $inline | tr -s ' ' ' ' | cut -d' ' -f6-10`
            datestr=`date -d "$datestr" +%Y-%m-%d-%H-%M-%S`

            # keep original filename and append suffix
            if [ "$quiet" != "quiet" ]
            then
                mv -v "$inline" "$backupd/`basename $inline`.$datestr"
            else
                # quiet
                mv "$inline" "$backupd/`basename $inline`.$datestr"
            fi
        done
    fi
}

#----------------------------------------------------------------------------------------
# main
#----------------------------------------------------------------------------------------

if [ $# -gt 0 ]
then
    if [ "$1" = "-quiet" ]
    then
        quiet_flag='quiet'
        shift
    fi

    # get file to backup
    source_file="$1"
    source_path="`dirname $source_file`"
    source_name="`basename $source_file`"

    if [ $# -gt 1 ]
    then
        file_suffix=$2
    else
        file_suffix=''
    fi

    if [ "$source_path" = "." -o "$source_path" = "$configd" ]
    then
        # default directory operation
        move_old_backup_files $configd/$source_name $quiet_flag

        backup_config $source_name $configd/$source_name "$file_suffix" $quiet_flag
    else
        # individual directory operation
        backupd=$source_path

        backup_config $source_name "$source_file" "$file_suffix" $quiet_flag
    fi
else
    # no file name given - show help
    mecho "  Usage: $pgmname [-quiet] file-to-backup [backup-suffix]"
    mecho
fi