#!/bin/bash


echo "en_US.UTF-8 UTF-8" >> /etc/locale.gen
locale-gen
export LANG=en_US.UTF-8

set -e

test_uri_parse(){
  echo "URI parse test"
  /usr/bin/drbl-uriparse smb://jack:loverose@titanic.com:1911/to/new-york domain|grep titanic.com
}

test_grub_menu(){
  echo "create client's grub menu test"
  mkdir -p /tftpboot/nbi_img/grub/
  /usr/sbin/gen-grub-efi-nb-menu
  cat /tftpboot/nbi_img/grub/grub.cfg|grep menu
}

test_pxe_menu(){
  echo "create client's pxe menu test"
  mkdir -p /tftpboot/nbi_img/pxelinux.cfg/
  /usr/sbin/generate-pxe-menu
  cat /tftpboot/nbi_img/pxelinux.cfg/default|grep menu
}

test_drblsrv_offline_package_scan(){
  echo "drblsrv-offline package scan test"
  /sbin/drblsrv-offline -r
}

test_httpboot_symlinks(){
  echo "Testing HTTPBoot symlink generation"
  # Create a mock web server root
  local mock_root="/tmp/mock_var_www_html"
  mkdir -p "$mock_root"
  mkdir -p /tftpboot/nbi_img/grub

  # Run the symlink creation block
  for docroot in "$mock_root"; do
    if [ -d "$docroot" ]; then
       if [ ! -e "$docroot"/tftpboot ] && [ ! -L "$docroot"/tftpboot ]; then
          ln -sf /tftpboot "$docroot"/tftpboot
       fi
       if [ ! -e "$docroot"/grub ] && [ ! -L "$docroot"/grub ]; then
          ln -sf /tftpboot/nbi_img/grub "$docroot"/grub
       fi
    fi
  done

  # Verify the symlinks were created correctly
  [ -L "$mock_root/tftpboot" ] || fail "tftpboot symlink was not created"
  [ -L "$mock_root/grub" ] || fail "grub symlink was not created"

  # Test skipping if destination already exists
  # Create a dummy file at grub and try again
  rm -f "$mock_root/grub"
  touch "$mock_root/grub"
  for docroot in "$mock_root"; do
    if [ -d "$docroot" ]; then
       if [ ! -e "$docroot"/tftpboot ] && [ ! -L "$docroot"/tftpboot ]; then
          ln -sf /tftpboot "$docroot"/tftpboot
       fi
       if [ ! -e "$docroot"/grub ] && [ ! -L "$docroot"/grub ]; then
          ln -sf /tftpboot/nbi_img/grub "$docroot"/grub
       fi
    fi
  done

  # grub should still be a normal file, not a symlink, since it was skipped
  [ -f "$mock_root/grub" ] && [ ! -L "$mock_root/grub" ] || fail "grub symlink was incorrectly created over existing file"

  # Clean up
  rm -rf "$mock_root"
}
. shunit2

