#!/bin/bash -x

touch /var/log/cisco/flush_vxc_logs
touch /var/log/cisco/flush_channel_logs
sleep 2.2

################################################################
# Variables that dictate the files gathered and commands used.
################################################################

# The directory to be used to store the temporary files.
TEMP_DIRECTORY=/tmp/cisco

# Standard outputs for files being collected.
STDOUT_RESULT=$TEMP_DIRECTORY/collection-output.stdout.txt
STDERR_RESULT=$TEMP_DIRECTORY/collection-output.stderr.txt

# USB redirection.
ICA_USB_CONFIGURATION=$TEMP_DIRECTORY/icaclient-usb-config.txt

# Pulseaudio output files.
PULSEAUDIO_CONF_OUTPUT=$TEMP_DIRECTORY/pulseaudio-dump-conf-output.txt
PULSEAUDIO_MODULES_OUTPUT=$TEMP_DIRECTORY/pulseaudio-dump-modules-output.txt

# ls output files.
LSMOD_OUTPUT=$TEMP_DIRECTORY/lsmod-output.txt
LSHAL_OUTPUT=$TEMP_DIRECTORY/lshal-output.txt

# DMesg output file.
DMESG_OUTPUT=$TEMP_DIRECTORY/dmesg-output.txt

# df output file.
DF_OUTPUT=$TEMP_DIRECTORY/df-output.txt

# lspci output file.
LSPCI_OUTPUT=$TEMP_DIRECTORY/lspci-output.txt

# ifconifg output file.
IFCONFIG_OUTPUT=$TEMP_DIRECTORY/ifconfig-output.txt

# IP Address output file.
IPADDR_OUTPUT=$TEMP_DIRECTORY/ip-addr-output.txt

# USB Device output file.
USB_VERSION_OUTPUT=$TEMP_DIRECTORY/usb-version-output.txt

# Session output file.
SESSION_OUTPUT=$TEMP_DIRECTORY/session-output.txt

################################################################
# Function Definitions
################################################################


