#!/bin/bash

DISTNAME=unknown
DISTVER=

resetDistVer() {
  DISTNAME=unknown
  DISTVER=
}

if [ -f /etc/centos-release ]; then
  . /etc/os-release
  if [ "$VERSION_ID" = "7" ]; then
    DISTNAME=centos
    DISTVER=7
  elif [ "$VERSION_ID" = "8" ]; then
    DISTNAME=debian
    DISTVER=10
  else
    resetDistVer
  fi
elif [ -f /etc/fedora-release ]; then
  . /etc/os-release
  if [ "$VERSION_ID" != "32" ] && [ "$VERSION_ID" != "31" ] && [ "$VERSION_ID" != "30" ] && [ "$VERSION_ID" != "29" ] && [ "$VERSION_ID" != "28" ]; then
    resetDistVer
  else
    DISTNAME=debian
    if [ "$VERSION_ID" = "28" ]; then
      DISTVER=9
    else
      DISTVER=10
    fi
  fi
elif [ -f /etc/solus-release ]; then
  DISTNAME=solus
elif [ -f /etc/lsb-release ]; then
  . /etc/lsb-release
  DISTNAME=$(echo ${DISTRIB_ID} | awk '{print tolower($0)}')
  DISTVER=${DISTRIB_RELEASE}
  if [ "$DISTNAME" != "ubuntu" ]; then
    if [ "$DISTNAME" = "linuxmint" ]; then
      if [ "$DISTVER" = "19" ] || [ "$DISTVER" = "19.1" ] || [ "$DISTVER" = "19.2" ] || [ "$DISTVER" = "19.3" ]; then
        DISTNAME=debian
        DISTVER=9
      else
        resetDistVer
      fi
    elif [ "$DISTNAME" = "elementary" ]; then
      if [ "$DISTVER" = "5.0" ] || [ "$DISTVER" = "5.1" ]; then
        DISTNAME=debian
        DISTVER=9
      else
        resetDistVer
      fi
    else
      resetDistVer
    fi
  elif [ "$DISTVER" != "18.04" ] && [ "$DISTVER" != "18.10" ] && [ "$DISTVER" != "19.04" ] && [ "$DISTVER" != "19.10" ] && [ "$DISTVER" != "20.04" ]; then
    resetDistVer
  else
    DISTNAME=debian
    if [ "$DISTVER" = "18.04" ]; then
      DISTVER=9
    else
      DISTVER=10
    fi
  fi
fi

if [ "$DISTNAME" = "unknown" ] &&  [ -f /etc/debian_version ]; then
  DISTNAME=debian
  DISTVER=$(head -1 /etc/debian_version|awk -F. '{print $1}')
  if [ "$DISTVER" != "9" ] && [ "$DISTVER" != "10" ]; then
    resetDistVer
  fi
fi

if [ "$DISTNAME" = "unknown" ]; then
  if [ -f /etc/os-release ]; then
    . /etc/os-release
    if [ "$ID" = "arch" ]; then
      DISTNAME=debian
      DISTVER=10
    elif [ "$ID" = "antergos" ] || [ "$ID" = "manjaro" ]; then
      DISTNAME=debian
      DISTVER=9
    elif [ "$ID" = "opensuse-leap" ]; then
      if [ "$VERSION_ID" = "15.0" ]; then
        DISTNAME=debian
        DISTVER=9
      elif [ "$VERSION_ID" = "15.1" ]; then
        DISTNAME=debian
        DISTVER=9
      elif [ "$VERSION_ID" = "15.2" ]; then
        DISTNAME=debian
        DISTVER=9
      else
        resetDistVer
      fi
    elif [ "$ID" = "opensuse-tumbleweed" ]; then
      DISTNAME=debian
      DISTVER=10
    else
      resetDistVer
    fi
  else
    resetDistVer
  fi
fi

echo ${DISTNAME}${DISTVER}
