#!/bin/bash

#AUTODESK INVENTORY TOOL FOR MAC AND LINUX v1.2.1
#16-July-2025
#This script is created by Autodesk License Compliance Techical Specialist team.
#All content is provided "as is" without any warranties, express, or implied.
#The creators assume no responsibility for any outcomes resulting from the use of this script.


script_dir=$(dirname "$0")
log_file="$script_dir/AITlog.log"

error_file="$script_dir/AITError.log"
exec 2> >(while read -r line; do echo "$(date '+%Y-%m-%dT%H:%M:%S') ERROR: $line" >> "$error_file"; done)
trap 'echo "$(date '+%Y-%m-%dT%H:%M:%S') ERROR at line $LINENO: command exited with code $?." >> "$error_log"' ERR
set -o errtrace

echo "*********************************************" | tee -a "$log_file"
echo "Autodesk Inventory Tool for Mac & Linux v1.2.1" | tee -a "$log_file"
echo "$(date +"%Y-%m-%dT%H:%M:%S")" >> "$log_file"
echo "*********************************************" | tee -a "$log_file"

echo -e "\nCommencing scan...\n"


#SYSTEM INFORMATION

echo -ne "Capturing System Information..."
echo -e "\n*********************************************" >> "$log_file"
echo "DATA 01 - SYSTEM INFORMATION" >> "$log_file"
echo -e "*********************************************\n" >> "$log_file"

# Capture the PC's hostname
pc_name=$(hostname | sed 's/\([^.]*\)\.[^.]*$/\1/')
echo "PC_Name: $pc_name" >> "$log_file"

# Capture the operating system name
os=$(uname -s)
if [ "$os" = "Darwin" ]; then
    os_name="Mac"
else
    os_name="$os"
fi
echo "OS: $os_name" >> "$log_file"

# Capture the operating system version
if command -v lsb_release &>/dev/null; then
    os_version=$(lsb_release -d | cut -f2-)
elif [ "$os_name" = "Mac" ]; then
    os_version=$(sw_vers -productVersion)
else
    if [ -f /etc/os-release ]; then
        os_version=$(grep "^PRETTY_NAME=" /etc/os-release | cut -d= -f2- | tr -d '"')
    fi
fi
echo "OS Version: $os_version" >> "$log_file"

# Capture the domain name
if [ "$os_name" == "Mac" ]; then
    domain=$(hostname | sed 's/^[^.]*\.//')
    if [ -z "$domain" ] || [ "$domain" = "local" ]; then
        domain="WORKGROUP"
    fi
else
    domain=$(dnsdomainname)
    if [ -z "$domain" ]; then
        domain=$(cat /etc/hostname | sed 's/^[^.]*\.//')
        if [[ "$domain" == "$pc_name" || "$domain" == "" ]]; then
            domain="WORKGROUP"
        fi
    fi
fi
echo "Domain: $domain" >> "$log_file"

# Capture the current logged-in user
logged_in_user=$(whoami)
echo "Current User: $logged_in_user" >> "$log_file"

# Detect if the system is running in a virtual machine or on physical hardware
if [[ "$os_name" == "Linux" ]]; then
    if command -v systemd-detect-virt &>/dev/null; then
        virt_type=$(systemd-detect-virt)
        if [[ "$virt_type" == "none" ]]; then
            pc_type="Physical"
            vm_type=""
            is_virt="false"
        else
            pc_type="Virtual"
            vm_type="$virt_type"
            is_virt="true"
        fi
    else
        pc_type="Physical"
        vm_type=""
        is_virt="false"
        if dmesg | grep -i -E "VirtualBox|VMware|KVM|QEMU|Xen" &>/dev/null; then
            pc_type="Virtual"
            vm_type=$(dmesg | grep -i -E "VirtualBox|VMware|KVM|QEMU|Xen" | head -n 1 | awk -F" " '{print $3}')
            is_virt="true"
        fi
    fi
elif [[ "$os_name" == "Mac" ]]; then
    pc_type="Physical"
    vm_type=""
    is_virt="false"
fi
echo "PC Type: $pc_type" >> "$log_file"
echo "VM Type: $vm_type" >> "$log_file"
echo "Is Virtual: $is_virt" >> "$log_file"

# Capture the current date and time in YYYYMMDDThh:mm:ss format
query_date=$(date +"%Y-%m-%dT%H:%M:%S")
echo "Query Date/Time: $query_date" >> "$log_file"

# Capture the timezone in (UTC+00:00) Region, City format
if [ "$os_name" == "Mac" ]; then
    timezone="(UTC$(date +'%z' | sed 's/\([+-]\)\([0-9]\{2\}\)\([0-9]\{2\}\)/\1\2:\3/')) $(sudo systemsetup -gettimezone | awk '{print $3}' | sed 's|/|, |g')"
else
    timezone="(UTC$(date +'%z' | sed 's/\([+-]\)\([0-9]\{2\}\)\([0-9]\{2\}\)/\1\2:\3/')) $(timedatectl | grep 'Time zone' | awk '{print $3}' | sed 's|/|, |g')"
fi
echo "Time Zone: $timezone" >> "$log_file"

echo -e "DONE"

# Get the OS installation date (timestamp of the root filesystem)
if [ "$os_name" == "Mac" ]; then

	if [ -f /var/db/.AppleSetupDone ]; then
		os_install_epoch=$(stat -f %B /var/db/.AppleSetupDone)
		if [ "$os_install_epoch" -eq 0 ]; then
			windows_installation_date=$(stat -f "%Sm" -t "%Y-%m-%d %H:%M:%S" /var/db/.AppleSetupDone)
		else
			windows_installation_date=$(date -r "$os_install_epoch" "+%Y-%m-%dT%H:%M:%S")
		fi
	else
		os_install_epoch=$(stat -f %B /)
		if [ "$os_install_epoch" -eq 0 ]; then
			windows_installation_date=$(stat -f "%Sm" -t "%Y-%m-%d %H:%M:%S" /)
		else
			windows_installation_date=$(date -r "$os_install_epoch" "+%Y-%m-%dT%H:%M:%S")
		fi
	fi

else

    os_install_date=$(stat -c %W /)
    if [ "$os_install_date" -eq 0 ]; then
        os_install_date=$(stat -c %z /)
    fi
    if [[ "$os_install_date" =~ ^[0-9]+$ ]]; then
        windows_installation_date=$(date -d @$os_install_date +"%Y-%m-%dT%H:%M:%S")
    else
        windows_installation_date=$(date -d "$os_install_date" +"%Y-%m-%dT%H:%M:%S")
    fi
