#!/bin/sh
# distro.sh
# Summapack proyect
# Diego Saravia, copyrighted under more recent GPL, or any free licence with copyleft 
# with help of a lot of people
#https://en.wikipedia.org/wiki/Uname

# not really distro, but system-distro combination, what I need to see how to install software, and discriminate ways of that

#call as "sh distro.sh" or as "sh distro.sh +" for more information


if test x"$1" = x--help ; then
    echo AYUDA
    exit 0
    fi

if test x"$1" = x--version ; then
    echo distro.sh version 3
    exit 0
    fi




DISTROUU=`uname -a |sed -e's/\n//'`
DISTROU=`echo $DISTROUU | tr '[A-Z]' '[a-z]'` ;

DISTROGL=`echo $DISTROU  | grep gnu/linux`
#UNAMEP=`uname |sed -e's/\n//' | tr '[A-Z]' '[a-z]' `




#DISTROA=`uname -a |sed -e's/\n//'`

if test "x${DISTROGL}" = x ; then 
        case "x${DISTROU}" in \
           x*cygwin*)      DISTRO=CYGWIN       ;;
           x*mingw*)       DISTRO=MINGW        ;;
           x*msys*)        DISTRO=MSYS         ;;
           x*freebsd* )    DISTRO=FREEBSD      ;;
           x*solaris* )    DISTRO=SUNOS        ;;
           x*sunos* )      DISTRO=SUNOS        ;;
           x*openbsd* )    DISTRO=OPENBSD      ;;
           x*netbsd* )     DISTRO=NETBSD       ;;
	   x*mirbsd* )     DISTRO=MIRBSD       ;;
   	   x*midnightbsd* ) DISTRO=MIRBSD      ;;
      	   x*libertybsd* ) DISTRO=LIBERTYBSD   ;;
           x*bigtrig* )    DISTRO=BIGTRIG      ;;
           x*darwin* )     DISTRO=DARWINBSD    ;;
           x*dragonfly* )  DISTRO=DRAGONFLYBSD ;;
           x*kfreebsd* )   DISTRO=DEBIANBSD    ;;
           x*bsd* )        DISTRO=OTHERBSD     ;;
           x*minix* )      DISTRO=MINIX        ;;
           x*aix* )        DISTRO=AIX          ;;
           x*mac* )        DISTRO=MACOSX       ;;
           x*linux* )      DISTRO=LINUX        ;;
           x*haiku* )      DISTRO=HAIKU        ;;
           x*sn5176* )     DISTRO=CRAY         ;;
           x*ms-dos* )     DISTRO=DJGPP        ;;
           x*nonstop* )    DISTRO=NONSTOP      ;;
           x*qnx* )        DISTRO=QNX          ;;
           x*reliant* )    DISTRO=RELIANT      ;;
           x*ultrix* )     DISTRO=ULTRIX       ;;
           x*unixware* )   DISTRO=UNIXWARE     ;;
           x*sco* )        DISTRO=SCO          ;;
           x*irix* )       DISTRO=IRIX         ;;
           x*os/390* )     DISTRO=OS390        ;;
           x*os400* )      DISTRO=OS400        ;;
           x*hurd* )       DISTRO=GNUHURD      ;;
           x*gnu* )        DISTRO=GNUHURD      ;;
           x*mach* )       DISTRO=MACH         ;;	   
           x* )            DISTRO=NONGNULINUX  ;;
        esac
        if test "x${DISTRO}" = xGNUHURD ; then
            HURD=`cat /etc/os-release|grep 'NAME="Debian GNU/Hurd"' |sed -e's/\n//g'` ;
            if test "x${HURD}" != x ; then
               DISTRO=DEBIANGNUHURD
               fi
	    fi
        if test "x${DISTRO}" = xSUNOS ; then
	    if x${DISTROU} = xsolaris ; then
		ISSOLARIS=solaris
	    else
		ISSOLARIS=notsolaris
	    fi
	    if x${DISTROU} = x86 ; then
                MACHINE=x86
	    elif x${DISTROU} = xsparc ; then
                MACHINE=xsparc
	    else
	        MACHINE=unknownmachine	     
	        fi	
	    for f in dilos  hipster  omnios openindiana tribblix xstreamos illumian illumos ; do	    
                     SISUNFLAVOUR=`cat /etc/release | grep $f` 
	             if test "x${SISUNFLAVOR}" != x ; then
			 SUNFLAVOR=$f
			 fi
                     done
	     if test "x${UNFLAVOR}" = xillumian ; then
		     SUNFLAVOR=ILLUMIANGNUDEBIAN
	             fi
	     if test -e /etc/lsb-release ; then
                 DISTROLSB=`cat /etc/lsb-release|grep _ID=|sed -e's/DISTRIB_ID=//'| tr '[a-z]' '[A-Z]'`
                 REV=`cat /etc/lsb-release|grep DISTRIB_RELEASE=|sed -e's/DISTRIB_RELEASE=//'| tr '[a-z]' '[A-Z]'`
                 PSEUDONAME=`cat /etc/lsb-release|grep DISTRIB_DESCRIPTION=|sed -e's/DISTRIB_DESCRIPTION=//'| sed -e's/"//g'`
                 PSEUDONAME="${PSEUDONAME} `cat /etc/lsb-release|grep DISTRIB_CODENAME=|sed -e's/DISTRIB_CODENAME=//'| tr '[a-z]' '[A-Z]'`"
		 SUNFLAVOR=${SUNFLAVOUR}${DISTROLSB}
                 fi
	    #borel dyson illumian olympus sol11 sunlit
	    # illumian is a GNU system!!!
	    # open?
	    # oracle?
	    DISTRO=${SUNOS}${ISSOLARIS}${MACHINE}${SUNFLAVOR}		 
            fi
        


   else  
          case x${DISTROU} in \
           x*arch* )            DISTRO=ARCH            ;;
           x*microsoft* )       DISTRO=WINDOWS10UBUNTU ;;
           x*void* )            DISTRO=VOID            ;;
           x*bodhi* )           DISTRO=BODHI           ;;  #ubuntu
           x*solus* )           DISTRO=SOLUS           ;;
	   x*pclinuxos* )       DISTRO=PCLINUXOS       ;;  #(is a madriva derivative)
   	   x*manjaro* )         DISTRO=MANJARO         ;;  #(is a madriva derivative)
   	   x*opensuse* )        DISTRO=OPENSUSE        ;;
   	   x*gobolinux* )       DISTRO=GOBOLINUX       ;;  	   
           x* )                 DISTRO=OTHERGNULINUX   ;;
	   
           esac 
          SUSEOCS=`head -n1 /etc/profile|grep SUSE`
          if test "x${DISTRO}" = "xOTHERGNULINUX" ; then
	  if test -e /etc/issue; then
              UTU=`cat /etc/issue|grep ututo |sed -e's/\n//g'` ;
	      fi
          if test "x${DISTRO3}" != "x"; then
               DISTRO=WINDOWS10UBUNTU;
          elif test "x${DISTRO2}" != "x"; then
               DISTRO=ARCH;
          elif test -e /etc/SuSE-release; then 
               DISTRO=SUSE;
               PSEUDONAME=`cat /etc/SUSE-release | tr "\n" ' '| sed s/VERSION.*//`
               REV=`cat /etc/SUSE-release | tr "\n" ' ' | sed s/.*=\ //`
          elif test -e /etc/mandriva-release; then 
               DISTRO=MANDRIVA;
          elif test -e /etc/mandrake-release; then 
               DISTRO=MANDRAKE;
               PSEUDONAME=`cat /etc/mandrake-release | sed s/.*\(// | sed s/\)//`
               REV=`cat /etc/mandrake-release | sed s/.*release\ // | sed s/\ .*//`
          elif test -e /etc/redhat-release; then 
               DISTRO=REDHAT;
               PSEUDONAME=`cat /etc/redhat-release | sed s/.*\(// | sed s/\)//`
               REV=`cat /etc/redhat-release | sed s/.*release\ // | sed s/\ .*//`
          elif test -e /etc/fedora-release; then 
               DISTRO=FEDORA;
          elif test -e /etc/debian_version; then
               DISTRO=DEBIAN;
               PSEUDONAME="DEBIAN `cat /etc/debian_version`"
               REV=""
               if test -e /etc/issue ; then 
                 UBU=`grep 'Ubuntu' </etc/issue |sed -e's/\n//g'  `;
                 if  test x"${UBU}" != x ; then 
                   DISTRO=UBUNTU;
                   fi; 
                 fi; \
          elif test -e /etc/yellowdog-release; then
               DISTRO=YELLOWDOG;
          elif test -e /etc/ututo-release ; then 
               DISTRO=UTUTO;
          elif test -e /ututo.lastversion ; then 
               DISTRO=UTUTO;
          elif test x${UTU} != x ; then 
               DISTRO=UTUTO;
          elif test -e /etc/gentoo-release; then 
               DISTRO=GENTOO;
          elif test -e /etc/antiX; then 
               DISTRO=ANTIX;
          elif test -e /etc/alpine-release; then 
               DISTRO=ALPINE;
          elif test -e /etc/slackware-version; then 
               DISTRO=SLACKWARE;
               if test -e /etc/slax-version; then 
                  DISTRO=Slax;
               fi;
          elif test x"SUSEOCS" != X ; then
	      DISTRO=SUSE;
               fi;
	  
          if test -f /etc/UnitedLinux-release  ; then
               PSEUDONAME="${PSEUDONAME} `cat /etc/UnitedLinux-release | tr "\n" '' | sed s/VERSION.*//`"
               fi;
          fi;
     fi;

