#!/bin/bash
#
# daemon to backup app configuration file.
# Copyright (c) 2014-2015 by Cisco Systems, Inc.
# All rights reserved.`

if [ -f "/pkg/etc/app_repo.txt" ]; then
    APP_REPO_DIR=`cat /pkg/etc/app_repo.txt`
else
    APP_REPO_DIR="/misc/disk1/apprepo-dont-delete"
fi

RPMS_DIR=$APP_REPO_DIR/rpms
CONFIGS_DIR=$APP_REPO_DIR/configs
ADDED_CONFIGS=$APP_REPO_DIR/added_configs.txt
BACKUP_SLEEP_TIME=60   # backup every 60 secs

# backup the configuration files for all the rpms every 1 mins..
function main
{
    echo `date`, "Started app-config-backup daemon" 

    while true; do
        if [ -d "$APP_REPO_DIR" ]; then
            add_config_files
            backup_config_files
        fi
        sleep $BACKUP_SLEEP_TIME
    done
}



# add_config_files
# This function checks app-repo, if there are any new rpms present in the
# app-repo it add's those rpms' config files  to CONFIGS_DIR

function add_config_files
{
    rpms_added=( `cat $ADDED_CONFIGS 2>/dev/null` )

    for rpm_file in `find $APP_REPO_DIR -type f -name "*.rpm" 2>/dev/null`; do

        found=0
        for i in "${rpms_added[@]}"; do
            if [ "$i" == "$rpm_file" ]; then
                found=1
                break
            fi
        done

        if [ $found -eq 0 ] ; then
            add_config_files_for_rpm $rpm_file
            echo $rpm_file >> $ADDED_CONFIGS
        fi

    done
}


# add_config_files_for_rpm <rpm>: 
# This functions computes the config files for the rpm and adds them to CONFIGS_DIR.

# https://www.redhat.com/archives/rpm-list/2003-October/msg00140.html
# The bit that implements %config(noreplace) maps to (from lib/rpmlib.h):
# rpmlib.h: RPMFILE_NOREPLACE = (1 << 4), /*!< from %%config(noreplace) */
# So the CLI query to extract the file flags for, say, the xinetd package
#    $ rpm -q --qf '[%{filenames}: %{fileflags}\n]' xinetd
# Files which have the bit 0x10 set in the flags are the config files.

function add_config_files_for_rpm
{
    rpm_file=$1
    rpm=`basename $rpm_file`

    echo "Adding config files for" `basename $rpm_file`

    config_file_found=0

    for fileinfo in `rpm -qp --qf '[%{filenames}:%{fileflags}\n]' $rpm_file` ; do 
        filename=`echo $fileinfo | cut -f1 -d:`
        flags=`echo $fileinfo | cut -f2 -d:`

        if ! [ "$flags" == "0" ] ; then
            mkdir -p $CONFIGS_DIR/`dirname $filename`
            cp $filename $CONFIGS_DIR/$filename
            echo "$rpm: Added config-file $filename"
            config_file_found=1 
        fi
        
    done

    if [ $config_file_found -eq 0 ]; then
        echo "$rpm: No config files found"
    fi
}

# copy the files from rootfs to configs-dir
function backup_config_files
{
    ROOT_DIR=/

    for file in `cd $CONFIGS_DIR && find . -type f`; do
        ROOT_FS_FILE=$ROOT_DIR/$file
        CONFIG_FILE=$CONFIGS_DIR/$file

        if [ "$ROOT_FS_FILE" -nt "$CONFIG_FILE" ]; then
            cp $ROOT_FS_FILE $CONFIG_FILE
            echo `date` : "backup-config: cp $ROOT_FS_FILE $CONFIG_FILE" 
        fi
    done
}

# 
trap '' SIGHUP SIGINT SIGTERM SIGUSR 
main > /var/log/app_config_backup.log 2>&1