fi
if [ -z "$windows_installation_date" ] || [ "$windows_installation_date" = "invalid_date_value" ]; then
    windows_installation_date=$query_date
fi
echo -e "Windows_Installation_Date: $windows_installation_date" >> "$log_file"



# XML SYSTEM INFORMATION

cat <<EOF > "$script_dir/$pc_name.xml"
<?xml version="1.0" encoding="utf-8"?>
<DataStore xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Created>$query_date</Created>
  <Updated>$query_date</Updated>
  <Log />
  <Computers>
    <Computer>
      <DiscoveryName>$pc_name</DiscoveryName>
      <DiscoverySource>Unknown</DiscoverySource>
      <NameFromWMI>$pc_name</NameFromWMI>
      <ConnectedIpAddress>1.1.1.1</ConnectedIpAddress>
      <ScanStart>$query_date</ScanStart>
      <ScanFinish>$query_date</ScanFinish>
      <LastSuccessfulScan>$query_date</LastSuccessfulScan>
      <ErrorOccurred>false</ErrorOccurred>
      <ScannerVersion>1.2.1.0</ScannerVersion>
      <Hardware>
        <Make>$os_name</Make>
        <Model>$os_name</Model>
        <Class>Desktop</Class>
        <IsVirtual>$is_virt</IsVirtual>
        <SerialNumber>Default string</SerialNumber>
        <SystemSku>Default string</SystemSku>
        <OperatingSystem>$os_name</OperatingSystem>
        <OsVersion>$os_version</OsVersion>
        <ServicePack>RTM</ServicePack>
        <Domain>$domain</Domain>
        <IpAddress>1.1.1.1</IpAddress>
        <CpuName>$os_name</CpuName>
        <CpuCount>1</CpuCount>
        <CpuCores>1</CpuCores>
        <CoreThreads>1</CoreThreads>
        <CpuLogical>1</CpuLogical>
        <CpuSpeed>1</CpuSpeed>
        <RamInBytes>1</RamInBytes>
        <User>$logged_in_user</User>
        <LastSeenDate>$query_date</LastSeenDate>
        <BiosDate>$query_date</BiosDate>
        <MultiThreading>true</MultiThreading>
        <VirtualisationType>$vm_type</VirtualisationType>
        <WindowsInstallDate>$windows_installation_date</WindowsInstallDate>
        <OriginalWindowsInstallDate>$windows_installation_date</OriginalWindowsInstallDate>
        <HyperVEnabled>false</HyperVEnabled>
        <NetworkAdapters />
        <Disks />
        <Lcid>1</Lcid>
        <LcidCode>en-GB</LcidCode>
        <LcidName>English (United Kingdom)</LcidName>
        <TimeZone>$timezone</TimeZone>
        <MsSCL xsi:nil="true" />
      </Hardware>
      <WindowsInstalls>
        <WindowsInstallItem>
          <Name>CurrentVersion</Name>
          <Product>$os_name</Product>
          <Edition>$os_version</Edition>
          <Build>1</Build>
          <InstallDate>$windows_installation_date</InstallDate>
        </WindowsInstallItem>
      </WindowsInstalls>
      <ActiveDirectory>
        <QueryDate xsi:nil="true" />
        <IsEnabled xsi:nil="true" />
        <LastLogonDate xsi:nil="true" />
        <CreatedDate xsi:nil="true" />
      </ActiveDirectory>
      <SqlInstances />
      <SoftwareInstalls>
EOF


#SOFTWARE LIST

echo -ne "Capturing Installed Software Information..."
echo -e "\n*********************************************" >> "$log_file"
echo "DATA 02 - INSTALLED SOFTWARE" >> "$log_file"
echo -e "*********************************************\n" >> "$log_file"

if [ "$os_name" == "Mac" ]; then
    installed_software=$(system_profiler SPApplicationsDataType | sed 's/^[[:space:]]*//' | grep -v '^$')
    autodesk_software=$(echo "$installed_software" | grep -v -E '^(Obtained from:|Last Modified:|Kind:|64-Bit \(Intel\):)' | 
      grep -B 2 -A 1 "Autodesk (XXKJ396S2Y)" | 
      grep -v '^--$' | 
      grep -v '^Signed by:' | 
      awk '/^Version/ { $0 = substr($0, 1, 20) } 1' | 
      awk 'NR%3 == 1 { sub(/:$/, ""); $0 = "Product: " $0 } 1')
    if [ -z "$autodesk_software" ]; then
        echo "No software installed." >> "$log_file"
    else
        while IFS= read -r line; do
            if [[ $line == Product:* ]]; then
                product="${line#Product: }"

cat <<EOF >> "$script_dir/$pc_name.xml"
        <SoftwareInstall>
          <Publisher>Autodesk</Publisher>
          <Product>$product</Product>
EOF

            elif [[ $line == Version:* ]]; then
                version="${line#Version: }"

cat <<EOF >> "$script_dir/$pc_name.xml"
          <Version>$version</Version>
EOF

            elif [[ $line == Location:* ]]; then
                location_value="${line#Location: }"
                install_date=$(stat -t "%Y-%m-%dT%H:%M:%S" -f "%Sm" "$location_value")
                soft_loc="${location_value%/*.app}/"

cat <<EOF >> "$script_dir/$pc_name.xml"
          <InstallDate>$install_date</InstallDate>
          <InstallPath>$soft_loc</InstallPath>
          <ScanDate>$query_date</ScanDate>
        </SoftwareInstall>
EOF

            fi
            echo "$line"
        done <<< "$autodesk_software" >> "$log_file"
    fi
