#!/scratchbox/tools/bin/perl -w

=head1 NAME

dh_installxsession - install a xsession snippet
call as 

dh_installxsession -u 'post 30'

or

dh_installxsession -u 'pre 20'

Where pre/post define where it get's started

=cut

use strict;
use Debian::Debhelper::Dh_Lib;

=head1 SYNOPSIS

instances of the same text to be added to maintainer scripts.

=cut

init();

foreach my $package (@{$dh{DOPACKAGES}}) {
	my $tmp=tmpdir($package);

	# Figure out what filename to install it as.
	my $script;
	if (defined $dh{NAME}) {
		$script=$dh{NAME};
	}
	elsif ($dh{INIT_SCRIPT}) {
		$script=$dh{INIT_SCRIPT};
	}
	else {
		$script=$package;
	}       
	
	my $init=pkgfile($package,$script) || pkgfile($package,"xsession");

	# This is set by the -u "foo" command line switch, it's
	# the parameters to set where to put the xsession script
	# invent something if not set
	my $params='';
	if (defined($dh{U_PARAMS})) {
		$params=join(' ',@{$dh{U_PARAMS}});
	}
	if ($params eq '') {
		 $params="post 50";
	}
	my $priority='';
	my $start='';
	
	($start, $priority) = split(/ /, $params, 2);

	if ($init ne '') {
		if ($start eq 'post'){
			if (! -d "$tmp/etc/X11/replace/Xsession.post") {
				doit("install","-d","$tmp/etc/X11/replace/Xsession.post");
			}
		
			doit("install","-p","-m755",$init,"$tmp/etc/X11/replace/Xsession.post/$script");
		} else {
			if (! -d "$tmp/etc/X11/replace/Xsession.d") {
				doit("install","-d","$tmp/etc/X11/replace/Xsession.d");
			}
		
			doit("install","-p","-m755",$init,"$tmp/etc/X11/replace/Xsession.d/$script");
		}
		
		if (! $dh{NOSCRIPTS}) {
			autoscript($package,"postinst","postinst-xsession",
				"s/#SCRIPT#/$script/g;s/#START#/$start/g;s/#PRIORITY#/$priority/g");
		}
	}
}

=head1 SEE ALSO

L<debhelper(7)>

This program is a part of debhelper.

=head1 AUTHOR

Joey Hess <ext-riku.voipio@nokia.com>

=cut