get_primary_logs() {
    mkdir -p $TEMP_DIRECTORY
    
    # Copy the  most recent logs.
    cp -fv /var/log/cisco/*.log $TEMP_DIRECTORY >> $STDOUT_RESULT 2>> $STDERR_RESULT
    #cp -fv /var/log/cisco/*.log.1 $TEMP_DIRECTORY >> $STDOUT_RESULT 2>> $STDERR_RESULT
    cp -fv /var/log/cisco/vxc_*.dmp $TEMP_DIRECTORY >> $STDOUT_RESULT 2>> $STDERR_RESULT
    cp -fv /etc/VXMEPlatformInfo.cisco $TEMP_DIRECTORY >> $STDOUT_RESULT 2>> $STDERR_RESULT
    cp -fv /var/log/cisco/*.bmp $TEMP_DIRECTORY >> $STDOUT_RESULT 2>> $STDERR_RESULT
    start=1
    while ((start<=VXC_LOG_COUNT&&start<VXC_LOG_COUNT_PERPACKAGE))
    do 
        cp -fv /var/log/cisco/vxc.log.${start} $TEMP_DIRECTORY >> $STDOUT_RESULT 2>> $STDERR_RESULT
        ((start+=1))
    done

    start=1
    while ((start<=VIRTUALCHANNEL_LOG_COUNT&&start<VIRTUALCHANNEL_LOG_COUNT_PERPACKAGE))
    do 
        cp -fv /var/log/cisco/VirtualChannel.log.${start} $TEMP_DIRECTORY >> $STDOUT_RESULT 2>> $STDERR_RESULT
        ((start+=1))
    done
}

get_secondary_logs() {
    if [ "$(id -u)" == "0" ]; then
        lsusb -v 2>/dev/null | grep -A2 idVendor > $USB_VERSION_OUTPUT 2>> $STDERR_RESULT
    fi

    #cp -fv /var/log/cisco/*.log.2 $TEMP_DIRECTORY >> $STDOUT_RESULT 2>> $STDERR_RESULT
    start=$((VXC_LOG_COUNT_PERPACKAGE*1))
    max=$((VXC_LOG_COUNT_PERPACKAGE*2))
    while ((start<max&&start<=VXC_LOG_COUNT))
    do 
        cp -fv /var/log/cisco/vxc.log.${start} $TEMP_DIRECTORY >> $STDOUT_RESULT 2>> $STDERR_RESULT
        ((start+=1))
    done

    start=$((VIRTUALCHANNEL_LOG_COUNT_PERPACKAGE*1))
    max=$((VIRTUALCHANNEL_LOG_COUNT_PERPACKAGE*2))
    while ((start<=VIRTUALCHANNEL_LOG_COUNT&&start<max))
    do 
        cp -fv /var/log/cisco/VirtualChannel.log.${start} $TEMP_DIRECTORY >> $STDOUT_RESULT 2>> $STDERR_RESULT
        ((start+=1))
    done
    #collecting list of installed packages
	dpkg -l >> $TEMP_DIRECTORY/versionInfo.txt
    #Grep the process list from system
	#top -n 1 >> $TEMP_DIRECTORY/topOutput.txt

    # Copy Citrix configuration.
    ls -alhR /usr/lib/ICAClient >> $ICA_USB_CONFIGURATION 2>> $STDERR_RESULT
    cp -Rfv /usr/lib/ICAClient/config/usb.conf $TEMP_DIRECTORY >> $STDOUT_RESULT 2>> $STDERR_RESULT
    cp -Rfv /usr/lib/ICAClient/config/module.ini $TEMP_DIRECTORY >> $STDOUT_RESULT 2>> $STDERR_RESULT
	
    # Copy files in the temporary directory.
    #cp -Rfv /tmp/*.log $TEMP_DIRECTORY >> $STDOUT_RESULT 2>> $STDERR_RESULT
    #cp -Rfv /tmp/*.txt $TEMP_DIRECTORY >> $STDOUT_RESULT 2>> $STDERR_RESULT
    
    # Pulseaudio configuration.
    if [ "$(id -u)" == "0" ]; then
        pulseaudio --dump-conf > $PULSEAUDIO_CONF_OUTPUT 2>> $STDERR_RESULT
        pulseaudio --dump-modules > $PULSEAUDIO_MODULES_OUTPUT 2>> $STDERR_RESULT
    fi
    
    # Copy the hosts file.
    cp -Rfv /etc/hosts $TEMP_DIRECTORY >> $STDOUT_RESULT 2>> $STDERR_RESULT

    # List different system modules, HAL entries, etc.
    if [ "$(id -u)" == "0" ]; then
        lsmod > $LSMOD_OUTPUT 2>> $STDERR_RESULT
    fi
    #lshal > $LSHAL_OUTPUT 2>> $STDERR_RESULT

    # Output dmesg to a file.
    dmesg > $DMESG_OUTPUT 2>> $STDERR_RESULT
    
    # Output the disk usage to a file.
    df > $DF_OUTPUT 2>> $STDERR_RESULT

    # Output the ip address to a file.
    ip addr > $IPADDR_OUTPUT 2>> $STDERR_RESULT

    # Output current session details to a file.
    echo "Uname output:" >> $SESSION_OUTPUT 2>> $STDERR_RESULT
    uname -a >> $SESSION_OUTPUT 2>> $STDERR_RESULT
    echo -e "\n\nWhoami output:" >> $SESSION_OUTPUT 2>> $STDERR_RESULT
    whoami >> $SESSION_OUTPUT 2>> $STDERR_RESULT
    echo -e "\n\nenv output:" >> $SESSION_OUTPUT 2>> $STDERR_RESULT
    env >> $SESSION_OUTPUT 2>> $STDERR_RESULT
    echo -e "\n\nps aux output:" >> $SESSION_OUTPUT 2>> $STDERR_RESULT
    ps aux >> $SESSION_OUTPUT 2>> $STDERR_RESULT
}

get_tertiary_logs() {
    mkdir -p $TEMP_DIRECTORY
    start=$((VXC_LOG_COUNT_PERPACKAGE*2))
    max=$((VXC_LOG_COUNT_PERPACKAGE*3))
    while ((start<max&&start<=VXC_LOG_COUNT))
    do 
        cp -fv /var/log/cisco/vxc.log.${start} $TEMP_DIRECTORY >> $STDOUT_RESULT 2>> $STDERR_RESULT
        ((start+=1))
    done

    start=$((VIRTUALCHANNEL_LOG_COUNT_PERPACKAGE*2))
    max=$((VIRTUALCHANNEL_LOG_COUNT_PERPACKAGE*3))
    while ((start<=VIRTUALCHANNEL_LOG_COUNT&&start<max))
    do 
        cp -fv /var/log/cisco/VirtualChannel.log.${start} $TEMP_DIRECTORY >> $STDOUT_RESULT 2>> $STDERR_RESULT
        ((start+=1))
    done
}

get_root_logs() {

    # Copy the /var/log/cisco/ older logs.
    start=$((VXC_LOG_COUNT_PERPACKAGE*3))
    while ((start<=VXC_LOG_COUNT))
    do 
        cp -fv /var/log/cisco/vxc.log.${start} $TEMP_DIRECTORY >> $STDOUT_RESULT 2>> $STDERR_RESULT
        ((start+=1))
    done

    start=$((VIRTUALCHANNEL_LOG_COUNT_PERPACKAGE*3))
    while ((start<=VIRTUALCHANNEL_LOG_COUNT))
    do 
        cp -fv /var/log/cisco/VirtualChannel.log.${start} $TEMP_DIRECTORY >> $STDOUT_RESULT 2>> $STDERR_RESULT
        ((start+=1))
    done
    # This section specifies commands that are available as root only.
    if [ "$(id -u)" == "0" ]; then
	    # Get the list of PCI devices attached to the machine.
	    lspci -t -vv -k -nn > $LSPCI_OUTPUT 2>> $STDERR_RESULT

	    # Interface information.
	    ifconfig > $IFCONFIG_OUTPUT 2>> $STDERR_RESULT
    else
	    # We will let the user know if they run as root it will help us more.
	    echo "As root this script can collect more data."
    fi
}

get_logcollection_logs() {
    mkdir -p $TEMP_DIRECTORY
    
    # Copy the /var/log/cisco/LogCollection.text.
    cp -fv /var/log/cisco/LogCollection.text* $TEMP_DIRECTORY >> $STDOUT_RESULT 2>> $STDERR_RESULT
}

GetLogCounts() {
	if [ "$1" = "vxc" ]; then
		count=`ls -artl /var/log/cisco/vxc.log.* | wc -l`
		echo "$count"
		return $count
	else
		count=`ls -artl /var/log/cisco/VirtualChannel.log.* | wc -l`
		echo "$count"
		return $count
	fi
}

archive_logs() {
    # Setup any common steps
    # Setup the temporary folder.
    echo "Called with: OUTPUT_FILE: $OUTPUT_FILE, LOG_PART: $LOG_PART"

    mkdir $TEMP_DIRECTORY

    # Call specific function to gather only the requested logs
    case $LOG_PART in
      0)
        echo "Getting all logs"
        get_primary_logs
        get_secondary_logs
        get_tertiary_logs
        get_root_logs
        get_logcollection_logs
		cd $TEMP_DIRECTORY
        ;;
      1)
        echo "Getting primary logs"
        get_primary_logs
		cd $TEMP_DIRECTORY
        ;;
      2)
        echo "Getting secondary logs"
        get_secondary_logs
		cd $TEMP_DIRECTORY
        ;;
      3)
        echo "Getting tertiary logs"
        get_tertiary_logs
		cd $TEMP_DIRECTORY
        ;;
      4)
        echo "Getting root logs"
        get_root_logs
		cd $TEMP_DIRECTORY
        ;;
      5)
        echo "Getting LogCollection.text"
        get_logcollection_logs
		cd $TEMP_DIRECTORY
        ;;
    esac
    
    # Common cleanup after specific funtion
    GZIP=-2 tar zcf $OUTPUT_FILE *

    rm -Rf $TEMP_DIRECTORY

    echo "VXC troubleshooting file located at: $OUTPUT_FILE"
}

################################################################
# Main logic of the script
################################################################
MACHINE_NAME=$(uname -n)
CURRENT_TIME=$(date '+%Y-%m-%d-%H-%M-%S')
OUTPUT_FILE=/tmp/VXC-$MACHINE_NAME-$CURRENT_TIME.tar.gz

LOG_PART=0

GetLogCounts vxc
VXC_LOG_COUNT=$?
VXC_LOG_COUNT_PERPACKAGE=$((VXC_LOG_COUNT/4))
if ((VXC_LOG_COUNT_PERPACKAGE<2)); then
    VXC_LOG_COUNT_PERPACKAGE=2
fi

GetLogCounts VirtualChannel
VIRTUALCHANNEL_LOG_COUNT=$?
VIRTUALCHANNEL_LOG_COUNT_PERPACKAGE=$((VIRTUALCHANNEL_LOG_COUNT/4))
if ((VIRTUALCHANNEL_LOG_COUNT_PERPACKAGE<2)); then
    VIRTUALCHANNEL_LOG_COUNT_PERPACKAGE=2
fi

# Read the arguments
if [ $# -eq 0 ]; then # The default output if no arguments
	archive_logs
elif [ $# -eq 2 ]; then # if there are exactly two arguments, check them
    OUTPUT_FILE=$1
    LOG_PART=$2
    archive_logs
else # if there is any other number of arguments
	echo "To use this script, pass the filename of the target file to use for the .tar.gz archive and log importance level, or pass nothing to use the default."
	echo "Usage: collect-files <file-name> <log-part 1:4>"
	exit 0;
fi


################################################################

