#!/usr/bin/env bash
# Script to parse config of renewable files.
# and renew them with latest versions.
# If a file iz zipped (mask .z??) - unzip it
# Config must contain folder path, filename w/o ext, and destination folder

NMSBASE=/opt/fidonms
FRCONFIG=${NMSBASE}/etc/filerenew



#############################################################################
#                                                                           #
#############################################################################
BIN_UZIP=$(which unzip)
BIN_LS=$(which ls)

declare -a FR_PATH=();
declare -a FR_NAME=();
declare -a FR_DEST=();

if [ -f "$FRCONFIG" ]; then
        while read -r line;
        do
            if [[ ! ${line} =~ ^[#\;] ]];
            then
                FR_PATH+=($(echo -ne "${line}" | awk '{ print $1 }'))
                FR_NAME+=($(echo -ne "${line}" | awk '{ print $2 }'))
                FR_DEST+=($(echo -ne "${line}" | awk '{ print $3 }'))
                
            fi
        done < ${FRCONFIG}
        
        for fidx in ${!FR_PATH[@]}; do
            echo "PATH: ${FR_PATH[${fidx}]} NAME: ${FR_NAME[${fidx}]} DEST: ${FR_DEST[${fidx}]}"
            if [[ -d "${FR_DEST[${fidx}]}" ]]; then
                # We have destination dir
                FRW_DEST="${FR_DEST[${fidx}]}"
                if [[ -d "${FR_PATH[${fidx}]}" ]]; then
                    # We have source folder
                    FRW_SRCD="${FR_PATH[${fidx}]}"
                    # Discover what the source folder is
                    # By listing all files in folder with -iname as FR_NAME 
                    # Count 1 is that file is static (renewable in-place) and it is our source
                    # Count >1 is that folder is FECHO with extension numbered
                    FCNT=$( find ${FRW_SRCD} -maxdepth 1 -iname ${FR_NAME[${fidx}]}* | wc -l )
                    if [[ "${FCNT}" -eq "1" ]]; then
                        # Folder contains 1 file with that name
                        echo "1 file"
                        FRW_FILE=$( find ${FRW_SRCD} -maxdepth 1 -iname ${FR_NAME[${fidx}]}* -printf %f )
                        echo ${FRW_FILE}
                    else
                        # That's for fecho folder with same filenames:
                        FRW_FILE=$( ${BIN_LS} -AB1t "${FRW_SRCD}" | head -n2 | grep -i "${FR_NAME[${fidx}]}")
                        echo "multiply files - last is ${FRW_FILE}"
                    fi
                    
                    if [[ -f "${FRW_SRCD}/${FRW_FILE}" ]]; then
                        echo "Got ${FRW_FILE} in ${FRW_SRCD}"
                        FRW_EXT=$(echo -ne "${FRW_FILE}" | awk -F'.' '{ print $2 }' )
                        if [[ ${FRW_EXT} =~ [Zz]+ ]]; then
                            echo "Got zipped file"
                            mkdir -vp "${FRW_DEST}/tmp"
                            # Unpack it to tmp folder
                            ${BIN_UZIP} "${FRW_SRCD}/${FRW_FILE}" -d "${FRW_DEST}/tmp"
                        else
                            echo "Got not zipped file"
                            mkdir -vp "${FRW_DEST}/tmp"
                            cp -v "${FRW_SRCD}/${FRW_FILE}" "${FRW_DEST}/tmp"
                        fi
                        # Now we have to compare new and old file
                        # Delete old, place new or (if they are the same) drop it
                        NEW_FILE=$( find "${FRW_DEST}/tmp/" -maxdepth 1 -iname ${FR_NAME[${fidx}]}.??? )
                        echo "NEW: ${NEW_FILE}"
                        OLD_FILE=$( find "${FRW_DEST}" -maxdepth 1 -iname ${FR_NAME[${fidx}]}.??? )
                        echo "OLD: ${OLD_FILE}"
                        if [[ -f "${NEW_FILE}" ]]; then
                            if [[ -f "${OLD_FILE}" ]]; then
                                # We have old file - try to compare them
                                echo "Comparing with ${OLD_FILE}"
                                cmp -s "${NEW_FILE}" "${OLD_FILE}"
                                if [[ "$?" -eq 1 ]]; then
                                    # Files are different
                                    rm -vf "${OLD_FILE}"
                                    # Also remove index (if any)
                                    rm -vf "${OLD_FILE}.index"
                                    # Move new to dest
                                    mv -v "${NEW_FILE}" "${FRW_DEST}"
                                fi
                            else
                                # We have no old file - just move it over to dest
                                echo "No old file - moving new one to destination"
                                mv -v "${NEW_FILE}" "${FRW_DEST}"
                            fi
                        else
                            echo "Something went wrong. New file is missing"
                        fi
                        # Delete tmp dir
                        rm -vrf "${FRW_DEST}/tmp"
                    else
                        echo "Could not pick up a file"
                    fi
                else
                    echo "Could not find source directory"
                fi
            else
                echo "Could not find dest directory"
            fi
        done
else
        echo "No config file, please make one: <SRC_FOLDER> <SRC_FILE_NAME> <DEST_FOLDER>"
fi

