1package App::Netdisco::Environment;
2
3use strict;
4use warnings;
5
6use File::ShareDir 'dist_dir';
7use Path::Class;
8use FindBin;
9
10BEGIN {
11  if (not ($ENV{DANCER_APPDIR} || '')
12      or not -f file($ENV{DANCER_APPDIR}, 'config.yml')) {
13
14      FindBin::again();
15      my $me = File::Spec->catfile($FindBin::RealBin, $FindBin::RealScript);
16      my $uid = (stat($me))[4] || 0;
17      my $home = ($ENV{NETDISCO_HOME} || (getpwuid($uid))[7] || $ENV{HOME});
18      $ENV{NETDISCO_HOME} ||= $home;
19
20      my $auto = dir(dist_dir('App-Netdisco'))->absolute;
21
22      $ENV{DANCER_APPDIR}  ||= $auto->stringify;
23      $ENV{DANCER_CONFDIR} ||= $auto->stringify;
24
25      my $test_envdir = dir($home, 'environments')->stringify;
26      $ENV{DANCER_ENVDIR} ||= (-d $test_envdir
27        ? $test_envdir : $auto->subdir('environments')->stringify);
28
29      $ENV{DANCER_ENVIRONMENT} ||= 'deployment';
30      $ENV{PLACK_ENV} ||= $ENV{DANCER_ENVIRONMENT};
31
32      $ENV{DANCER_PUBLIC} ||= $auto->subdir('public')->stringify;
33      $ENV{DANCER_VIEWS}  ||= $auto->subdir('views')->stringify;
34  }
35
36  {
37      # Dancer 1 uses the broken YAML.pm module
38      # This is a global sledgehammer - could just apply to Dancer::Config
39      use YAML;
40      use YAML::XS;
41      no warnings 'redefine';
42      *YAML::LoadFile = sub { goto \&YAML::XS::LoadFile };
43  }
44}
45
461;
47