#!/bin/bash

get_debian_packages() {
    local MOUNT="$1"
    if [ -z "$MOUNT" ]; then
        echo "Usage: get_debian_packages <mount_point>"
        return 1
    fi

    if [ ! -f "$MOUNT/etc/debian_version" ]; then
        echo "[WARN] Non-Debian OS detected, exiting."
        return 1
    fi

    local STATUS_FILE="$MOUNT/var/lib/dpkg/status"
    local PKG_STATUS=()
    if [ -f "$STATUS_FILE" ]; then
        while read -r line; do
            if [[ "$line" =~ ^Package:\ (.+) ]]; then
                PKG_STATUS+=("${BASH_REMATCH[1]}")
            fi
        done < "$STATUS_FILE"

        if [ "${#PKG_STATUS[@]}" -gt 0 ]; then
            printf '['
            for i in "${!PKG_STATUS[@]}"; do
                printf '"%s"' "${PKG_STATUS[$i]}"
                if [ "$i" -lt $((${#PKG_STATUS[@]}-1)) ]; then
                    printf ', '
                fi
            done
            printf ']\n'
        fi
    fi

    printf "\n"
    local EXT_FILE="$MOUNT/var/lib/apt/extended_states"
    local PKG_EXT=()
    if [ -f "$EXT_FILE" ]; then
        while read -r line; do
            if [[ "$line" =~ ^Package:\ (.+) ]]; then
                PKG_EXT+=("${BASH_REMATCH[1]}")
            fi
        done < "$EXT_FILE"

        if [ "${#PKG_EXT[@]}" -gt 0 ]; then
            printf '['
            for i in "${!PKG_EXT[@]}"; do
                printf '"%s"' "${PKG_EXT[$i]}"
                if [ "$i" -lt $((${#PKG_EXT[@]}-1)) ]; then
                    printf ', '
                fi
            done
            printf ']\n'
        fi
    fi
}


list_dirs_and_files() {
    local MOUNT="$1"

    local DIR_LIST=(
        "/opt"
        "/"
        "/root"
        "/home/administrator"
        "/home"
        "/etc"
        "/var/lib"
        "/usr/local/share"
    )

    echo "===== 目录和文件列表 ====="
    for d in "${DIR_LIST[@]}"; do
        local path="$MOUNT$d"
        echo "$d:"

        if [ ! -d "$path" ]; then
            echo
            continue
        fi

        local dirs=()
        local files=()
        for entry in "$path"/* "$path"/.*; do
            [ ! -e "$entry" ] && continue
            name="${entry##*/}"
            [ "$name" == "." ] || [ "$name" == ".." ] && continue
            if [ -d "$entry" ]; then
                dirs+=("$name")
            else
                files+=("$name")
            fi
        done

        echo "  dir: ${dirs[*]}"
        echo "  dir: ${files[*]}"
        echo
    done
}

show_bash_history() {
    local MOUNT="$1"
    local users=("root" "administrator")

    for u in "${users[@]}"; do
        local hist_file="$MOUNT/home/$u/.bash_history"
        if [ "$u" == "root" ]; then
            hist_file="$MOUNT/root/.bash_history"
        fi

        echo "===== $u 的 Bash 历史记录 ====="
        if [ -f "$hist_file" ]; then
            cat "$hist_file"
        else
            echo "not find"
        fi
        echo
    done
}

MOUNT_POINT="$1"
if [ -z "$MOUNT_POINT" ]; then
    echo "Usage: $0 <mount_point>"
    exit 1
fi

get_debian_packages "$MOUNT_POINT"
list_dirs_and_files "$MOUNT_POINT"
show_bash_history "$MOUNT_POINT"

