#!/bin/bash
set -e

pkg=yaegi

if [ "${AUTOPKGTEST_TMP}" = "" ] ; then
  AUTOPKGTEST_TMP=$(mktemp -d /tmp/${pkg}-test.XXXXXX)
  trap "rm -rf ${AUTOPKGTEST_TMP}" 0 INT QUIT ABRT PIPE TERM
fi

cd "${AUTOPKGTEST_TMP}"

echo "Test 1: Check that REPL works"
yaegi<<HERE > test1
import "fmt"
fmt.Println("Debian Autopkgtest")
fmt.Println(1 + 2)
fmt.Println(reflect.TypeOf(time.Date))
HERE

cat test1

result=$(md5sum test1 | awk '{print $1}')
if [ x"$result" != x"1a21e16a6eb2167ba79b50053af1e1e8" ]; then
	echo "FAIL: output file not as expected"
	exit 1
fi

# Clean up
rm -f test1
echo "============= PASS =============="

echo "Test 2: Check that yaegi script runs as expeted"
cat<<HERE > test2.sh
#!/usr/bin/env yaegi
package main

import "fmt"

func main() {
	fmt.Println("test")
}
HERE

chmod +x test2.sh
./test2.sh
if [ "$(./test2.sh)" != "test" ]; then
	echo "FAIL: unexpected output"
	exit 1
fi

# Clean up
rm -f test2.sh
echo "============= PASS =============="

echo "Test 3: Confirm that yaegi is able to run a go file"
cat<<HERE > test3.go
package main

import "fmt"

func main() {
        fmt.Println("testing yaegi run")
}
HERE

yaegi run test3.go

# Clean up
rm -f test3.go
echo "============= PASS =============="
