#!/bin/bash -x

function is_rhel {
    [ -f /etc/redhat-release ]
}

function is_bwan {
    [ -d /infroot ]
}

function cleanup_bash_history {
    # Check if any history files exist before trying to shred them
    local history_files=(~/.bash_history ~/.zsh_history ~/.history)
    local files_found=false
    
    for file in "${history_files[@]}"; do
        if [ -f "$file" ]; then
            shred -u "$file"
            files_found=true
            echo "Shredded: $file"
        fi
    done
    
    # Also try to clean any other .*history files that might exist
    for file in ~/.*history; do
        if [ -f "$file" ]; then
            shred -u "$file"
            files_found=true
            echo "Shredded: $file"
        fi
    done
    
    if [ "$files_found" = false ]; then
        echo "No history files found to shred"
    fi
    
    history -cw
}

function packages_cleanup {
    if is_rhel ; then
        yum clean all -y
    else 
        apt autoremove -y
        apt-get clean -y
    fi
}

function remove_downloaded_files {
    rm -f provision_shared.sh
    rm -f cleanup.sh
}

if is_bwan; then
    remove_downloaded_files
else
    cleanup_bash_history
    packages_cleanup
    remove_downloaded_files
fi