#!/usr/bin/perl
use common::sense;
use Term::ANSIColor;
use POSIX qw(strftime);
use Sys::Hostname;
use Cwd;

my ($exitCode, $jobs) = @ARGV;

#Exit code or time
print color 'bold red';
if ($exitCode) {
	printf "%5d ", $exitCode;
} else {
	print strftime "%H:%M ", localtime;
}
# Hostname, etc
if ($ENV{USER} eq 'root') {
	print color 'bold red';
	print hostname, ' ';
} else {
	print color 'bold green';
	print $ENV{USER}, '@', hostname, ' ';
}

# The directory
my $dir = getcwd;
my $cdir = $dir;
my $origDir = $dir;
my $gdir;
my $dtype;
my $gitFound;
# Look if there's a parent git directory
while ($cdir) {
	for my $type (qw(git hg)) {
		if(-d "$cdir/.$type") {
			eval {
				$gdir = substr $dir, 1 + length $cdir;
				$dir = substr $dir, 0, 1 + length $cdir;
			};
			if ($@) {
				$gdir = "";
			}
			$dtype = $type;
			last;
		}
	}
	if ($dtype) {
		$gitFound = 1 if ($dtype eq 'git');
		last;
	}
	$cdir =~ s#/[^/]*$##;
}
# Some mangling
$dir =~ s/^$ENV{HOME}/~/;
$dir =~ s#^/home/(.*?)/#~$1/#;
$dir =~ s#^/home/(.*?)$#~$1#;
print color 'bold blue';
print $dir;
# And a git output, if there's git
print color 'bold magenta';
print $gdir;
if ($gitFound) {
	my ($bname, $status);
	open my $gitBranch, '-|', 'git', 'branch', '--no-color', '--no-abbrev', '-vv' or die "Could not call git branch :-( ($!)\n";
	while (<$gitBranch>) {
		next unless s/^\* //;
		($bname, $status) = /^(\S+)\s+[0-9a-f]+\s+(?:\[[^]]+?( [^]]+)?\])?/;
		$status =~ s/ahead (\d+), behind (\d+)/$1\/$2/;
		$status =~ s/ahead (\d+)/+$1/;
		$status =~ s/behind (\d+)/-$1/;
	}
	print " ($bname$status";
	if(open my $gitStatus, '-|', 'git', 'status', '-uall', '--porcelain') {
		my $cnt;
		my $untr;
		while (<$gitStatus>) {
			$untr ++, next if /^\?\?/;
			next if /^\!\!/;
			$cnt ++;
		}
		if ($cnt) {
			print color 'bold red';
			print " $cnt";
		}
		if ($untr) {
			print color 'bold blue';
			print " $untr";
		}
		print color 'bold magenta';
	}
	print ')';
} elsif ($dtype eq 'hg') {
	if (open my $hgBranch, '-|', 'hg', 'branch') {
		my $bname = <$hgBranch>;
		chomp $bname;
		print color 'bold magenta';
		print " ($bname)";
	}
}
if ($jobs =~ /\S/) {
	my $jobOutput;
	my $printOK;
	open my $jobFile, '>', \$jobOutput;
	print $jobFile color 'bold green';
	print $jobFile ' [';
	my $space;
	for (split /\n/, $jobs) {
		my ($pid, $status, $name) = /^\[.*?\].\s+(\d+)\s+(Stopped|Running)\s+(?:\(.*?\)\s+)?(\S+)/;
		if ($status eq 'Stopped') {
			print $jobFile color 'bold magenta';
		} else {
			print $jobFile color 'bold green';
		}
		print $jobFile "$space$name($pid)";
		$printOK = 1 if $pid > 0;
		$space = ' ';
	}
	print $jobFile ']';
	close $jobFile;
	print $jobOutput if $printOK;
}
# DF in the current directory
my $percent = '??';
my $color = 'red';
if (open my $df, '-|', 'df', $origDir) {
	<$df>; # Skip the header
	local $/;
	my $line = <$df>;
	if ($line =~ /(\d+)%/) {
		$percent = $1;
		if ($percent < 50) {
			$color = 'green';
		} elsif ($percent < 85) {
			$color = 'magenta';
		}
	}
}
print color $color;
print " $percent%";
if (exists $ENV{STY}) {
	print color 'black';
	print " ($ENV{STY})";
}
if (exists $ENV{SSH_CLIENT}) {
	print color 'black';
	print " ($ENV{SSH_CLIENT})";
}
print color 'bold blue';
#print "\n\$ ";
#print color 'reset';
