#!/bin/bash
#
# Interative test of nightview package
#

set -u 

echo "--------------------------------------------------------------------------"
echo
echo "   Iteractive test of nightview package"
echo
echo "To start press [ENTER]"
read

echo "Part I. Basic functions"
echo 
echo "--------    Test No.1: Is a local server running? ------------- "
echo "This test run a client named 'night_power' with option 'connect'. The client"
echo "will be send string over an unix socket to the server. If socket exist, the server"
echo "is listening and it will be give an ansfer."
echo "[RETURN]"
read

printf "Connecting to nightview server..\n"
night_power on
if [ $? -eq 0 ]; then
    printf ". Successful connection\n"
else
    printf "..Failed. \n"
    echo "Possible problems:"
    echo " * The nightview daemon don't running"
    echo " * If you try connection over internet, the http server must run"
    echo " * If binaries are not instaled, set PATH to its directory"
    echo " * Try set variable by 'export NIGHTVIEW_HOST=x.y.z', for network."
    exit 1
fi

echo
echo "End of test 1. Server is up."
echo
echo
echo "-------------- Test No. 2: Conecting to camera. -----------------"
echo ">>>>Switch on your camera electric power before this test. << "
echo "This test run 'night_power' with 'on' parameter, it will be try connect"
echo "to CCD camera."
echo "[RETURN]"
read

printf "Swithing camera on.."
night_power on
if [ $? -eq 0 ]; then
    echo " ..successful weak up."
    echo "The camera's fan is on, the LED blinks very slowly"
else
    echo "Failed..."
    echo "You have probably problem with a cable or parallel port"
    exit 1
fi

echo 
echo "End of test 2. Camera connected."
echo
echo
echo "--------------- Test No. 3 Camera info. --------------------------"
echo "It will be run 'night_power' with 'info' option. Some basic info about"
echo "your CCD will be printed. Check visually this values."
echo "[RETURN]"
read

printf "Downloading basic info about your camera.."
printf "Basic info about your camera:"
night_power info
if [ $? -eq 0 ]; then
    printf "OK"
else
    echo "Failed."
    echo "Possible problems:"
    echo " * Unknowns."
    exit 1
fi

echo
echo "End of test 3. Info written."
echo
echo
echo "---------------- Test No. 4. Shutdown of the camera -----------------"
echo "This will be run 'night_power' with 'off'. The camera will be shut downed."
echo "[RETURN]"
read

printf "Shut downing CCD camera.."
night_power off
if [ $? -eq 0 ]; then
    echo "successfully."
    echo "The fan and LED are off."
else
    echo "Failed."
    echo "Possible problems:"
    echo "  * Unknown"
    exit 1
fi

echo
echo "End of test 4. The camera is down."
echo "[RETURN]"
read

echo
echo "Now, we switch the camera on again. Test purposes."
night_power on

echo
echo
echo
echo
echo "Part II. Temperature functions"
echo 
echo "-------------- Test No. 5: Getting temperature. ---------------------"
echo "This test will be run the 'night_temperature' utility with option 'get'."
echo "The current values of ccd and air temperature will be printed. The fan status"
echo "and the cooling power will be indicated too."
echo "[RETURN]"
read

printf "Downloading temperature status..."
night_temperature get
if [ $? -eq 0 ]; then
    echo "successfully."
    echo "Nice temperature."
else
    echo "Failed."
    echo "Possible problems:"
    echo "  * Unknown"
    exit 1
fi

echo
echo "End of test 5. Temperature was get."

echo
echo
echo "------------- Test No. 6 Temperature setting. -------------------"
echo "It will be run 'night_temperature' with options 'set -t 9 -f'. This will"
echo "be set the camera temperature 9 degrees under zero and switch the fan on"
echo "[RETURN]"
read

printf "Setting camera temperature..."
night_temperature set -t 9 -f
if [ $? -eq 0 ]; then
    echo "successfully."
    echo "Nice temperature."
else
    echo "Failed."
    echo "Possible problems:"
    echo "  * Unknown"
    exit 1
fi

echo
echo "Now, we are runnig 6 periods each 10 seconds. The temperature ccd and cooling"
echo "power will be printed."
echo "[RETURN]"
read
i=0;
while [ $i -le 6 ]; do
    night_temperature get
    sleep 10
    i=$(($i+1))
done
echo
echo "Nice? Oook. We switch temperature regulation off. See the values"
echo "[RETURN]"
read
night_temperature set -off
i=0;
while [ $i -le 6 ]; do
    night_temperature get
    sleep 10
    i=$(($i+1))
done

echo
echo "End of test 6. The temperature regulations works correctly"
echo
echo
echo "------------- Test No. 007 What do James Bond drinks ? --------------"
echo "Select item:"
select drink in "Beer" \
                "Wine" \
                "Soda" \
                "Cola-cola" \
                "Pepsi-cola" \
                "Tea" \
                "Martini" \
                "dry Martini" \
                "I don't know" ; do
if [ "$drink" = "dry Martini" ]; then
    echo "Yes, of course.! You are expert! Congratulation."
else
    echo "It's too difficult for you. Please, ask your father for this"
fi
break
done

echo
echo "End of test 7. James Bond is agent 007."
echo

echo
echo "Part III. Exposure functions"
echo 
echo "------------ Test No. 8 Simple exposure ---------------------------"
echo "This will be start 'night_exposure' with option '-t 10' meaning 10 second"
echo "exposure. The image will be saved to 'nightview.fits' image."
echo "[RETURN]"
read

printf "Starting exposure..."
night_exposure -t 10
if [ $? -eq 0 ]; then
    echo "successfully."
    echo "Nice image."
else
    echo "Failed."
    echo "Possible problems:"
    echo "  * Unknown"
    exit 1
fi

echo 
echo "End of test 8. Image exposed."

echo
echo
echo "Part IV. Filter functions"
echo 
echo "------------ Test No. 9 Filter setting  ---------------------------"
echo "This will be start 'night_filter' with option '-f V'. The filter will be set"
echo "to V".
echo "[RETURN]"
read

printf "Selecting filter ..."
night_filter -f V
if [ $? -eq 0 ]; then
    echo "successfully."
    echo "Nice filter."
else
    echo "Failed."
    echo "Possible problems:"
    echo "  * Unknown"
    exit 1
fi

echo 
echo "End of test 9. V filter selected."


echo
echo
echo "END of tests. Your camera is prepared to observation."
echo
echo "To iteractive snapshoting use gtknightview. The batch processing of image"
echo "series is provided by the night_control utility."
echo
echo "Clear night."
