#!/bin/sh
# Collect informations from sourceable /etc/[distro]-release file:
# DISTRIB_ID, DISTRIB_RELEASE, DISTRIB_CODENAME, DISTRIB_DESCRIPTION
#
# Copyright (C) 2006-2007,2013 Davide Madrisan <davide.madrisan@gmail.com>

script_ver=2
script_name="$(basename $0 2>/dev/null)"

usage ()
{
    echo "\
$script_name v$script_ver, prints Distribution informations
Copyright (C) 2006-2007,2013 Davide Madrisan <davide.madrisan@gmail.com>

Usage: $script_name [OPTION]
With no OPTION specified it is the same as -a.

Options:
  -i, --id
    Display the string id of the distributor.
  -d, --description
    Display the single line text description of the distribution.
  -r, --release
    Display the release number of the distribution.
  -c, --codename
    Display the codename according to the distribution release.
  -a, --all
    Display all of the above information.
  -h, --help
    Display this message."
}

get_all=0
get_codename=0
get_description=0
get_id=0
get_release=0

[ -z "$@" ] && get_all=1

OPTS=`LANG=C getopt -o acdhirs \
   --long all,codename,description,help,id,release,short \
   -n "$script_name" -- "$@"`
[ $? = 0 ] || exit 1

eval set -- "$OPTS"

while :; do
   case "$1" in
   -a|--all)
      get_all=1
      get_codename=1
      get_description=1
      get_id=1
      get_release=1 ;;
   -c|--codename)
      get_codename=1 ;;
   -d|--description)
      get_description=1 ;;
   -h|--help)
      usage; exit 0 ;;
   -i|--id)
      get_id=1 ;;
   -r|--release)
      get_release=1 ;;
   -s|--short)
      short=0 ;;
   --) shift; break ;;
   *) echo "\
(bug) -- $script_name: \`getopt' error: bad command \`$1'" ;;
   esac
   shift
done

[ -r /etc/os-release ] || exit 1

. /etc/os-release

distro_id="$ID"
distro_description="$PRETTY_NAME"
distro_release="$VERSION_ID"
distro_codename="$OPENMAMBA_CODENAME"
[ "$distro_codename" ] || distro_codename="n/a"

unset res
if [ "$get_all" = 1 ]; then
   res="\
$distro_id \"$distro_description\" $distro_release $distro_codename"
else
   [ "$get_id" = 1 ] &&
      res="${res:+$res }$distro_id"
   [ "$get_description" = 1 ] &&
      res="${res:+$res }$distro_description"
   [ "$get_release" = 1 ] &&
      res="${res:+$res }$distro_release"
   [ "$get_codename" =  1 ] &&
      res="${res:+$res }$distro_codename"
fi

[ "$res" ] || exit 1
echo "$res"

exit 0
