#!/bin/bash


#scriptlist="sophomorix-test-1 sophomorix-test-2 sophomorix-test-3 sophomorix-test-4 sophomorix-test-5 sophomorix-test-workflow"


case $1 in
   r|re|res|rese|reset)
      echo "### Resetting samba  for $test ... ###"
      net conf delshare linuxmuster-global
      net conf delshare abc
      net conf delshare bsz
      net conf delshare default-school
      net conf delshare ghs
      net conf delshare lin
      net conf delshare ltg
      net conf delshare test
      net conf delshare uni

      rm -f /etc/linuxmuster/sophomorix/bsz/bsz.devices.csv
      rm -f /etc/linuxmuster/sophomorix/bsz/bsz.teachers.csv
      rm -f /etc/linuxmuster/sophomorix/bsz/bsz.students.csv
      rm -f /etc/linuxmuster/sophomorix/uni/uni.teachers.csv
      rm -f /etc/linuxmuster/sophomorix/uni/uni.students.csv
      rm -f /etc/linuxmuster/sophomorix/default-school/teachers.csv
      rm -f /etc/linuxmuster/sophomorix/default-school/students.csv
      rm -f /etc/linuxmuster/sophomorix/default-school/devices.csv

      sophomorix-samba --restore-samba ohne-user --schema-load
      rm -rf /srv/samba
      sleep 5
      #./tools/add-sophomorix-admin
      sshpass -p "Muster!" samba-tool user setpassword Administrator
      scriptlist=""
      exit
      ;;
   1)
      scriptlist="sophomorix-test-1"
      ;;
   2)
      scriptlist="sophomorix-test-2"
      ;;
   3)
      scriptlist="sophomorix-test-3"
      ;;
   4)
      scriptlist="sophomorix-test-4"
      ;;
   5)
      scriptlist="sophomorix-test-5"
      ;;
   w|wo|wor|work|workf|workfl|workflo|workflow)
      scriptlist="sophomorix-test-workflow"
      ;;
   a|al|all)
      # all scripts
      scriptlist="sophomorix-test-1 sophomorix-test-2 sophomorix-test-3 sophomorix-test-4 sophomorix-test-5 sophomorix-test-workflow"
      ;;

   *)
      echo ""
      echo "Usage:"
      echo ""
      echo "  Run all tests:"
      echo "     regression-supertest all"
      echo ""
      echo "  Run a single test:"
      echo "     regression-supertest 1"
      echo "     regression-supertest 2"
      echo "     regression-supertest 3"
      echo "     regression-supertest 4"
      echo "     regression-supertest 5"
      echo "     regression-supertest workflow"
      echo ""
      echo "  Reset samba only:"
      echo "     regression-supertest reset"
      echo ""
      exit
      ;;
esac



# run the tests

for test in $scriptlist
do
    echo "### Resetting samba  for $test ... ###"
    net conf delshare linuxmuster-global
    net conf delshare abc
    net conf delshare bsz
    net conf delshare default-school
    net conf delshare ghs
    net conf delshare lin
    net conf delshare ltg
    net conf delshare test
    net conf delshare uni

    rm -f /etc/linuxmuster/sophomorix/bsz/bsz.devices.csv
    rm -f /etc/linuxmuster/sophomorix/bsz/bsz.teachers.csv
    rm -f /etc/linuxmuster/sophomorix/bsz/bsz.students.csv
    rm -f /etc/linuxmuster/sophomorix/uni/uni.teachers.csv
    rm -f /etc/linuxmuster/sophomorix/uni/uni.students.csv
    rm -f /etc/linuxmuster/sophomorix/default-school/teachers.csv
    rm -f /etc/linuxmuster/sophomorix/default-school/students.csv
    rm -f /etc/linuxmuster/sophomorix/default-school/devices.csv

    sophomorix-samba --restore-samba ohne-user --schema-load
    rm -rf /srv/samba
    sleep 5
    #./tools/add-sophomorix-admin
    sshpass -p "Muster!" samba-tool user setpassword Administrator
    #samba-tool user setpassword Administrator
    echo "######################################################################"
    echo "### Running $test ... ###"
    time $test --full 1> /tmp/$test.log 2> /tmp/$test.error
    echo "######################################################################"
done



# show summary of results
echo ""
echo ""
echo ""
echo "Test results follow:"
echo ""

for test in $scriptlist
do
    echo "######################################################################"
    echo "########## $test result:##############################"
    tail -n 3 /tmp/$test.log
    echo "########## $test errors:##############################"
    cat /tmp/$test.error | grep -v "Domain=" | grep -v "OS=" | grep -v "Server="
    echo "##### done with $test"
    echo ""
    echo ""
done
