#!/bin/bash
# Application Inventory Script
# Lists all applications, their locations, databases, and configurations

echo "=========================================="
echo "APPLICATION INVENTORY REPORT"
echo "Generated: $(date)"
echo "=========================================="
echo ""

# Colors for output
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color

echo "=== WEB APPLICATIONS ==="
echo ""
for dir in /var/www/html/*/; do
    if [ -d "$dir" ]; then
        app_name=$(basename "$dir")
        size=$(du -sh "$dir" 2>/dev/null | cut -f1)
        echo -e "${GREEN}Application:${NC} $app_name"
        echo "  Location: $dir"
        echo "  Size: $size"
        
        # Check for common config files
        if [ -f "$dir/configuration.php" ]; then
            echo "  Type: WHMCS (configuration.php found)"
        elif [ -f "$dir/wp-config.php" ]; then
            echo "  Type: WordPress (wp-config.php found)"
        elif [ -f "$dir/package.json" ]; then
            echo "  Type: Node.js (package.json found)"
        elif [ -f "$dir/composer.json" ]; then
            echo "  Type: PHP/Composer (composer.json found)"
        fi
        
        # Check last modification
        last_mod=$(stat -c %y "$dir" 2>/dev/null | cut -d' ' -f1)
        echo "  Last Modified: $last_mod"
        echo ""
    fi
done

echo ""
echo "=== APACHE VIRTUAL HOSTS ==="
echo ""
for conf in /etc/apache2/sites-available/*.conf; do
    if [ -f "$conf" ]; then
        vhost=$(basename "$conf" .conf)
        servername=$(grep -i "ServerName" "$conf" 2>/dev/null | head -1 | awk '{print $2}')
        documentroot=$(grep -i "DocumentRoot" "$conf" 2>/dev/null | head -1 | awk '{print $2}')
        
        # Check if enabled
        if [ -L "/etc/apache2/sites-enabled/$vhost.conf" ]; then
            status="${GREEN}ENABLED${NC}"
        else
            status="${YELLOW}DISABLED${NC}"
        fi
        
        echo -e "Virtual Host: $vhost ($status)"
        if [ ! -z "$servername" ]; then
            echo "  ServerName: $servername"
        fi
        if [ ! -z "$documentroot" ]; then
            echo "  DocumentRoot: $documentroot"
            if [ -d "$documentroot" ]; then
                echo "  Directory exists: Yes"
            else
                echo -e "  Directory exists: ${RED}No${NC}"
            fi
        fi
        echo ""
    fi
done

echo ""
echo "=== DATABASES ==="
echo ""
mysql -u admin -p'3mdbpB2k%*@!Dcfd' -e "
SELECT 
    table_schema AS 'Database',
    COUNT(*) AS 'Tables',
    ROUND(SUM(data_length + index_length) / 1024 / 1024, 2) AS 'Size (MB)'
FROM information_schema.tables
WHERE table_schema NOT IN ('information_schema', 'performance_schema', 'mysql', 'sys')
GROUP BY table_schema
ORDER BY SUM(data_length + index_length) DESC;
" 2>/dev/null

echo ""
echo "=== POTENTIALLY UNUSED DATABASES ==="
echo ""
mysql -u admin -p'3mdbpB2k%*@!Dcfd' -e "SHOW DATABASES;" 2>/dev/null | grep -E "old|backup|temp|test" | while read db; do
    size=$(mysql -u admin -p'3mdbpB2k%*@!Dcfd' -e "
    SELECT ROUND(SUM(data_length + index_length) / 1024 / 1024, 2) AS 'Size (MB)'
    FROM information_schema.tables
    WHERE table_schema = '$db';
    " 2>/dev/null | tail -1)
    echo -e "${YELLOW}$db${NC} - Size: ${size}MB"
done

echo ""
echo "=== DISK USAGE ==="
echo ""
df -h /var/www/html

echo ""
echo "=== LARGE DIRECTORIES ==="
echo ""
du -sh /var/www/html/* 2>/dev/null | sort -h | tail -10

echo ""
echo "=========================================="
echo "Inventory Complete"
echo "=========================================="

