#---------------------------------------------------------------------------
# Consistency checks
#---------------------------------------------------------------------------


# all the checks we do
check: localfiles check_dirs check_loop check_depends check_tools check_locale check_newt_so check_non_i18n $(arch_addl_checks)
	@echo "check successful"

# check some settings from config
check_dirs:
	@[ -d $(ftp_archive) ] || \
	  ( echo "ftp_archive variable from 'config' file ($(ftp_archive)) doesn't exist as directory"; exit 1)

# check whether your kernel has support for the msdos filesystem
check_msdos:
	@grep msdos /proc/filesystems 2>&1 >/dev/null || \
		test -f /lib/modules/`uname -r`/fs/msdos.o || \
		test -f /lib/modules/`uname -r`/kernel/fs/msdos/msdos.o || \
		( echo msdos filesystem support needed; exit 1 )

# Use this to check if your kernel has support for the loop device
check_loop:
	@grep loop /proc/devices 2>&1 >/dev/null || \
		test -f /lib/modules/`uname -r`/block/loop.o || \
		test -f /lib/modules/`uname -r`/kernel/drivers/block/loop.o || \
		( echo loop device support needed ; exit 1 )

# are our depends satisfied?  (e.g., for people using CVS)
check_depends:
	dpkg-checkbuilddeps
	@echo checking variable dependancies
	@perl -e \
	  '$$_="$(depends_checks)"; \
	   $$/="\n\n"; y/,/ /; s/\(.*?\)//g; @M{split()}=""; \
	   while (<>) { \
	     /^Status: \S+ \S+ installed$$/ms or next; \
	     delete $$M{$$1} if /^Package: (\S+)/ms; \
	     delete @M{split(/ *, */,$$1)} if /^Provides: (.+?)$$/ms; \
	   } \
	   foreach my $$m (keys %M) { \
	     print "  didn'\''t find $$m\n"; \
	   } \
	   if (%M) { \
	     print "To install missing packages: apt-get install ", \
               join (" ", keys %M), "\n"; \
	     exit 1; \
	   }' /var/lib/dpkg/status

check_tools:
ifneq ($(tools_checks), )
	@echo "checking stuff in $(tools_dir)"
	@for t in $(tools_checks); do \
	  [ -f $(tools_dir)/$$t ] || { echo "  didn't find $$t"; e=1; }; \
	done; exit $$e
endif

check_locale:
ifeq ($(USE_LANGUAGE_CHOOSER), true)
	@echo "checking for locale charset of en_IN UTF-8"
	@if ! ( LC_ALL=en_IN locale charmap ) | grep -q "^UTF-8"; then \
	  echo "add the line 'en_IN UTF-8' in /etc/locale.gen, and rerun locale-gen"; \
	  exit 1; \
        fi
endif

check_newt_so:
	@if [ -L /usr/lib/libnewt.so -a ! -e /usr/lib/libnewt.so ]; then \
	  echo "you have a dangling symlink in /usr/lib/libnewt.so, please make sure that's pointing somewhere valid"; \
	  exit 1; \
	fi
	@if [ ! -e /usr/lib/libnewt.so ]; then \
	  echo "you can't link dynamically with -lnewt, please create /usr/lib/libnewt.so as appropriate"; \
	  exit 1; \
	fi

check_non_i18n:
#	 if utf8 packages are installed and we're not doing an i18n build,
#	 that's bad
ifneq ($(USE_LANGUAGE_CHOOSER), true)
	@echo "checking for i18n packages which will mess up non-i18n build"
	@perl -e \
	  '%M = ("libnewt-utf8-0"=>1, "libnewt-utf8-pic"=>1,  \
		 "slang1a-utf8"=>1, "slang1-utf8-pic"=>1); \
	   $$/ = "\n\n"; \
	   @Q = (); \
	   while (<>) { \
	     /^Status: \S+ \S+ installed$$/ms or next; \
	     /^Package: (\S+)/ms && $$M{$$1} && push(@Q, $$1); \
	   } \
	   foreach my $$q (@Q) { \
	     print "  found $$q\n"; \
	   } \
	   if (@Q) { \
	     print "To remove these packages: apt-get remove ", \
               join (" ", @Q), "\n"; \
	     exit 1; \
	   }' /var/lib/dpkg/status
endif

#Local variables:
#mode: Makefile
#End:
