#!/bin/bash
#########################################################################
# (C) ZE CMS, Humboldt-Universitaet zu Berlin
# Written 2010-2014 by Daniel Rohde <d.rohde@cms.hu-berlin.de>
#########################################################################
# This program 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.
#
# This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
#########################################################################


MODULES='CGI DBI POSIX File::Temp Date::Parse UUID::Tiny XML::Simple Quota Archive::Zip IO::Compress::Gzip IO::Compress::Deflate Digest::MD5 Module::Load JSON File::Copy::Link DateTime DateTime::Format::Human::Duration URI::Escape IO::Compress::Brotli'

OPTIONALMODULES='DBD::SQLite DBD::mysql DBD::Pg MIME::Entity'
FSBACKENDMODULES='File::Spec::Link'
AFSBACKENDMODULES=$FSBACKENDMODULES
GFSBACKENDMODULES=$FSBACKENDMODULES
SMBBACKENDMODULES='Filesys::SmbClient'
RCSBACKENDMODULES='Rcs'
WEBINTERFACEMODULES='Graphics::Magick URI::Escape'


OPTIONALBINARIES='smbclient pagsh kinit aklog soffice'

CHECKEDMODULES=""
MISSINGMODULES=""
MISSINGBINARIES=""
mcheck() {
	for module in $@ ; do
		if ( echo $CHECKEDMODULES | grep -qw $module ) ; then
			echo "  $module already checked"
			continue
		fi
		out=$(perl -M$module -e 'print "installed"' 2>&1)
		if test $? -ne 0 ; then
			out='---- NOT INSTALLED ----'
			MISSINGMODULES="$MISSINGMODULES $module"
		fi
		CHECKEDMODULES="$CHECKEDMODULES $module"
		echo "  $module $out"
	done
}

bcheck() {
	for binary in $@ ; do
		out=$(which $binary)
		if test $? -ne 0 ; then
			out='--- NOT INSTALLED ----'
			MISSINGBINARIES="$MISSINGBINARIES $binary"
		fi
		echo "  $binary $out"
	done
}

echo "+++ Checking perl:"
bcheck perl

echo "++++ Checking required modules:"
mcheck $MODULES

echo "++++ Checking optional modules:"
mcheck $OPTIONALMODULES

echo "++++ Checking required modules for FS backend:"
mcheck $FSBACKENDMODULES

echo "++++ Checking required modules for AFS backend:"
mcheck $AFSBACKENDMODULES

echo "++++ Checking required modules for GFS backend:"
mcheck $GFSBACKENDMODULES

echo "++++ Checking required modules for SMB backend:"
mcheck $SMBBACKENDMODULES

echo "++++ Checking required modules for RCS backend:"
mcheck $RCSBACKENDMODULES

echo "++++ Checking optional binaries:"
bcheck $OPTIONALBINARIES



echo "#### Summary:"
if test "$MISSINGMODULES" != "" ; then
	echo "Missing modules: $MISSINGMODULES"
else
	echo "All modules found."
fi
if test "$MISSINGBINARIES" != "" ; then
	echo "Missing binaries: $MISSINGBINARIES"
else
	echo "All binaries found."
fi


