#!/bin/sh
PATH=$PATH:/opt/lsb/bin
PERLBIN=/usr/bin/perl

perl_version() {
  perl_ver=`$PERLBIN -V:version | sed "s|version=||g;s|;||g;s|'||g"`
  real_perl_ver=$perl_ver
  numeric_ver=`echo $perl_ver | sed "s|\\.||g"`
  if [ $numeric_ver -ge 5100 ];then
    perl_ver=5.10.0
  else
    perl_ver=5.8.8
  fi
  fix_perl_symlink
}

fix_perl_symlink() {
  # packaged perl symlink in 5.10.0 points to /usr/bin/perl5.10.0
  # this needs to point to the real, versioned, perl binary
  cd $perl_ver
  rm -f perl
  ln -s $PERLBIN$real_perl_ver perl
  cd t
  rm -f perl
  ln -s $PERLBIN perl
  cd ../..
}

restore_perl_symlink() {
  cd $perl_ver
  rm -f perl
  ln -s $PERLBIN$perl_ver perl
  cd ..
}

save_journal() {
  last_dir=`find results -name '00*e' | sort -n | tail -1 | cut  -c11- | cut -c-2`
  next_dir=`expr $last_dir + 1`
  next_dir=`printf "%02d" $next_dir`
  next_dir="results/00$next_dir"e
  mkdir $next_dir
  mv $perl_ver/t/tet_xres $next_dir/journal
  echo "Journal file is $next_dir/journal"
}

unicode_symlink() {
  if [ ! -h $perl_ver/lib/unicore/lib ];then
    echo "Creating Unicode Character Database symlink..."
    cd $perl_ver/lib/unicore
    if [ -d "$installprivlib/unicore/lib" ];then
      ln -s "$installprivlib/unicore/lib" .
    else
      echo "Cannot find the Unicode Character Database"
      echo "Test uni/class.t will most likely fail"
      sleep 10
    fi
    cd ../../..
  fi
}

memoize_symlink() {
  cd $perl_ver/lib
  for pfile in blib.pm constant.pm strict.pm;do
    if [ ! -h "$pfile" ]; then
      echo "Creating symlink to system $pfile"
      if [ -f "$installprivlib/$pfile" ];then
        ln -s "$installprivlib/$pfile" .
      else
        echo "Cannot find $pfile in the installed perl"
        echo "Test lib/Tie/Memoize.t will most likely fail"
        sleep 10
      fi
    fi
  done
  cd ../..
}

pod_symlink() {
  cd $perl_ver/lib/Pod
  if [ ! -h "Functions.pm" ]; then
      echo "Creating symlink to system Functions.pm"
      if [ -f "$installprivlib/Pod/Functions.pm" ];then
        ln -s "$installprivlib/Pod/Functions.pm" .
      else
        echo "Cannot find Pod/Functions.pm in the installed perl"
        echo "Pod tests may fail"
        sleep 10
      fi
    fi
  cd ../../..
}

warnings_symlink() {
  cd $perl_ver/lib
  if [ ! -h "warnings.pm" ]; then
      echo "Creating symlink to system warnings.pm"
      if [ -f "$installprivlib/warnings.pm" ];then
        ln -s "$installprivlib/warnings.pm" .
      else
        echo "Cannot find warnings.pm in the installed perl"
        echo "A number of tests may fail"
        sleep 10
      fi
    fi
  cd ../..
}

# cleanup in case one switches back and forth from system perl to lsb-perl
clean_symlinks() {
  if [ -h "$perl_ver/lib/unicore/lib" ];then
    rm -f $perl_ver/lib/unicore/lib
  fi
  cd $perl_ver/lib
  for pfile in blib.pm constant.pm strict.pm warnings.pm;do
    if [ -h "$pfile" ];then
      rm -f "$pfile"
    fi
  done
  if [ -h "Pod/Functions.pm" ];then
    rm -f Pod/Functions.pm
  fi
  cd ../..
}

# for information only
perl_version
echo "System perl version is: $real_perl_ver"
# get $installprivlib
eval `$PERLBIN '-V:installprivlib'`
clean_symlinks
unicode_symlink
memoize_symlink
pod_symlink
warnings_symlink
cd $perl_ver/t
TET_ROOT=/opt/lsb-tet3-lite PERL_TEST_Net_Ping=1 PERL_SRC=../.. PERL=$PERLBIN $PERLBIN -I /opt/lsb/test/perl/$perl_ver/tlib harness
cd ../..
save_journal
restore_perl_symlink
