#!/bin/bash

# Check for prerequisite packages

# This file is part of DDD.
# 
# DDD 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.
# 
# DDD 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 DDD -- see the file COPYING.
# If not, see <http://www.gnu.org/licenses/>.
# 
# DDD is the data display debugger.
# For details, see the DDD World-Wide-Web page, 
# `http://www.gnu.org/software/ddd/',
# or send a mail to the DDD developers <ddd@gnu.org>.

# Usage:  prereq [-maint*]
#  -maint | -maintainer:  Check for pre-reqs to build from repo

MAINTAINER=0

usage()
{
  echo "Check for pre-requisite packages to build DDD"
  echo "Usage:  $0 [-maint | maintainer]"
  echo "  -maint | -maintainer:  Check for pre-reqs to build from repo"
}

for ARG in $*; do
  case "$ARG" in 
    -maint | -maintainer)
	MAINTAINER=1
	;;
    -h | -help | --help)
	usage
	exit 1   
	;;
    *)
	echo "error: unrecognized argument: $ARG"
	usage
	exit 1   
	;;
  esac
done 

DEBIANPKGS=( \
  "binutils" "g++" "gcc" "make" "libmotif-dev" "libx11-dev" "libxpm-dev" \
  "libxt-dev" "libxaw7-dev" "libxpm-dev" "libncurses-dev" "gdb" "fonts-liberation" \
  )
DEBIANDEVPKGS=( \
  "automake" "autoconf" "libtool" "bison" "flex" "texinfo" "netpbm" "fig2dev" \
  "imagemagick" "texlive-font-utils" "texlive-latex-base" \
  )

FEDORAPKGS=( \
  "binutils" "gcc-c++" "gcc" "make" "motif-devel" "libX11-devel" "libXpm-devel" \
  "libXaw-devel" "libXt-devel" "ncurses-devel" "gdb" "liberation-fonts-common" \
  "liberation-sans-fonts" "liberation-mono-fonts" "liberation-serif-fonts" \
  "netpbm-progs" \
  )
FEDORADEVPKGS=( \
  "automake" "autoconf" "libtool" "bison" "flex" "texinfo" "netpbm" "transfig" \
  "ImageMagick" "texinfo-tex" \
  )

NEEDED=""

if grep -E "Ubuntu|Raspbian" /etc/os-release > /dev/null; then
  DISTRO="DEBIAN"
  INSTALLED=$(dpkg -l | cut -d ' ' -f 3 | cut -d ':' -f 1)
  INSTALLED=" $INSTALLED "
elif grep Fedora /etc/redhat-release > /dev/null; then
  DISTRO="FEDORA"
else
  DISTRO="UNKNOWN"
fi

if [ "$DISTRO" = "UNKNOWN" ]; then
  echo "Cannot check pre-req's for $DISTRO distro"
  usage
  exit 1
fi

if [ "$DISTRO" = "DEBIAN" ]; then
  echo "Checking pre-req's for $DISTRO distro"
  echo ""

  if [ $MAINTAINER = 1 ]; then
    PKGS=( ${DEBIANPKGS[*]} ${DEBIANDEVPKGS[*]} )
  else
    PKGS=( ${DEBIANPKGS[*]} )
  fi
  
  for PKG in ${PKGS[@]}
  do 
    if echo $INSTALLED | grep " $PKG " &> /dev/null ; then
      echo "$PKG installed"
    else
      echo "$PKG not installed"
      NEEDED="$NEEDED $PKG"
    fi
  done

fi

if [ "$DISTRO" = "FEDORA" ]; then
  echo "Checking pre-req's for $distro distro"

  if [ $MAINTAINER = 1 ]; then
    PKGS=( ${FEDORAPKGS[*]} ${FEDORADEVPKGS[*]} )
  else
    PKGS=( ${FEDORAPKGS[*]} )
  fi
 
  for PKG in ${PKGS[@]}
  do 
    if dnf list installed $PKG &> /dev/null ; then
      echo "$PKG installed"
    else
      echo "$PKG not installed"
      NEEDED="$NEEDED $PKG"
    fi
  done

fi

echo " "
if [ "$NEEDED" == "" ] ; then
  echo "All prerequsite packages installed"
else 
  echo "The following packages need to be installed:"
  echo $NEEDED
fi