else

    # Function for Debian-based systems (APT)
    get_apt_software() {
        found_autodesk_software=false
        while read -r product version; do
            if echo "$product" | grep -iqE 'autodesk|adsk|adlm|flame|flare|maya|arnold|mudbox|moldflow|motionbuilder'; then
                found_autodesk_software=true
                echo -e "Product: $product\nVersion: $version" >> "$log_file"
                install_date=$(stat --format='%y' "/var/lib/dpkg/info/${product}.list" 2>/dev/null)
                if [[ -n "$install_date" ]]; then
                    install_date=$(date -d "$install_date" +"%Y-%m-%dT%H:%M:%S")
                    echo "Install: $install_date" >> "$log_file"
                else
                    install_date="$query_date"
                    echo "Install: N/A" >> "$log_file"
                fi
                bin_folder=$(dpkg-query -L "$product" 2>/dev/null | grep -i "/bin/" | head -n 1)
                if [[ -n "$bin_folder" ]]; then
                    soft_loc=$(echo "$bin_folder" | sed 's|\(.*\)/bin/\(.*\)|\1/bin|')
                    echo "Location: $soft_loc" >> "$log_file"
                else
                    prefix=${product:0:4}
                    fallback_path=$(dpkg-query -L "$product" 2>/dev/null | grep -i "$prefix" | head -n 1)
                    if [[ -n "$fallback_path" ]]; then
                        soft_loc=$(dirname "$fallback_path")
                        echo "Location: $soft_loc" >> "$log_file"
                    else
                        soft_loc="unknown"
                        echo "Location: N/A" >> "$log_file"
                    fi
                fi

cat <<EOF >> "$script_dir/$pc_name.xml"
        <SoftwareInstall>
          <Publisher>Autodesk</Publisher>
          <Product>$product</Product>
          <Version>$version</Version>
          <InstallDate>$install_date</InstallDate>
          <InstallPath>$soft_loc</InstallPath>
          <ScanDate>$query_date</ScanDate>
        </SoftwareInstall>
EOF

            fi
        done < <(dpkg-query -W -f='${Package}\t${Version}\n')
    if ! $found_autodesk_software; then
        echo "No software installed." >> "$log_file"
    fi
    }

# Function for Red Hat-based systems (YUM/DNF)
get_yum_dnf_software() {
    found_autodesk_software=false
    while read -r package; do
        if echo "$package" | grep -iqE 'autodesk|adsk|adlm|flame|flare|maya|arnold|mudbox|moldflow|motionbuilder'; then
            found_autodesk_software=true
            product=$(echo "$package" | cut -d'-' -f1)
            version=$(echo "$package" | cut -d'-' -f2)
            echo -e "Product: $product\nVersion: $version" >> "$log_file"
            install_date=$(rpm -qi "$product" | grep "Install Date" | awk -F: '{print $2}' | xargs)
            if [[ -n "$install_date" ]]; then
                install_date=$(date -d "$install_date" +"%Y-%m-%dT%H:%M:%S")
                echo "Install: $install_date" >> "$log_file"
            else
                install_date="$query_date"
                echo "Install: N/A" >> "$log_file"
            fi
            bin_folder=$(rpm -ql "$product" 2>/dev/null | grep -i "/bin/" | head -n 1)
            if [[ -n "$bin_folder" ]]; then
                soft_loc=$(echo "$bin_folder" | sed 's|\(.*\)/bin/\(.*\)|\1/bin|')
                echo "Location: $soft_loc" >> "$log_file"
            else
                prefix=${product:0:4}
                fallback_path=$(rpm -ql "$product" 2>/dev/null | grep -i "$prefix" | head -n 1)
                if [[ -n "$fallback_path" ]]; then
                    soft_loc=$(dirname "$fallback_path")
                    echo "Location: $soft_loc" >> "$log_file"
                else
                    soft_loc="unknown"
                    echo "Location: N/A" >> "$log_file"
                fi
            fi

cat <<EOF >> "$script_dir/$pc_name.xml"
        <SoftwareInstall>
          <Publisher>Autodesk</Publisher>
          <Product>$product</Product>
          <Version>$version</Version>
          <InstallDate>$install_date</InstallDate>
          <InstallPath>$soft_loc</InstallPath>
          <ScanDate>$query_date</ScanDate>
        </SoftwareInstall>
EOF

        fi
    done < <(rpm -qa --qf "%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}\n")
    if ! $found_autodesk_software; then
        echo "No software installed." >> "$log_file"
    fi
}

# Function for Arch-based systems (Pacman)
get_pacman_software() {
    found_autodesk_software=false
    while read -r product version; do
        if echo "$product" | grep -iqE 'autodesk|adsk|adlm|flame|flare|maya|arnold|mudbox|moldflow|motionbuilder'; then
            found_autodesk_software=true
            echo -e "Product: $product\nVersion: $version" >> "$log_file"
            install_date=$(pacman -Qi "$product" | grep "Install Date" | awk -F: '{print $2}' | xargs)
            if [[ -n "$install_date" ]]; then
                install_date=$(date -d "$install_date" +"%Y-%m-%dT%H:%M:%S")
                echo "Install: $install_date" >> "$log_file"
            else
                install_date="$query_date"
                echo "Install: N/A" >> "$log_file"
            fi
            bin_folder=$(pacman -Ql "$product" 2>/dev/null | grep -i "/bin/" | head -n 1)
            if [[ -n "$bin_folder" ]]; then
                soft_loc=$(echo "$bin_folder" | sed 's|\(.*\)/bin/\(.*\)|\1/bin|')
                echo "Location: $soft_loc" >> "$log_file"
            else
                prefix=${product:0:4}
                fallback_path=$(pacman -Ql "$product" 2>/dev/null | grep -i "$prefix" | head -n 1)
                if [[ -n "$fallback_path" ]]; then
                    soft_loc=$(dirname "$fallback_path")
                    echo "Location: $soft_loc" >> "$log_file"
                else
                    soft_loc="unknown"
                    echo "Location: N/A" >> "$log_file"
                fi
            fi

cat <<EOF >> "$script_dir/$pc_name.xml"
        <SoftwareInstall>
          <Publisher>Autodesk</Publisher>
          <Product>$product</Product>
          <Version>$version</Version>
          <InstallDate>$install_date</InstallDate>
          <InstallPath>$soft_loc</InstallPath>
          <ScanDate>$query_date</ScanDate>
        </SoftwareInstall>
EOF

        fi
    done < <(pacman -Q)
    if ! $found_autodesk_software; then
        echo "No software installed." >> "$log_file"
    fi
}

