#!/bin/sh


#
# FUNCTIONS
#


#dumped info format
dump()
{

#    DUMP_FILE_INDX=`expr $DUMP_FILE_INDX + 1`
#    DUMP_FILE=$DUMP_DIR/$DUMP_FILE_INDX
#    echo "[$DUMP_HEADER]" > $DUMP_FILE
    echo "[$DUMP_HEADER]" >> $DUMP_FILE
    echo $ECHO_E "$DUMP_BODY" >> $DUMP_FILE

}


sys_info()
{

    if [ $OS = "Linux" ]
    then
        #linux distro
        DUMP_HEADER="Linux Distribution"
        for file in $(ls /etc/*{-,_}{release,version} 2>/dev/null) #this one is for bash  only
        do
            DUMP_BODY=$DUMP_BODY$file"\n"$(cat $file 2>&1)"\n"
        done
        DUMP_BODY=$(echo "$DUMP_BODY"|sed '/^$/d'  2>&1)
        dump
    fi

    #kernel
    DUMP_HEADER="Kernel :: uname -a"
    DUMP_BODY=$(uname -a 2>&1)
    dump

    #gcc
    DUMP_HEADER="Gcc :: gcc -v"
    DUMP_BODY=$(gcc -v 2>&1)
    dump

    if [ $OS = "Linux" ]
    then
        #glibc // removed copyright, left only version info
        DUMP_HEADER="Gnu C Library :: /lib/libc.so.6"
        DUMP_BODY=$(/lib/libc.so.6 |grep "release version" 2>&1)
        dump
    fi

    #Ld
    DUMP_HEADER="Gnu Linker :: ld -v"
    DUMP_BODY=$(ld -v 2>&1)
    dump

    #installed packages
    DUMP_HEADER="Installed packages ::"
    DUMP_BODY=
    for CMD in 'rpm -qa' 'dpkg --list' 'erm -qa' 'pkg_info' 'ls -1 /var/log/packages/'
    do
        which `echo $CMD|awk '{print $1}'` >/dev/null 2>&1
        if [ $? = 0 ]
        then
            TMP=$($CMD 2>/dev/null)
            if [ $? -eq 0 ] && [ -n "$TMP" ]
            then
                DUMP_HEADER=$DUMP_HEADER" "$CMD
                DUMP_BODY=$DUMP_BODY$(echo "$TMP"|sort 2>&1)
            fi
        fi
    done
    echo "[$DUMP_HEADER]" >> packages
    echo $ECHO_E "$DUMP_BODY" >> packages 
#    dump

    if [ $OS = "Linux" ]
    then
        #CPU
        DUMP_HEADER="Cpu :: /proc/cpuinfo"
        DUMP_BODY=$(cat /proc/cpuinfo 2>&1)
        dump

        #mem
        DUMP_HEADER="Memory :: /proc/meminfo"
        DUMP_BODY=$(cat /proc/meminfo 2>&1)
        dump
    fi

    if [ $OS = "FreeBSD" ]
    then
        #sysctl stuff
        DUMP_HEADER="Kernel details :: sysctl"
        DUMP_BODY=$(sysctl hw.machine hw.model hw.physmem hw.usermem kern.maxproc kern.maxfiles kern.maxfilesperproc kern.maxprocperuid kern.openfiles vm.vmtotal hw.clockrate 2>&1)
        dump
    fi

    #dmesg
    DUMP_HEADER="Bootup Messages :: dmesg"
    DUMP_BODY=$(dmesg 2>&1)
    dump

    #mount
    DUMP_HEADER="Mounted FileSystems :: mount"
    DUMP_BODY=$(mount 2>&1)
    dump

    #disk space
    DUMP_HEADER="Disk Space Usage :: df -h"
    DUMP_BODY=$(df -h 2>&1)

    dump
    #disk indodes
    DUMP_HEADER="Disk Inodes Usage :: df -i"
    DUMP_BODY=$(df -i 2>&1)
    dump

    #env
    DUMP_HEADER="Environment :: env"
    DUMP_BODY=$(env 2>&1)
    dump

    #date
    DUMP_HEADER="System Date :: date"
    DUMP_BODY=$(date 2>&1)" - "$(date +%s 2>&1)
    dump

    #ps
    DUMP_HEADER="Running Processes :: "
    DUMP_BODY=$(ps auxHww 2>&1)
    if [ $? = 1 ]
    then
        DUMP_BODY=$(ps axHww 2>&1)
        if [ $? = 1 ]
        then
            DUMP_BODY=$(ps auxww 2>&1)
            DUMP_HEADER=$DUMP_HEADER"ps auxww"
        else
            DUMP_HEADER=$DUMP_HEADER"ps axHww"
        fi
    else
        DUMP_HEADER=$DUMP_HEADER"ps auxHww"
    fi
    dump

    #netstat
    DUMP_HEADER="Network Connections :: "
    DUMP_BODY=$(netstat -anpe 2>&1)
    if [ $? = 1 ]
    then
        #bsd
        DUMP_BODY=$(netstat -an 2>&1)
        DUMP_HEADER=$DUMP_HEADER"netstat -an"
    else
        DUMP_HEADER=$DUMP_HEADER"netstat -anpe"
    fi
    dump

}


#get BitDefender servers common information
bd_srv_info()
{

    #bd info
    DUMP_HEADER="BD Info"
    DUMP_BODY=$($BD_PATH/bin/bd info 2>&1)
    dump

    #bd status
    DUMP_HEADER="BD Status"
    DUMP_BODY=$($BD_PATH/bin/bd status 2>&1)
    dump

    #bd stats
    DUMP_HEADER="BD Stats"
    DUMP_BODY=$($BD_PATH/bin/bd stats 2>&1)
    dump

#    #bdsettings.xml
#    DUMP_HEADER="BD Configuration :: bdsettings.xml"
#    DUMP_BODY=$(cat $BD_PATH/etc/bdsettings.xml 2>&1)
#    dump

#   changed WL/BL locations to reflect current versions (2.x)
    #antispam wl
    DUMP_HEADER="Antispam WL :: as_wlist"
    DUMP_BODY=$(cat $BD_PATH/etc/as_wlist 2>&1)
    dump

    #antispam bl
    DUMP_HEADER="Antispam BL :: as_blist"
    DUMP_BODY=$(cat $BD_PATH/etc/as_blist 2>&1)
    dump

#    if [ $OS = "Linux" ]
#    then
#        #bdsafe list available
#        DUMP_HEADER="BDsafe list available"
#        DUMP_BODY=$($BD_PATH/bin/bdsafe list available 2>&1)
#        dump
#
#        #bdsafe list installed
#        DUMP_HEADER="BDsafe list installed"
#        DUMP_BODY=$($BD_PATH/bin/bdsafe list installed 2>&1)
#        dump
#    fi

    #ls -alR
    DUMP_HEADER="BD_PATH File List :: ls -alR BD_PATH"
    DUMP_BODY=$(ls -alR $BD_PATH 2>&1)
    dump

    if [ $OS = "Linux" ]
    then
        #ls -al /etc/BitDefender
        DUMP_HEADER="BD_ETC Directory :: ls -al BD_ETC"
        DUMP_BODY=$(ls -al /etc/BitDefender 2>&1)
        dump
    fi

    #/var/run/BitDefender
    DUMP_HEADER="BD_VAR_RUN File List :: ls -al BD_VAR_RUN"
    DUMP_BODY=$(ls -al /var/run/BitDefender 2>&1)
    dump

    #/var/tmp
    DUMP_HEADER="BD_VAR_TMP File List :: ls -al BD_VAR_TMP"
    DUMP_BODY=$(ls -al /var/tmp 2>&1)
    dump

    #bd.log_last
#    DUMP_HEADER="BD Log"
#    DUMP_BODY=$(tail -100 $BD_PATH/var/log/bd.log 2>&1)
#    dump

    #bd.log !Virus !debug
#    DUMP_HEADER="BD Log !Virus !Debug"
#    DUMP_BODY=$(grep -vi "virus" $BD_PATH/var/log/bd.log 2>&1|grep -vi "]\ debug" 2>&1|tail -100)
#    dump

    #bd.log debug
#    DUMP_HEADER="BD Log Debug"
#    DUMP_BODY=$(grep -i "]\ debug" $BD_PATH/var/log/bd.log 2>&1|tail -100)
#    dump

    #id
    DUMP_HEADER="BD User :: id BD_USER"
    BD_USER=$(grep "user=" $BD_PATH/bin/bd 2>&1|head -1 2>&1|awk -F "=" '{print $2}' 2>&1)
    DUMP_BODY=$(id $BD_USER 2>&1)
    dump

    if [ $OS = "Linux" ]
    then
        #lsof
        DUMP_HEADER="BD Opened Files :: lsof -u BD_USER"
        DUMP_BODY=$(lsof -u $BD_USER 2>&1)
        dump
    fi

    #plugins.htm
#    DUMP_HEADER="BD Plugins :: plugins.htm"
#    DUMP_BODY=$(cat $BD_PATH/lib/plugins.htm 2>&1)
#    dump

    #update.txt
#    DUMP_HEADER="BD update.txt"
#    DUMP_BODY=$(cat $BD_PATH/lib/Plugins/update.txt 2>&1)
#    dump


}


#get sendmail libmilter information
milter_info()
{

    #product
    DUMP_HEADER="BD Product"
    DUMP_BODY="BitDefender for Sendmail Milter -> $BD_PATH"
    dump

    #sendmail.cf
    DUMP_HEADER="Sendmail Configuration :: sendmail.cf"
    DUMP_BODY=$(cat $CONF_PATH/sendmail.cf 2>&1)
    dump

    #sendmail info
    DUMP_HEADER="Sendmail Details :: sendmail -d0.1"
    DUMP_BODY=$(sendmail -d0.1 </dev/null 2>&1)
    dump

}


#get postfix information
postfix_info()
{

    #product
    DUMP_HEADER="BD Product"
    DUMP_BODY="BitDefender for Postfix -> $BD_PATH"
    dump

    #main.cf
    DUMP_HEADER="Postfix Configuration :: main.cf"
    DUMP_BODY=$(cat $CONF_PATH/main.cf 2>&1)
    dump

    #master.cf
    DUMP_HEADER="Postfix Configuration :: master.cf"
    DUMP_BODY=$(cat $CONF_PATH/master.cf 2>&1)
    dump

}


#get qmail information
qmail_info()
{

    #product
    DUMP_HEADER="BD Product"
    DUMP_BODY="BitDefender for Qmail -> $BD_PATH"
    dump

    #ls -al qmail/bin/
    DUMP_HEADER="QM_BIN_PATH File List :: ls -al QM_BIN_PATH"
    QM_BIN_PATH=$(dirname `awk -F "\"" ' /QmailQueuePath/ {print  $2}' $BD_PATH/etc/bdsettings.xml 2>&1` 2>&1)
    DUMP_BODY=$(ls -al  $QM_BIN_PATH/ 2>&1)
    dump

    #softlimit
    DUMP_HEADER="Qmail Softlimit -> $BD_PATH"
    DUMP_BODY=""
    i=0
    QM_PATH=$(dirname $QM_BIN_PATH 2>&1)
    for file in `find $QM_PATH /service -type f 2>&1`
    do
        if [ "$(grep softlimit $file 2>/dev/null)" ]
        then
            i=$(expr $i + 1)
            DUMP_BODY=$DUMP_BODY"File $i : $file\n  $(grep softlimit $file 2>&1)\n"
        fi
    done
    dump

}


#get smtp_proxy information
smtp_info()
{

    #product
    DUMP_HEADER="BD Product"
    DUMP_BODY="BitDefender SMTP Proxy -> $BD_PATH"
    dump

}


#get courier information
crier_info()
{

    #product
    DUMP_HEADER="BD Product"
    DUMP_BODY="BitDefender for Courier -> $BD_PATH"
    dump

    #ls -al bdsubmint
    DUMP_HEADER="CR_BIN_PATH File List ls -al CR_BIN_PATH"
    CR_BIN_PATH=$(dirname $(awk -F "\"" ' /<Submit/ {print  $2}' $BD_PATH/etc/bdsettings.xml 2>&1) 2>/dev/null)
    DUMP_BODY=$(ls -al  $CR_BIN_PATH 2>&1)
    dump

}


#get samba information
samba_info()
{

    #product
    DUMP_HEADER="BD Product"
    DUMP_BODY="BitDefender for Samba -> $BD_PATH"
    dump

    #version
    DUMP_HEADER="Samba Version"
    DUMP_BODY=$(smbd --version 2>&1)$(smbd -V 2>&1)
    dump

    #smb.conf
    DUMP_HEADER="Samba Configuration :: smb.conf"
    DUMP_BODY=$(cat $CONF_PATH/smb.conf 2>&1)
    dump

    #ls -al vfs
    DUMP_HEADER="Samba Vfs Objects"
    DUMP_BODY=$(ls -al /usr/lib/vfs 2>/dev/null)$(ls -al /usr/local/samba/lib/vfs 2>/dev/null)$(ls -al /usr/lib/samba/vfs 2>/dev/null)
    dump

    #libacl
    DUMP_HEADER="Samba Libacl Support"
    DUMP_BODY=$(strings `which smbd 2>&1` 2>&1| grep libacl 2>&1)
    dump

}


#get comunigate information
cgate_info()
{

    #product
    DUMP_HEADER="BD Product"
    DUMP_BODY="BitDefender for Communigate -> $BD_PATH"
    dump

    #Main.settings
    DUMP_HEADER="Communigate Configuration :: Main.settings"
    DUMP_BODY=$(cat $CONF_PATH/Main.settings 2>&1)
    dump

    #Rules.settings
    DUMP_HEADER="Communigate Configuration :: Rules.settings"
    DUMP_BODY=$(cat $CONF_PATH/Rules.settings 2>&1)
    dump

}


get_os()
{
    echo_style
    OS=`uname 2>&1`
    if [ $(echo $OS|grep -i linux) ]
    then
        OS="Linux"
    elif [ $(echo $OS|grep -i freebsd) ]
    then
        OS="FreeBSD"
    else
        echo
        echo
        echo "${CC_BOLD}Please select the Operating System you are using${CC_RST}"
        echo
        echo "${CC_BOLD}[${CC_RST}1${CC_BOLD}]${CC_RST} Linux "
        echo "${CC_BOLD}[${CC_RST}2${CC_BOLD}]${CC_RST} FreeBSD "
        echo
        echo $ECHO_N "The Operating System you are using ${CC_BOLD}(${CC_RST}1/2${CC_BOLD})${CC_RST} : $ECHO_C "
        read OS
        #make sure we've got a numerical value here
        OS=$(echo $OS | awk '{ret+=$1; print ret}' 2>&1)

        while [ $OS -lt 1 ] || [ $OS -gt 2 ]
        do
            echo
            echo "The only possible selections are : ${CC_BOLD}1, 2${CC_RST} "
            echo $ECHO_N "The Operating Systems product that you are using ${CC_BOLD}(${CC_RST}1/2${CC_BOLD})${CC_RST} : $ECHO_C"
            read OS
            #make sure we've got a numerical value here
            OS=$(echo $OS | awk '{ret+=$1; print ret}' 2>&1)
        done

        case $OS in
        1)
            OS="Linux"
        ;;
        2)
            OS="FreeBSD"
        ;;
        esac
        return 1
    fi
    return 0

}


#get the BitDefender installation directory
get_bd_path()
{
    echo_style
    if [ -d /usr/local/bitdefender ];
    then
        BD_PATH=/usr/local/bitdefender
    else
        if [ -L /etc/BitDefender ];
        then
            BD_PATH=$(readlink /etc/BitDefender 2>/dev/null)
            if [ "$?" -ne 0 ]; then
                BD_PATH=`ls -l "/etc/BitDefender" | sed -re 's/.* ->.(.*)/\1/'`
            fi
            BD_PATH=${BD_PATH%\/}
            BD_PATH=${BD_PATH%\/etc}
        fi
    fi

    if [ "$BD_PATH" = '' ];
    then
        if [ -e /etc/init.d/bd ]
        then
            BD_PATH=$(stat /etc/init.d/bd|head -1|awk -F "\`" '{print $3}'|awk -F "bin/bd" '{print $1}')
        elif [ -e /etc/rc.d/rc.bd ]
        then
            BD_PATH=$(stat /etc/rc.d/rc.bd|head -1|awk -F "\`" '{print $3}'|awk -F "bin/bd" '{print $1}')
        fi
    fi

    if [ "$BD_PATH" = '' ];
    then
        if [ $OS = "Linux" ]
        then
            BD_PATH_DEF="/opt/BitDefender/"
        elif [ $OS = "FreeBSD" ]
        then
            BD_PATH_DEF="/usr/local/bitdefender/"
        fi

        echo
        echo
        echo "${CC_BOLD}Please enter the BitDefender instllation directory${CC_RST}"
        echo

        while [ 1 ];
        do
            echo $ECHO_N "The BitDefender installation directory ${CC_BOLD}[${CC_RST}${BD_PATH_DEF}${CC_BOLD}]${CC_RST} : $ECHO_C"
            read BD_PATH
            if [ -d "$BD_PATH" ] ;
            then
                break
            elif [ "$BD_PATH" = ''  ] && [ -d $BD_PATH_DEF ];
            then
                BD_PATH=$BD_PATH_DEF
                break
            fi
        done
        return 1
    fi
    return 0

}



#get the BitDefender product installed
get_bd_prod()
{
echo_style

FUSER=$(fuser -v -n tcp 25 2>&1|awk '/25/ {print $5}')

#if [ $FUSER ] && [ ${PIPESTATUS[0]} = 0 ] (not compatible with ubuntu, which uses dash as default shell, not bash)
if [ -n "$FUSER" ]
    then
    
case $FUSER in 

    sendmail)
	BD_PROD=1
	;;
    master)
	BD_PROD=2
	;;
    tcpserver)
	BD_PROD=3
	;;
    bdsmtpd)
	BD_PROD=4
	;;
    couriertcpd)
	BD_PROD=5
	;;
    CGServer)
	BD_PROD=6
	;;
esac

#if present, grab also BitDefender for Samba info
    if [ -e $BD_PATH/var/samba.inf ]
	then BD_PROD=""$BD_PROD"7"
    fi

else


        echo
        echo
        echo "${CC_BOLD}Please select the BitDefender for Linux product that you are using${CC_RST} "
        echo
        echo "${CC_BOLD}[${CC_RST}1${CC_BOLD}]${CC_RST} BitDefender for Sendmail Milter"
        echo "${CC_BOLD}[${CC_RST}2${CC_BOLD}]${CC_RST} BitDefender for Postfix"
        echo "${CC_BOLD}[${CC_RST}3${CC_BOLD}]${CC_RST} BitDefender for Qmail"
        echo "${CC_BOLD}[${CC_RST}4${CC_BOLD}]${CC_RST} BitDefender SMTP Proxy"
        echo "${CC_BOLD}[${CC_RST}5${CC_BOLD}]${CC_RST} BitDefender for Courier"
        echo "${CC_BOLD}[${CC_RST}6${CC_BOLD}]${CC_RST} BitDefender for Communigate"
        echo "${CC_BOLD}[${CC_RST}7${CC_BOLD}]${CC_RST} BitDefender for Samba"
	echo "${CC_BOLD}[${CC_RST}17/27/37/47/57/67(the first digit corresponds to one of the above options)${CC_BOLD}]${CC_RST} BitDefender for Mail and BitDefender for Samba"
	
        echo
        echo $ECHO_N "The BitDefender product that you are using ${CC_BOLD}(${CC_RST}1/2/3/4/5/6/7/17/27/37/47/57/67${CC_BOLD})${CC_RST} : $ECHO_C "
        read BD_PROD
        #make sure we've got a numerical value here
#        BD_PROD=$(echo $BD_PROD | awk '{ret+=$1; print ret}' 2>&1)
	return 1
fi
return 0
}

check_for_zero_size_variable()
{
	echo_style
        if [ -z $BD_PROD ]
	    then
		echo
		echo $ECHO_N "Please enter your choise here: "
		read BD_PROD
		return 1
	fi
	return 0
}

os_differentiated_stream_editor()
{
if [ $OS = "Linux" ]; then
    SED='sed -n /^[0-6]\?[7]\?$/p'
elif [ $OS = "FreeBSD" ];then
    SED='sed -E -n /^[0-6]?[7]?$/p'
else
    SED='sed -n /^[0-6]\?[7]\?$/p'
fi
}

check_for_correct_product_number()
{	
	echo_style
	BD_PROD=$(echo $BD_PROD | $SED)
	
        if [ -z "$BD_PROD" ]
	    then
	        echo
        	echo "The only possible selections are : ${CC_BOLD}1, 2, 3, 4, 5, 6, 7, 17, 27, 37, 47, 57, 67${CC_RST} "
	        echo $ECHO_N "The BitDefender product that you are using ${CC_BOLD}(${CC_RST}1/2/3/4/5/6/7/17/27/37/47/57/67${CC_BOLD})${CC_RST} : $ECHO_C"
		read BD_PROD
		return 1
	    fi		
        return 0
}

#get the configuration files of the real server (only if it's necessary)
get_conf()
{
    echo_style
    CONF_PATH=''
    case $1 in
        'Sendmail' )
            if [ -e /etc/mail/sendmail.cf ]
            then
                CONF_PATH=/etc/mail/
            elif [ -e /etc/sendmail.cf ]
            then
                CONF_PATH=/etc/
            fi
        ;;
        'Postfix')
            if [ -e /etc/postfix/main.cf ] && [ -e /etc/postfix/master.cf ]
            then
                CONF_PATH=/etc/postfix
            fi
        ;;
        'Samba')
            if [ -e /etc/samba/smb.conf ]
            then
                CONF_PATH=/etc/samba
            elif [ -e /usr/local/samba/etc/smb.conf ]
            then
                CONF_PATH=/usr/local/samba/etc/
            fi
        ;;
        'Communigate')
            if [ -e /var/CommuniGate/Settings/Main.settings ] && [ -e /var/CommuniGate/Settings/Rules.settings ]
            then
                CONF_PATH=/var/CommuniGate/Settings/
            fi
        ;;
    esac
    if [ "$CONF_PATH" = '' ]
    then
        echo
        echo
        echo "${CC_BOLD}Please enter the directory containing $1's configuration files${CC_RST}"
        echo
        while [ "$CONF_PATH" = '' ]
        do
            echo $ECHO_N "The directory with $1's configuration files : $ECHO_C"
            read CONF_PATH
            if [ -e "$CONF_PATH" ]
            then
                break
            else
                CONF_PATH=''
            fi
        done
        return 1
    fi
    return 0

}


rot_progr_start()
{

    while [ 1 ]
    do
        for i in  '|' '/' '-' '\'
        do
            #ip the parent dies we also ...
            STAT=$(ps -p $$ -o ppid|wc -l)
            if [ $STAT = 2 ]
            then
                printf "\010$i"
                sleep 0.01
            else
                return
            fi
        done
    done

}


rot_progr_stop()
{

    exec 3>&1
    exec 4>&2
    exec 1>/dev/null
    exec 2>/dev/null
    kill $!
    wait $!
    exec 1>&3
    exec 2>&4
    printf "\010$1"

}


#
# MAIN
#

clear

#test color change style
if [ -t 1 ];
then
    CC_BOLD="[1m"
    CC_RST="[0m"
fi


#test echo style

echo_style()
{
case `echo "testing\c"; echo 1,2,3`,`echo -n testing; echo 1,2,3` in
  *c*,-n*) ECHO_N= ECHO_C='' ;;
  *c*,*  ) ECHO_N=-n ECHO_C= ;;
  *)       ECHO_N= ECHO_C='\c' ;;
esac



case `echo -e` in
  -e*)
    ECHO_E=''
    ;;
  *)
    ECHO_E='-e'
esac
}

echo_style

#log all script output
# LOG_FN="bdsysinfo.log"
# :>$LOG_FN
# exec 1>>$LOG_FN
# exec 2>>$LOG_FN
# TTY=`tty`
# tail -f $LOG_FN >$TTY &
# TAIL_PID=$!

#create the dump dir
#DUMP_DIR=$(mktemp -d bdsysinfo.XXXXXX)
# added a timestamp to the filename, for better
# tracking issues with multiple sysinfo reports

DUMP_FILE=$(mktemp bdsysinfo.XXXXXX)
if [ $? -ne 0 ]
then
    echo
    echo
    echo "${CC_BOLD}Can not create temporary file. Exiting ... ${CC_RST}"
    echo
    echo
    exit 1
fi

#DUMP_FILE_INDX=0

#always get system info
echo
echo
echo $ECHO_N "${CC_BOLD} >> Gathering general system information ... ${CC_RST} $ECHO_C"

#determine the OS
get_os
if [ $? = 1 ]
then
    echo
    echo $ECHO_N "${CC_BOLD} >> Gathering general system information ... ${CC_RST} $ECHO_C"
fi

rot_progr_start&

#get OS common information
sys_info
rot_progr_stop "done"

echo
echo
echo
echo $ECHO_N "${CC_BOLD} >> Gathering Bitdefender specific information ... ${CC_RST} $ECHO_C"

#bd installation details
get_bd_path
if [ $? = 1 ]
then
    echo
    echo $ECHO_N "${CC_BOLD} >> Gathering Bitdefender specific information ... ${CC_RST} $ECHO_C"
fi

get_bd_prod
if [ $? = 1 ]
    then 
    echo
    while [ 1 ]
	do check_for_zero_size_variable
	    if [ $? = 1 ]
		then continue
	    fi
	    os_differentiated_stream_editor
	    check_for_correct_product_number
	    if [ $? = 0 ]
		then break
	    fi
	done
    echo
    echo $ECHO_N "${CC_BOLD} >> Gathering Bitdefender specific information ... ${CC_RST} $ECHO_C"
fi

rot_progr_start&

#bd servers info
bd_srv_info


#product specific info
case $BD_PROD in
    1)
        rot_progr_stop " "
        get_conf 'Sendmail'
        if [ $? = 1 ]
        then
            echo
            echo $ECHO_N "${CC_BOLD} >> Gathering Bitdefender specific information ... ${CC_RST} $ECHO_C"
        fi
        rot_progr_start&
        milter_info
        rot_progr_stop "done"
	;;
    2)
        rot_progr_stop " "
        get_conf 'Postfix'
        if [ $? = 1 ]
        then
            echo
            echo $ECHO_N "${CC_BOLD} >> Gathering Bitdefender specific information ... ${CC_RST} $ECHO_C"
        fi
        rot_progr_start&
        postfix_info
        rot_progr_stop "done"
        ;;
    3)
        qmail_info
        rot_progr_stop "done"
	;;
    4)
        smtp_info
        rot_progr_stop "done"
        ;;
    5)
        crier_info
        rot_progr_stop "done"
	;;
    6)
        rot_progr_stop " "
        get_conf 'Communigate'
        if [ $? = 1 ]
        then
            echo
            echo $ECHO_N "${CC_BOLD} >> Gathering Bitdefender specific information ... ${CC_RST} $ECHO_C"
        fi
        rot_progr_start&
        cgate_info
        rot_progr_stop "done"
        ;;
    7)
        rot_progr_stop " "
        get_conf 'Samba'
        if [ $? = 1 ]
        then
	    echo
            echo $ECHO_N "${CC_BOLD} >> Gathering Bitdefender specific information ... ${CC_RST} $ECHO_C"
        fi
        rot_progr_start&
        samba_info
        rot_progr_stop "done"
	;;
    17|27|37|47|57|67)
	rot_progr_stop " "
        get_conf 'Samba'
        if [ $? = 1 ]
        then
            echo
            echo $ECHO_N "${CC_BOLD} >> Gathering Bitdefender specific information ... ${CC_RST} $ECHO_C"
        fi
        rot_progr_start&
        samba_info
	BD_MAIL_PROD=$(echo $BD_PROD|sed -e 's/.$//')
	case $BD_MAIL_PROD in
	    1)
	        rot_progr_stop " "
    		get_conf 'Sendmail'
	        if [ $? = 1 ]
    		    then
            		echo
	                echo $ECHO_N "${CC_BOLD} >> Gathering Bitdefender specific information ... ${CC_RST} $ECHO_C"
	        fi
	        rot_progr_start&
    		milter_info
	        rot_progr_stop "done"
	        ;;
	    2)
	        rot_progr_stop " "
    		get_conf 'Postfix'
	        if [ $? = 1 ]
		    then
    		        echo
		        echo $ECHO_N "${CC_BOLD} >> Gathering Bitdefender specific information ... ${CC_RST} $ECHO_C"
    		fi
	        rot_progr_start&
	        postfix_info
   		rot_progr_stop "done"
	        ;;
	    3)
    		qmail_info
	        rot_progr_stop "done"
	        ;;
	    4)
	        smtp_info
	        rot_progr_stop "done"
	        ;;
	    5)
	        crier_info
	        rot_progr_stop "done"
	        ;;
	    6)
    		rot_progr_stop " "
	        get_conf 'Communigate'
	        if [ $? = 1 ]
	        then
    		    echo
    		    echo $ECHO_N "${CC_BOLD} >> Gathering Bitdefender specific information ... ${CC_RST} $ECHO_C"
	        fi
    		rot_progr_start&
	        cgate_info
	        rot_progr_stop "done"
	        ;;
	esac
esac	

#ziiiip it!

echo
echo
echo
echo $ECHO_N "${CC_BOLD} >> Generating report file 'bdsysinfo.tar.gz' ...  ${CC_RST}" $ECHO_C

rot_progr_start&
# the list of packages and the bdsettings files
# are now standalone for better readability
# also added some logs
mkdir logs
cp -p $BD_PATH/etc/bdsettings.xml bdsettings_.xml
cp -p $BD_PATH/var/log/update.log logs/update.log
cp -p $BD_PATH/var/log/error.log logs/error.log
cp -p $BD_PATH/var/log/mail.log logs/mail.log

if [ -e $BD_PATH/etc/cf/Default-cf.conf ]
    then
	cp -p /$BD_PATH/etc/cf/Default-cf.conf Default-cf_.conf
fi

if [ -e $BD_PATH/etc/bdemagentd.xml ];
    then
        cp -p $BD_PATH/etc/bdemagentd.xml bdemagentd_.xml
fi

if [ -e /var/log/maillog ]
    then
        cp -p /var/log/maillog logs/system_mail.log
elif [ -e /var/log/mail ]
    then
        cp -p /var/log/mail logs/system_mail.log
elif [ -e /var/log/mail.log ]
    then
        cp -p /var/log/mail.log logs/system_mail.log
fi

$(tar -czf bdsysinfo.tar.gz $DUMP_FILE bdsettings_.xml bdemagentd_.xml packages Default-cf_.conf logs/* >/dev/null 2>&1)
rm -rf logs
rm -f packages
rm -f bdsettings_.xml
rm -f bdemagentd_.xml
rm -f Default-cf_.conf
rot_progr_stop "done"



if [ $? -ne 0 ];
then
    echo
    echo
    echo "${CC_BOLD}An error occurred during archiving gathered information. Exiting ... ${CC_RST}"
    echo
    echo
    exit 1
fi


echo
echo
echo
echo "${CC_BOLD} >> Finished ${CC_RST}"
echo
echo

#mv $DUMP_DIR/bdsysinfo.tar.gz .
rm -f $DUMP_FILE

#add the log
# tar -rf bdsysinfo.tar $LOG_FN >/dev/null 2>&1
# gzip -f bdsysinfo.tar >/dev/null 2>&1

# kill $TAIL_PID
# wait $TAIL_PID
# rm -f $LOG_FN

exit 0
