#!/bin/sh

set -e

cat > /etc/apache2/conf-enabled/testfcgid.conf <<EOT
Alias /testfcgid /var/www/testfcgid
<Location /testfcgid>
  Options +ExecCGI
  AddHandler fcgid-script .pl
</Location>
EOT

mkdir /var/www/testfcgid
cat > /var/www/testfcgid/app.pl <<EOT
#!/usr/bin/perl
use CGI::Fast;

while (my \$q = CGI::Fast->new) {
  print("Content-Type: text/plain\n\n");
  print("hello world\n");
}
EOT
chown -R www-data:www-data /var/www/testfcgid
chmod +x /var/www/testfcgid/app.pl

a2enmod fcgid
APACHE_STARTED_BY_SYSTEMD=yes apache2ctl restart

if ! output=`wget -O- http://localhost/testfcgid/app.pl 2>/dev/null`; then
  echo "wget failed, output was:"
  echo "$output"
  exit 1
else
  if [ "$output" != "hello world" ]; then
    echo "output is wrong"
    echo "got:      | $output |"
    echo "expected: | hello world |"
    exit 1
  fi
fi