# Function for openSUSE systems (Zypper)
get_zypper_software() {
    found_autodesk_software=false
    while read -r product version; do
        if echo "$product" | grep -iqE 'autodesk|adsk|adlm|flame|flare|maya|arnold|mudbox|moldflow|motionbuilder'; then
            found_autodesk_software=true
            echo -e "Product: $product\nVersion: $version" >> "$log_file"
            install_date=$(rpm -qi "$product" | grep "Install Date" | awk -F: '{print $2}' | xargs)
            if [[ -n "$install_date" ]]; then
                install_date=$(date -d "$install_date" +"%Y-%m-%dT%H:%M:%S")
                echo "Install: $install_date" >> "$log_file"
            else
                install_date="$query_date"
                echo "Install: N/A" >> "$log_file"
            fi
            bin_folder=$(rpm -ql "$product" 2>/dev/null | grep -i "/bin/" | head -n 1)
            if [[ -n "$bin_folder" ]]; then
                soft_loc=$(echo "$bin_folder" | sed 's|\(.*\)/bin/\(.*\)|\1/bin|')
                echo "Location: $soft_loc" >> "$log_file"
            else
                prefix=${product:0:4}
                fallback_path=$(rpm -ql "$product" 2>/dev/null | grep -i "$prefix" | head -n 1)
                if [[ -n "$fallback_path" ]]; then
                    soft_loc=$(dirname "$fallback_path")
                    echo "Location: $soft_loc" >> "$log_file"
                else
                    soft_loc="unknown"
                    echo "Location: N/A" >> "$log_file"
                fi
            fi

cat <<EOF >> "$script_dir/$pc_name.xml"
        <SoftwareInstall>
          <Publisher>Autodesk</Publisher>
          <Product>$product</Product>
          <Version>$version</Version>
          <InstallDate>$install_date</InstallDate>
          <InstallPath>$soft_loc</InstallPath>
          <ScanDate>$query_date</ScanDate>
        </SoftwareInstall>
EOF

        fi
    done < <(zypper search --installed-only | awk 'NR>2 {print $3, $4}')
    if ! $found_autodesk_software; then
        echo "No software installed." >> "$log_file"
    fi
}

    # Main section to detect the distribution and call the respective function
    if [ -f /etc/debian_version ]; then
        echo -e "Debian-based system detected\n" >> "$log_file"
        get_apt_software
    elif [ -f /etc/redhat-release ]; then
        echo -e "Red Hat-based system detected\n" >> "$log_file"
        if command -v dnf &> /dev/null; then
            get_yum_dnf_software
        else
            get_yum_dnf_software
        fi
    elif [ -f /etc/arch-release ]; then
        echo -e "Arch-based system detected\n" >> "$log_file"
        get_pacman_software
    elif [ -f /etc/os-release ] && grep -q "SUSE" /etc/os-release; then
        echo -e "openSUSE system detected\n"
        get_zypper_software
    else
        echo -e "Unsupported Linux distribution\n" >> "$log_file"
    fi
fi
echo -e "DONE"


#LGS FILE

echo -ne "Capturing Software Information (Part I)..."
echo -e "\n*********************************************" >> "$log_file"
echo "DATA 03 - LGS INFORMATION" >> "$log_file"
echo -e "*********************************************\n" >> "$log_file"

linux_lgs_dir="/var/opt/Autodesk//CLM/LGS"
mac_lgs_dir="/Library/Application Support/Autodesk/CLM/LGS"
temp_lgs_file=$(mktemp)

if [ "$os_name" == "Mac" ]; then
    lgs_dir="$mac_lgs_dir"
else
    lgs_dir="$linux_lgs_dir"
fi

cat <<EOF >> "$script_dir/$pc_name.xml"
      </SoftwareInstalls>
      <ExchangeInstance>
        <ScanDate xsi:nil="true" />
      </ExchangeInstance>
      <Files>
EOF

if [ -d "$lgs_dir" ]; then
    for lgs_folder in "$lgs_dir/"*; do
        if [ -d "$lgs_folder" ]; then
            lgs_product=$(basename "$lgs_folder" | cut -d '_' -f 1)
            lgs_version=$(basename "$lgs_folder" | cut -d '_' -f 2-)
            lgs_data_file="$lgs_folder/LGS.data"
            if [ -f "$lgs_data_file" ]; then
                lgs_install=$(head -n 1 "$lgs_data_file" | sed 's/^_//')
            else
                lgs_install=""
            fi
            replace_value() {
                case "$1" in
                    777*) echo "AutoCAD for Mac" ;;
                    827*) echo "AutoCAD LT for Mac" ;;
                    657*) echo "Autodesk Maya" ;;
                    C0W*) echo "Autodesk Flare" ;;
                    C0T*) echo "Autodesk Flame" ;;
                    498*) echo "Autodesk Mudbox" ;;
                    C0P*) echo "Arnold" ;;
                    C7X*) echo "Moldflow Insight" ;;
                    727*) echo "MotionBuilder" ;;
                    871*|732*) echo "Autodesk SketchBook Pro" ;;
                *) echo "$1" ;;
                esac
            }
            lgs_product_name=$(replace_value "$lgs_product")
            echo -e "Product: $lgs_product_name\nVersion: $lgs_version\nInstall: $lgs_install\nCode: $lgs_product\nPath: $lgs_folder" >> "$log_file"

cat <<EOF >> "$script_dir/$pc_name.xml"
        <FileInformation>
          <Publisher>Autodesk</Publisher>
          <Description>LGS Licence Files</Description>
          <FileName>LGS.data</FileName>
          <FilePath>$lgs_folder</FilePath>
          <DateFound>$query_date</DateFound>
          <FileContents xsi:type="xsd:string">$lgs_install</FileContents>
          <DecodedContents>
            <FileDecodedContents>
              <Product>$lgs_product_name</Product>
              <Version>${lgs_version:0:4}</Version>
              <Type>$lgs_install</Type>
              <DecodedContentType>AutodeskLGS</DecodedContentType>
            </FileDecodedContents>
          </DecodedContents>
        </FileInformation>
EOF

cat <<EOF >> "$temp_lgs_file"
            <SerialNumber>
                <Publisher>Autodesk</Publisher>
                <Description>LGS Licence Files</Description>
                <Product>$lgs_product_name</Product>
                <Version>${lgs_version:0:4}</Version>              
                <Type>$lgs_install</Type>
                <ProductCode>$lgs_product</ProductCode>
            </SerialNumber>
EOF

        fi
    done
else
    echo "LGS folder does not exist." >> "$log_file"
fi
echo -e "DONE"


#PIT FILE

echo -ne "Capturing Software Information (Part II)..."
echo -e "\n*********************************************" >> "$log_file"
echo "DATA 04 - PIT INFORMATION" >> "$log_file"
echo -e "*********************************************\n" >> "$log_file"

linux_pit_file="/var/opt/Autodesk/Adlm/.config/ProductInformation.pit"
mac_pit_file="/Library/Application Support/Autodesk/ADLM/.config/ProductInformation.pit"

if [ "$os_name" == "Mac" ]; then
    pit_file="$mac_pit_file"
else
    pit_file="$linux_pit_file"
fi
if [ -f "$pit_file" ]; then
    pit_content_base64=$(base64 -i "$pit_file" 2>> "$log_file")
    echo -e "PIT File:\n$pit_content_base64" >> "$log_file"

