#!/bin/bash


DEBUG="1"
LOGFILE="/dev/null"

while [ -n "$1" ]
do
        if [ -z "$2" ]
        then
            echo "ERROR! no values specified after "$1" !" | tee -a $LOG_FILE
            echo "$USAGE"
            exit 1
        fi
        param="$2"
        case "$1" in

                --debug)
                        DEBUG=$param
                        shift ;;
                --logfile)
                        LOGFILE=$param
                        shift ;;

                --) shift
                break ;;
                *) echo "ERROR! $1 is not an option"
                   echo "$USAGE"
                   exit 1
        esac
        shift
done





lowercase(){
        echo "$1" | sed "y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/"
}

####################################################################
# Get System Info
# https://github.com/coto/server-easy-install/blob/master/lib/core.sh
####################################################################
shootProfile(){
        OS=`lowercase \`uname\``
        KERNEL=`uname -r`
        MACH=`uname -m`

        if [ "${OS}" == "windowsnt" ]; then
                OS=windows
        elif [ "${OS}" == "darwin" ]; then
                OS=mac
        else
                OS=`uname`
                if [ "${OS}" = "SunOS" ] ; then
                        OS=Solaris
                        ARCH=`uname -p`
                        OSSTR="${OS} ${REV}(${ARCH} `uname -v`)"
                elif [ "${OS}" = "AIX" ] ; then
                        OSSTR="${OS} `oslevel` (`oslevel -r`)"
                elif [ "${OS}" = "Linux" ] ; then
                        if [ -f /etc/redhat-release ] ; then
                                DistroBasedOn='RedHat'
                                DIST=`cat /etc/redhat-release |sed s/\ release.*//`
                                PSUEDONAME=`cat /etc/redhat-release | sed s/.*\(// | sed s/\)//`
                                REV=`cat /etc/redhat-release | sed s/.*release\ // | sed s/\ .*//`
                        elif [ -f /etc/SuSE-release ] ; then
                                DistroBasedOn='SuSe'
                                PSUEDONAME=`cat /etc/SuSE-release | tr "\n" ' '| sed s/VERSION.*//`
                                REV=`cat /etc/SuSE-release | tr "\n" ' ' | sed s/.*=\ //`
                        elif [ -f /etc/mandrake-release ] ; then
                                DistroBasedOn='Mandrake'
                                PSUEDONAME=`cat /etc/mandrake-release | sed s/.*\(// | sed s/\)//`
                                REV=`cat /etc/mandrake-release | sed s/.*release\ // | sed s/\ .*//`
                        elif [ -f /etc/debian_version ] ; then
                                DistroBasedOn='Debian'
                                if [ -f /etc/lsb-release ] ; then
                                        DIST=`cat /etc/lsb-release | grep '^DISTRIB_ID' | awk -F=  '{ print $2 }'`
                                        PSUEDONAME=`cat /etc/lsb-release | grep '^DISTRIB_CODENAME' | awk -F=  '{ print $2 }'`
                                        REV=`cat /etc/lsb-release | grep '^DISTRIB_RELEASE' | awk -F=  '{ print $2 }'`
                                fi
                        elif [ -f /etc/arch-release ] ; then
                                DistroBasedOn='Arch linux'
                                DIST='Arch' 
                        elif [ -f /etc/os-release ] ; then
                                DistroBasedOn='RedHat'
                                DIST=$(grep  -m1 "NAME=" /etc/os-release   | sed 's/NAME=//' |  sed 's/"//g')   
                                REV=$(grep  -m1 "VERSION=" /etc/os-release   | sed 's/VERSION=//' |  sed 's/"//g')
                        fi
                        if [ -f /etc/UnitedLinux-release ] ; then
                                DIST="${DIST}[`cat /etc/UnitedLinux-release | tr "\n" ' ' | sed s/VERSION.*//`]"
                        fi
                        OS=`lowercase $OS`
                        DistroBasedOn=`lowercase $DistroBasedOn`
            SUB_REV=${REV:0:1} 
                 
                        readonly OS
                        readonly DIST
                        readonly DistroBasedOn
                        readonly PSUEDONAME
                        readonly REV
                        readonly KERNEL
                        readonly MACH
            readonly SUB_REV
      
                fi

        fi
}
shootProfile

if [ $DEBUG = "1" ] ; then
        echo "OS = $OS"
        echo "DIST = $DIST"
        echo "DistroBasedOn = $DistroBasedOn"
        echo "PSUEDONAME = $PSUEDONAME"
        echo "REV = $REV"
        echo "KERNEL = $KERNEL"
        echo "MACH = $MACH"
        echo "SUB_REV = $SUB_REV"
fi