if test "x${DISTRO}" = 'DARWINBSD' ; then
     if test -f /System/Library/CoreServices/SystemVersion.plist ; then
         DISTRO=`grep -A1 -E '(ProductName|ProductVersion)' /System/Library/CoreServices/SystemVersion.plist  | grep '<string>' | sed -E 's/<[\/]?string>//g' `
         DISTRO=`echo $distro`
     fi
     if test -z $DISTRO ; then
         # SUPUESTAMENTE YA ESTABA DE ANTES
         DISTRO='MACOSX'
        fi
     fi


if test -z $DISTRO ; then
      if test -e /etc/lsb-release ; then
          DISTRO=`cat /etc/lsb-release|grep _ID=|sed -e's/DISTRIB_ID=//'| tr '[a-z]' '[A-Z]'`
          REV=`cat /etc/lsb-release|grep DISTRIB_RELEASE=|sed -e's/DISTRIB_RELEASE=//'| tr '[a-z]' '[A-Z]'`
          PSEUDONAME=`cat /etc/lsb-release|grep DISTRIB_DESCRIPTION=|sed -e's/DISTRIB_DESCRIPTION=//'| sed -e's/"//g'`
          PSEUDONAME="${PSEUDONAME} `cat /etc/lsb-release|grep DISTRIB_CODENAME=|sed -e's/DISTRIB_CODENAME=//'| tr '[a-z]' '[A-Z]'`"
          fi
      fi

if test -z $DISTRO ; then
    DISTRO=UNKNOWN
    PSEUDONAME=`echo $DISTROUU | sed -e's/ /_/g'| sed -e's/\//_/g'`      
    fi

if test "x$1" != "x"; then
    SDISTRO=`echo $DISTRO $PSEUDONAME $REV | sed -e's/ /_/g'| sed -e's/\//_/g'| sed -e's/#/_/g'| sed -e's/:/_/g'| sed -e's/\./_/g'`
    echo $SDISTRO
    # agregar si falla. $DISTROU
else
    echo $DISTRO
    fi