cat <<EOF >> "$script_dir/$pc_name.xml"
        <FileInformation>
          <Publisher>Autodesk</Publisher>
          <Description>PIT Licence Files</Description>
          <FileName>ProductInformation.pit</FileName>
          <FilePath>$pit_file</FilePath>
          <DateFound>$query_date</DateFound>
          <FileContents xsi:type="xsd:base64Binary">$pit_content_base64</FileContents>
          <DecodedContents/>
        </FileInformation>
EOF

else
    echo -e "ProductInformation.pit does not exist." >> "$log_file"
fi
echo -e "DONE"


#LICENSING HELPER

echo -ne "Capturing License Information..."
echo -e "\n*********************************************" >> "$log_file"
echo "DATA 05 - LICENSE INFORMATION" >> "$log_file"
echo -e "*********************************************\n" >> "$log_file"

linux_lic_dir="/opt/Autodesk/AdskLicensing/Current/helper"
mac_lic_dir="/Library/Application Support/Autodesk/AdskLicensing/Current/helper"
temp_xml_file=$(mktemp)

if [ "$os_name" == "Mac" ]; then
    license_dir="$mac_lic_dir"
else
    license_dir="$linux_lic_dir"
fi
if [ -d "$license_dir" ]; then
    licensing_details=$("$license_dir/AdskLicensingInstHelper" list)

cat <<EOF >> "$script_dir/$pc_name.xml"
        <FileInformation>
          <Publisher>Autodesk</Publisher>
          <Description>Installer Helper</Description>
          <FileName>AdskLicenseHelper.json</FileName>
          <FilePath>$license_dir</FilePath>
          <DateFound>$query_date</DateFound>
          <FileContents xsi:type="xsd:string"></FileContents>
          <DecodedContents>
EOF

else
    echo "Licensing Helper does not exist." >> "$log_file"
fi
if [ -n "$licensing_details" ]; then
    formatted_details=$(echo "$licensing_details" | sed 's/^[ \t]*//' | awk '/"lic_servers": \[/{ print; getline; print "\"net_server\":", $0; next } 1')
    formatted_details=$(echo "$formatted_details" | grep '"feature_id"\|"def_prod_key"\|"def_prod_ver"\|"sel_prod_key"\|"lic_method"\|"net_server"\|"serial_number_sa"\|"serial_number_nw"' | sed 's/"//g; s/,$//')
    replace_value() {
        case "$1" in
            "ACDMAC") echo "AutoCAD for Mac" ;;
            "ACDLTM") echo "AutoCAD LT for Mac" ;;
            "MAYA"|"MAYACRE") echo "Autodesk Maya" ;;
            "A400") echo "Autodesk Flare" ;;
            "A250") echo "Autodesk Flame" ;;
            "MBXPRO"|"MOBPRO") echo "Autodesk Mudbox" ;;
            "ARNOL") echo "Arnold" ;;
            "MFI") echo "Moldflow Insight" ;;
            "MOBPRO") echo "MotionBuilder" ;;
            "SKETPRO"|"SBPNL") echo "Autodesk SketchBook Pro" ;;
            *) echo "$1" ;;
        esac
    }
    replace_value2() {
        case "$1" in
            "1") echo "Network" ;;
            "2") echo "Standalone" ;;
            "4") echo "User" ;;
            *) echo "$1" ;;
        esac
    }
    decrypted_details=$(echo "$formatted_details" | while IFS= read -r line; do
        if [[ $line == feature_id:* ]]; then
            value="${line#feature_id: }"
            new_value=$(replace_value "$value")
            line="feature_id: $new_value"
        elif [[ $line == lic_method:* ]]; then
            value="${line#lic_method: }"
            new_value=$(replace_value2 "$value")
            line="lic_method: $new_value"
        fi
        echo "$line"
    done)
    echo "$decrypted_details" >> "$log_file"
    temp_file=$(mktemp)
    echo "$decrypted_details" > "$temp_file"
    product=""
    version=""
    type=""
    code=""
    code2=""
    serial_sa=""
    serial_nw=""
    net_server=""
    while IFS= read -r line; do
        key=$(echo "$line" | cut -d: -f1 | xargs)
        value=$(echo "$line" | cut -d: -f2- | xargs)
        case "$key" in
            feature_id)
                if [ -n "$product" ]; then
                    case "$type" in
                        "User")

cat <<EOF >> "$script_dir/$pc_name.xml"
            <FileDecodedContents>
                <Product>$product</Product>
                <Version>$version</Version>
                <Type>$type</Type>
                <ProductCode>$code</ProductCode>
                <BundleCode>$code2</BundleCode>
                <DecodedContentType>AutodeskInstallerHelper</DecodedContentType>
            </FileDecodedContents>
EOF

cat <<EOF >> "$temp_xml_file"
            <SerialNumber>
                <Publisher>Autodesk</Publisher>
                <Description>Installer Helper</Description>
                <Product>$product</Product>
                <Version>$version</Version>
                <Type>$type</Type>
                <ProductCode>$code</ProductCode>
                <BundleCode>$code2</BundleCode>
            </SerialNumber>
EOF

                            ;;
                        "Standalone")

cat <<EOF >> "$script_dir/$pc_name.xml"
            <FileDecodedContents>
                <Product>$product</Product>
                <Version>$version</Version>
                <Serial>$serial_sa</Serial>
                <Type>$type</Type>
                <ProductCode>$code</ProductCode>
                <BundleCode>$code2</BundleCode>
                <DecodedContentType>AutodeskInstallerHelper</DecodedContentType>
            </FileDecodedContents>
EOF

cat <<EOF >> "$temp_xml_file"
            <SerialNumber>
                <Publisher>Autodesk</Publisher>
                <Description>Installer Helper</Description>
                <Product>$product</Product>
                <Version>$version</Version>
                <Serial>$serial_sa</Serial>                
                <Type>$type</Type>
                <ProductCode>$code</ProductCode>
                <BundleCode>$code2</BundleCode>
            </SerialNumber>
EOF

                            ;;
                        "Network")

cat <<EOF >> "$script_dir/$pc_name.xml"
            <FileDecodedContents>
                <Product>$product</Product>
                <Version>$version</Version>
                <Serial>$serial_nw</Serial>
                <Type>$type</Type>
                <ProductCode>$code</ProductCode>
                <BundleCode>$code2</BundleCode>
                <DecodedContentType>AutodeskInstallerHelper</DecodedContentType>
            </FileDecodedContents>
            <FileDecodedContents>
                <Product>$net_server</Product>
                <DecodedContentType>AutodeskAdskFlexLicFile</DecodedContentType>
            </FileDecodedContents>
