#!/usr/bin/perl

use 5.005;
use strict;

use vars qw{$VERSION};
BEGIN {
	$VERSION = '0.92';
}

eval {
	require FindBin;
	require TinyAuth;
};
if ( $@ ) {
	error("Failed to load critical module dependency: $@");
}

# Create the configuration
my $config_file = File::Spec->catfile( $FindBin::Bin, 'tinyauth.conf' );
unless ( -f $config_file ) {
	error("Config file $config_file does not exist");
}
unless ( -f $config_file ) {
	error("No read permissions for config file $config_file");
}

my $config = YAML::Tiny->read( $config_file );
unless ( $config ) {
	error("Failed to load config file at $config_file");
}

# Create the web application
my $application = eval {
	TinyAuth->new(
		config => $config,
	)
};
unless ( $application ) {
	error("Failed to create TinyAuth instance: $@");
}

# Run the instance
my $rv = eval { $application->run };
unless ( $rv ) {
	error("Application instance failed to run: $@");
}

exit(0);

sub error {
	print "Content-type: text/plain\n\nError: $_[0]\n";
	exit(0);
}
