#!/bin/sh -e
#
# 2010-2011 Nico Schottelius (nico-cdist at schottelius.org)
# 2020-2021 Dennis Camera (dennis.camera at ssrq-sds-fds.ch)
#
# This file is part of cdist.
#
# cdist is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# cdist is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with cdist. If not, see <http://www.gnu.org/licenses/>.
#
# All os variables are lower case
#

rc_getvar() {
   awk -F= -v varname="$2" '
      function unquote(s) {
         if (s ~ /^".*"$/ || s ~ /^'\''.*'\''$/)
            return substr(s, 2, length(s) - 2)
         else
            return s
      }
      $1 == varname { print unquote(substr($0, index($0, "=") + 1)) }' "$1"
}

case $("${__explorer:?}/os")
in
   amazon)
      cat /etc/system-release
   ;;
   archlinux)
      # empty, but well...
      cat /etc/arch-release
   ;;
   checkpoint)
       awk '{version=$NF; printf("%s\n", substr(version, 2))}' /etc/cp-release
    ;;
   debian)
      debian_version=$(cat /etc/debian_version)
      case $debian_version
      in
          testing/unstable)
              # previous to Debian 4.0 testing/unstable was used
              # cf. https://metadata.ftp-master.debian.org/changelogs/main/b/base-files/base-files_11_changelog
              echo 3.99
              ;;
          */sid)
              # sid versions don't have a number, so we decode by codename:
              case $(expr "$debian_version" : '\([a-z]\{1,\}\)/')
              in
                  trixie) echo 12.99 ;;
                  bookworm) echo 11.99 ;;
                  bullseye) echo 10.99 ;;
                  buster) echo 9.99 ;;
                  stretch) echo 8.99 ;;
                  jessie) echo 7.99 ;;
                  wheezy) echo 6.99 ;;
                  squeeze) echo 5.99 ;;
                  lenny) echo 4.99 ;;
                  *) echo 99.99 ;;
              esac
              ;;
          *)
              echo "$debian_version"
              ;;
      esac
   ;;
   devuan)
      devuan_version=$(cat /etc/devuan_version)
      case ${devuan_version}
      in
         (*/ceres)
            # ceres versions don't have a number, so we decode by codename:
            case ${devuan_version}
            in
               (daedalus/ceres) echo 4.99 ;;
               (chimaera/ceres) echo 3.99 ;;
               (beowulf/ceres) echo 2.99 ;;
               (ascii/ceres) echo 1.99 ;;
               (*) exit 1
            esac
            ;;
         (*)
            echo "${devuan_version}"
            ;;
      esac
   ;;
   fedora)
      cat /etc/fedora-release
   ;;
   gentoo)
      cat /etc/gentoo-release
   ;;
   macosx)
      # NOTE: Legacy versions (< 10.3) do not support options
      sw_vers | awk -F ':[ \t]+' '$1 == "ProductVersion" { print $2 }'
   ;;
   freebsd)
      # Apparently uname -r is not a reliable way to get the patch level.
      # See: https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=251743
      if command -v freebsd-version >/dev/null 2>&1
      then
         # get userland version
         freebsd-version -u
      else
         # fallback to kernel release for FreeBSD < 10.0
         uname -r
      fi
   ;;
   *bsd|solaris)
      uname -r
   ;;
   openwrt)
      cat /etc/openwrt_version
   ;;
   owl)
      cat /etc/owl-release
   ;;
   redhat|centos|mitel|scientific)
      cat /etc/redhat-release
   ;;
   slackware)
      cat /etc/slackware-version
   ;;
   suse)
      if [ -f /etc/os-release ]; then
        cat /etc/os-release
      else
        cat /etc/SuSE-release
      fi
   ;;
   ubuntu)
      if command -v lsb_release >/dev/null 2>&1
      then
         lsb_release -sr
      elif test -r /usr/lib/os-release
      then
         # fallback to /usr/lib/os-release if lsb_release is not present (like
         # on minimized Ubuntu installations)
         rc_getvar /usr/lib/os-release VERSION_ID
      elif test -r /etc/lsb-release
      then
         # extract DISTRIB_RELEASE= variable from /etc/lsb-release on old
         # versions without /usr/lib/os-release.
         rc_getvar /etc/lsb-release DISTRIB_RELEASE
      fi
   ;;
   alpine)
       cat /etc/alpine-release
   ;;
esac