EOF

cat <<EOF >> "$temp_xml_file"
            <SerialNumber>
                <Publisher>Autodesk</Publisher>
                <Description>Installer Helper</Description>
                <Product>$product</Product>
                <Version>$version</Version>
                <Serial>$serial_nw</Serial>                
                <Type>$type</Type>
                <ProductCode>$code</ProductCode>
                <BundleCode>$code2</BundleCode>
            </SerialNumber>
EOF

                            ;;
                    esac
                fi
                product="$value"
                version=""
                type=""
                code=""
                code2=""
                serial_sa=""
                serial_nw=""
                net_server=""
                ;;
            def_prod_ver)
                version="${value:0:4}"
                ;;
            lic_method)
                type="$value"
                ;;
            def_prod_key)
                code="$value"
                ;;
            sel_prod_key)
                code2="$value"
                ;;
            serial_number_sa)
                serial_sa="$value"
                ;;
            serial_number_nw)
                serial_nw="$value"
                ;;
            net_server)
                net_server="$value"
                ;;
        esac
    done < "$temp_file"
    if [ -n "$product" ]; then
        case "$type" in
            "User")

cat <<EOF >> "$script_dir/$pc_name.xml"
            <FileDecodedContents>
                <Product>$product</Product>
                <Version>$version</Version>
                <Type>$type</Type>
                <ProductCode>$code</ProductCode>
                <BundleCode>$code2</BundleCode>
                <DecodedContentType>AutodeskInstallerHelper</DecodedContentType>
            </FileDecodedContents>
EOF

cat <<EOF >> "$temp_xml_file"
            <SerialNumber>
                <Publisher>Autodesk</Publisher>
                <Description>Installer Helper</Description>
                <Product>$product</Product>
                <Version>$version</Version>
                <Type>$type</Type>
                <ProductCode>$code</ProductCode>
                <BundleCode>$code2</BundleCode>
            </SerialNumber>
EOF

                ;;
            "Standalone")

cat <<EOF >> "$script_dir/$pc_name.xml"
            <FileDecodedContents>
                <Product>$product</Product>
                <Version>$version</Version>
                <Serial>$serial_sa</Serial>
                <Type>$type</Type>
                <ProductCode>$code</ProductCode>
                <BundleCode>$code2</BundleCode>
                <DecodedContentType>AutodeskInstallerHelper</DecodedContentType>
            </FileDecodedContents>
EOF

cat <<EOF >> "$temp_xml_file"
            <SerialNumber>
                <Publisher>Autodesk</Publisher>
                <Description>Installer Helper</Description>
                <Product>$product</Product>
                <Version>$version</Version>
                <Serial>$serial_sa</Serial>                
                <Type>$type</Type>
                <ProductCode>$code</ProductCode>
                <BundleCode>$code2</BundleCode>
            </SerialNumber>
EOF

                ;;
            "Network")

cat <<EOF >> "$script_dir/$pc_name.xml"
            <FileDecodedContents>
                <Product>$product</Product>
                <Version>$version</Version>
                <Serial>$serial_nw</Serial>
                <Type>$type</Type>
                <ProductCode>$code</ProductCode>
                <BundleCode>$code2</BundleCode>
                <DecodedContentType>AutodeskInstallerHelper</DecodedContentType>
            </FileDecodedContents>
            <FileDecodedContents>
                <Product>$net_server</Product>
                <DecodedContentType>AutodeskAdskFlexLicFile</DecodedContentType>
            </FileDecodedContents>
EOF

cat <<EOF >> "$temp_xml_file"
            <SerialNumber>
                <Publisher>Autodesk</Publisher>
                <Description>Installer Helper</Description>
                <Product>$product</Product>
                <Version>$version</Version>
                <Serial>$serial_nw</Serial>                
                <Type>$type</Type>
                <ProductCode>$code</ProductCode>
                <BundleCode>$code2</BundleCode>
            </SerialNumber>
EOF

                ;;
        esac
    fi
    rm "$temp_file"

cat <<EOF >> "$script_dir/$pc_name.xml"
          </DecodedContents>
        </FileInformation>
EOF

fi
echo -e "DONE"


#SUBSCRIPTION LIST

echo -ne "Capturing Subscription Information..."
echo -e "\n*********************************************" >> "$log_file"
echo "DATA 06 - SUBSCRIPTION INFORMATION" >> "$log_file"
echo -e "*********************************************\n" >> "$log_file"

#LOGINSTATE

echo "LoginState.xml" >> "$log_file"
if [ "$os_name" == "Mac" ]; then

cat <<EOF >> "$script_dir/$pc_name.xml"
        <FileInformation>
          <Publisher>Autodesk</Publisher>
          <Description>User Subscriptions</Description>
          <FileName>LoginState.xml</FileName>
          <FilePath>/Library/Application Support/Autodesk/Web Services/</FilePath>
          <DateFound>$query_date</DateFound>
          <FileContents xsi:type="xsd:string"></FileContents>
          <DecodedContents>
EOF

    USERS=$(dscl . list /Users | grep -v "^_\|root\|daemon\|nobody")
    for U in $USERS; do
        homeDirectory=$(dscl . -read /Users/${U} NFSHomeDirectory | awk '{print $2}')
        echo -e "User: ${U}\nHome Directory: $homeDirectory" >> "$log_file"
        LOGINSTATE_XML_SOURCE="$homeDirectory/Library/Application Support/Autodesk/Web Services/LoginState.xml"
       if [ -f "$LOGINSTATE_XML_SOURCE" ]; then
            {
            state_oxy_name=$(grep -o 'UserName="[^"]*' "$LOGINSTATE_XML_SOURCE" | sed 's/UserName="//')
            state_oxy_id=$(grep -o 'UserId="[^"]*' "$LOGINSTATE_XML_SOURCE" | sed 's/UserId="//')
            echo "Oxygen Username: $state_oxy_name"
            echo -e "Oxygen ID: $state_oxy_id"
            } >> "$log_file"

cat <<EOF >> "$script_dir/$pc_name.xml"
            <FileDecodedContents>
            <Product>${U}</Product>
            <Version>$state_oxy_id</Version>
            <Serial>$state_oxy_name</Serial>
            <DecodedContentType>AutodeskOxygen</DecodedContentType>
            </FileDecodedContents>
