#!/bin/bash
# Check if every package from chapter 5 has been installed.
# No checks to see if linked to any host libs.

check_file () {
  echo -n " $1: "
  test ! -f $2 && echo "not installed, $2 does not exist" && return
  echo "OK"
}

check_symlink () {
  echo -n " $1: "
  ! [ -h $2 ] && echo "not installed $2 does not exist" && return
  file $1 | grep -q 'broken' && echo "mis-installed $2 is a broken symbolic link" && return
  echo "OK" && return
}

echo -n "Confirming that /tools is a symlink to /mnt/lfs/tools ... "
test -h /tools
if [ $? -ne 0 ]; then
  echo "/tools is not a symlink" ; exit 1
fi
MYLFS=$(readlink  -f /tools)
if [ "$MYLFS" = /mnt/lfs/tools ]; then
  echo "OK"
else
  # some people build in their own prefix
  echo "\n* unexpected result, it links to $MYLFS"
fi

echo -n "Confirming that /bin/sh is bash ... "
test -h /bin/sh
if [ $? -ne 0 ]; then
  echo "Weird,\n/bin/sh is not a symlink, not testing it"
else
  # based on part of version-check.sh
  MYSH=$(readlink -f /bin/sh)
  echo $MYSH | grep -q bash && echo "OK" ||
  echo "ERROR: /bin/sh does not point to bash, read section 5.3"
fi

echo
echo "Checking if all packages from chapter 5 have been installed"
# debug code
# test for missing file and missing symlink
#check_file nofile /tools/bin/fubar
#check_symlink nosym /tools/bin/fubar
# create badlink as symlink to the non-existent fubar
#check_symlink broken /tools/bin/badlink

# look for last non-doc file installed by the package
# but for the toolchain keep it simple (ignore variables)
check_file binutils-pass-1 /tools/bin/*-lfs-linux-gnu-ld.bfd
check_file gcc-pass-1 /tools/lib/gcc/*-lfs-linux-gnu/*/plugin/libcc1plugin.so
check_file linux-headers /tools/include/xen/..install.cmd
# on x86_64, stubs-64.h is later, but obviously not present on i686
check_file glibc /tools/include/gnu/stubs.h
check_file libstdc++ /tools/*-linux-gnu/include/c++/*/ext/opt_random.h
check_file binutils-pass-2 /tools/bin/ld-new
check_symlink gcc-pass-2 /tools/bin/cc
check_symlink tcl-core /tools/bin/tclsh
check_file expect /tools/bin/autoexpect
check_file dejagnu /tools/share/dejagnu/utils.exp
check_file check /tools/bin/checkmk
check_symlink ncurses /tools/lib/libncursesw.so
check_symlink bash /tools/bin/sh
check_symlink bzip2 /tools/bin/bzcmp
check_file coreutils /tools/libexec/coreutils/libstdbuf.so
check_file diffutils /tools/bin/sdiff
check_file file /tools/share/misc/magic.mgc
check_file findutils /tools/libexec/bigram
check_file gawk /tools/lib/gawk/time.so
check_file gettext /tools/bin/xgettext
check_file grep /tools/bin/fgrep
check_file gzip /tools/bin/uncompress
check_file m4 /tools/bin/m4
check_file make /tools/bin/make
check_file patch /tools/bin/patch
check_file perl /tools/lib/perl5/5.*/warnings.pm
check_file sed /tools/bin/sed
check_file tar /tools/bin/tar
check_file texinfo /tools/share/texinfo/texindex.awk
check_file util-linux /tools/bin/umount
check_symlink xz /tools/bin/lzcat

echo "I have not tested if anything is linked to the host system."
echo "If you think you might have built anything without the"
echo "correct PATH being set, please run ldd on example programs"
echo "from the packages which might have been miscompiled."
echo
echo "In chroot, you can look at the contents on each page if you"
echo "think you missed a package."