EOF

        else
            echo "LoginState.xml does not exist." >> "$log_file"

cat <<EOF >> "$script_dir/$pc_name.xml"
            <FileDecodedContents>
            <Product>${U}</Product>
            <Description>LoginState.xml does not contain entries to decode</Description>
            <DecodedContentType>AutodeskOxygen</DecodedContentType>
            </FileDecodedContents>
EOF

        fi
    done

cat <<EOF >> "$script_dir/$pc_name.xml"
          </DecodedContents>
        </FileInformation>
EOF

else

cat <<EOF >> "$script_dir/$pc_name.xml"
        <FileInformation>
          <Publisher>Autodesk</Publisher>
          <Description>User Subscriptions</Description>
          <FileName>LoginState.xml</FileName>
          <FilePath>/.local/share/Autodesk/Web Services/</FilePath>
          <DateFound>$query_date</DateFound>
          <FileContents xsi:type="xsd:string"></FileContents>
          <DecodedContents>
EOF

    USERS=$(awk -F: '($7 !~ /(false|nologin|sync)/) && ($3 == 0 || $3 >= 1000) { print $1 }' /etc/passwd)
    for U in $USERS; do
        homeDirectory=$(awk -F: -v user="${U}" '$1 == user {print $6}' /etc/passwd)
        echo -e "User: ${U}\nHome Directory: $homeDirectory" >> "$log_file"
        LOGINSTATE_XML_SOURCE="$homeDirectory/.local/share/Autodesk/Web Services/LoginState.xml"
        if [ -f "$LOGINSTATE_XML_SOURCE" ]; then
            state_oxy_name=$(grep -o 'UserName="[^"]*' "$LOGINSTATE_XML_SOURCE" | sed 's/UserName="//')
            state_oxy_id=$(grep -o 'UserId="[^"]*' "$LOGINSTATE_XML_SOURCE" | sed 's/UserId="//')
            echo "Oxygen Username: $state_oxy_name" >> "$log_file"
            echo -e "Oxygen ID: $state_oxy_id" >> "$log_file"

cat <<EOF >> "$script_dir/$pc_name.xml"
            <FileDecodedContents>
            <Product>${U}</Product>
            <Version>$state_oxy_id</Version>
            <Serial>$state_oxy_name</Serial>
            <DecodedContentType>AutodeskOxygen</DecodedContentType>
            </FileDecodedContents>
EOF

        else
            echo "LoginState.xml does not exist." >> "$log_file"

cat <<EOF >> "$script_dir/$pc_name.xml"
            <FileDecodedContents>
            <Product>${U}</Product>
            <Description>LoginState.xml does not contain entries to decode</Description>
            <DecodedContentType>AutodeskOxygen</DecodedContentType>
            </FileDecodedContents>
EOF

        fi
    done

cat <<EOF >> "$script_dir/$pc_name.xml"
          </DecodedContents>
        </FileInformation>
EOF

fi


#WEBSERVICES

echo -e "\nWebServices.log" >> "$log_file"
if [ "$os_name" == "Mac" ]; then

cat <<EOF >> "$script_dir/$pc_name.xml"
        <FileInformation>
          <Publisher>Autodesk</Publisher>
          <Description>AutodeskLoginState</Description>
          <FileName>WebServices.log</FileName>
          <FilePath>/Library/Application Support/Autodesk/Web Services/Log/</FilePath>
          <DateFound>$query_date</DateFound>
          <FileContents xsi:type="xsd:string"></FileContents>
          <DecodedContents>
EOF

    USERS=$(dscl . list /Users | grep -v "^_\|root\|daemon\|nobody")
    for U in $USERS; do
        homeDirectory=$(dscl . -read /Users/${U} NFSHomeDirectory | awk '{print $2}')
        echo -e "User: ${U}\nHome Directory: $homeDirectory" >> "$log_file"
        WEBSERVICES_LOG_SOURCE="$homeDirectory/Library/Application Support/Autodesk/Web Services/Log/WebServices.log"
       if [ -f "$WEBSERVICES_LOG_SOURCE" ]; then
            {
            web_oxy_name=$(grep -m 1 -E 'User name:' "$WEBSERVICES_LOG_SOURCE" | grep -o 'User name: [^,]*' | awk '{print $3}')
            web_oxy_id=$(grep -m 1 -E 'User id:' "$WEBSERVICES_LOG_SOURCE" | grep -o 'User id: [^,]*' | awk '{print $3}')
            echo "Oxygen Username: $web_oxy_name"
            echo "Oxygen ID: $web_oxy_id"
            } >> "$log_file"

cat <<EOF >> "$script_dir/$pc_name.xml"
            <FileDecodedContents>
            <Product>${U}</Product>
            <Version>$web_oxy_id</Version>
            <Serial>$web_oxy_name</Serial>
            <DecodedContentType>AutodeskLoginState</DecodedContentType>
            </FileDecodedContents>
EOF

        else
            echo "WebServices.log does not exist." >> "$log_file"

cat <<EOF >> "$script_dir/$pc_name.xml"
            <FileDecodedContents>
            <Product>${U}</Product>
            <DecodedContentType>AutodeskLoginState</DecodedContentType>
            </FileDecodedContents>
EOF

        fi
    done

cat <<EOF >> "$script_dir/$pc_name.xml"
          </DecodedContents>
        </FileInformation>
EOF

else

cat <<EOF >> "$script_dir/$pc_name.xml"
        <FileInformation>
          <Publisher>Autodesk</Publisher>
          <Description>AutodeskLoginState</Description>
          <FileName>WebServices.log</FileName>
          <FilePath>/.local/share/Autodesk/Web Services/Log/</FilePath>
          <DateFound>$query_date</DateFound>
          <FileContents xsi:type="xsd:string"></FileContents>
          <DecodedContents>
EOF

    USERS=$(awk -F: '($7 !~ /(false|nologin|sync)/) && ($3 == 0 || $3 >= 1000) { print $1 }' /etc/passwd)
    for U in $USERS; do
        homeDirectory=$(awk -F: -v user="${U}" '$1 == user {print $6}' /etc/passwd)
        echo -e "User: ${U}\nHome Directory: $homeDirectory" >> "$log_file"
        WEBSERVICES_LOG_SOURCE="$homeDirectory/.local/share/Autodesk/Web Services/Log/WebServices.log"
        if [ -f "$WEBSERVICES_LOG_SOURCE" ]; then
            web_oxy_name=$(grep -m 1 -E 'User name:' "$WEBSERVICES_LOG_SOURCE" | grep -o 'User name: [^,]*' | awk '{print $3}')
            web_oxy_id=$(grep -m 1 -E 'User id:' "$WEBSERVICES_LOG_SOURCE" | grep -o 'User id: [^,]*' | awk '{print $3}')
            echo "Oxygen Username: $web_oxy_name" >> "$log_file"
            echo "Oxygen ID: $web_oxy_id" >> "$log_file"

cat <<EOF >> "$script_dir/$pc_name.xml"
            <FileDecodedContents>
            <Product>${U}</Product>
            <Version>$web_oxy_id</Version>
            <Serial>$web_oxy_name</Serial>
            <DecodedContentType>AutodeskLoginState</DecodedContentType>
            </FileDecodedContents>
EOF

        else
            echo -e "WebServices.log does not exist." >> "$log_file"

cat <<EOF >> "$script_dir/$pc_name.xml"
            <FileDecodedContents>
            <Product>${U}</Product>
            <DecodedContentType>AutodeskLoginState</DecodedContentType>
            </FileDecodedContents>
EOF

        fi
    done

cat <<EOF >> "$script_dir/$pc_name.xml"
          </DecodedContents>
        </FileInformation>
EOF

fi

echo -e "DONE"


#ADLM FILE

echo -ne "Capturing Software Activation Information..."
echo -e "\n*********************************************" >> "$log_file"
echo "DATA 07 - ADLM INFORMATION" >> "$log_file"
echo -e "*********************************************\n" >> "$log_file"

linux_adlm_dir="/var/opt/Autodesk/Adlm"
mac_adlm_dir="/Library/Application Support/Autodesk/ADLM"

if [ "$os_name" == "Mac" ]; then
    adlm_dir="$mac_adlm_dir"
else
    adlm_dir="$linux_adlm_dir"
fi
for html_file in "$adlm_dir"/*RegInfo.html; do
    if [ -e "$html_file" ]; then
        html_content=$(iconv -f UTF-16LE -t UTF-8 "$html_file" | tr -d '\r')
        html_content=$(echo "$html_content" | sed '/^[[:space:]]*$/d; s/^[[:space:]]*//')
        adlm_product_line=$(grep '<tr><td align=left id=1 width=15%>Product:</td><td align=left id=2 width=35%><!--PREVINFO_START-->' <<< "$html_content")
        adlm_serial_number_line=$(grep '<tr><td align=left id=1 width=15%>Serial number:</td><td align=left id=2><!--PREVINFO_START-->' <<< "$html_content")
        adlm_product_key_line=$(grep '<tr><td align=left id=1 width=15%>Product key:</td><td align=left id=2>' <<< "$html_content")
        adlm_date_time_line=$(grep '<center><I><font color="#7f7f7f">' <<< "$html_content")
        adlm_product=$(echo "$adlm_product_line" | sed -e 's/<[^>]*>//g' -e 's/&nbsp;.*//g' | cut -d':' -f2- | sed 's/^ *//')
        adlm_serial=$(echo "$adlm_serial_number_line" | sed -e 's/<[^>]*>//g' -e 's/&nbsp;.*//g' | cut -d':' -f2- | sed 's/^ *//')
        adlm_key=$(echo "$adlm_product_key_line" | sed -e 's/<[^>]*>//g' -e 's/&nbsp;.*//g' | cut -d':' -f2- | sed 's/^ *//')
        adlm_date=$(echo "$adlm_date_time_line" | sed -e 's/<[^>]*>//g' -e 's/&nbsp;.*//g' -e 's/^ *//' -e 's/+.*//' -e 's/\([0-9]\{2\}:[0-9]\{2\}\)$/\1:00/')
        adlm_version=${adlm_product: -4}
        {
            echo "Product: $adlm_product"
            echo "Serial: $adlm_serial"
            echo "Product key: $adlm_key"
            echo "Date: $adlm_date"
            echo "Version: $adlm_version"
            echo ""
        } >> "$log_file"

cat <<EOF >> "$script_dir/$pc_name.xml"
        <FileInformation>
            <Publisher>Autodesk</Publisher>
            <Description>ADLM Data</Description>
            <FileName>${html_file##*/}</FileName>
            <FilePath>$adlm_dir</FilePath>
            <DateFound>$query_date</DateFound>
            <FileContents xsi:type="xsd:string"></FileContents>
            <DecodedContents>
                <FileDecodedContents>
                    <Product>$adlm_product</Product>
                    <Version>$adlm_version</Version>
                    <Description>$adlm_date</Description>
                    <Serial>$adlm_serial</Serial>
                    <DecodedContentType>AutodeskAdlmHtml</DecodedContentType>
                    </FileDecodedContents>
            </DecodedContents>
        </FileInformation>
EOF

    else
        echo "ADLM file not found." >> "$log_file"
    fi
done

echo -e "DONE"


#SERIAL NUMBERS

echo -ne "Capturing Serial Information..."

cat <<EOF >> "$script_dir/$pc_name.xml"
      </Files>
      <SerialNumbers>
EOF

cat "$temp_lgs_file" >> "$script_dir/$pc_name.xml"
cat "$temp_xml_file" >> "$script_dir/$pc_name.xml"


# XML FOOTER

AITlog_content_base64=$(base64 -i "$log_file")

cat <<EOF >> "$script_dir/$pc_name.xml"
      </SerialNumbers>
      <Errors/>
      <SoftwareLastUsed/>
      <AntiVirusProducts/>
      <RunningProcesses/>
      <EnvironmentVariables/>
      <FlexLmLicenseServers/>
      <RegistryKeys/>
      <VirtualMachines/>
      <AITlog>
        <FileName>AITlog.log</FileName>
        <FileContents xsi:type="xsd:base64Binary">$AITlog_content_base64</FileContents>
      </AITlog>
EOF

if [ -f "$error_file" ]; then
    AITerror_content_base64=$(base64 -i "$error_file")

cat <<EOF >> "$script_dir/$pc_name.xml"
      <AITError>
        <FileName>AITerror.log</FileName>
        <FileContents xsi:type="xsd:base64Binary">$AITerror_content_base64</FileContents>
      </AITError>
EOF

fi

cat <<EOF >> "$script_dir/$pc_name.xml"
    </Computer>
  </Computers>
</DataStore>
EOF

echo -e "DONE"

echo -e "\nScan completed successfully."
echo -e "\n*********************************************"

[ -f "$log_file" ] && rm -f "$log_file"
[ -f "$error_file" ] && rm -f "$error_file"