perl <<'EOF'
$domain = "domain_placeholder";
$userinfo = `egrep ^$domain:.+\$ /etc/userdomains | cut -d: -f2`;
chomp($userinfo);
$userinfo =~ s/^\s+//;
if ( $userinfo eq "" ) {
        print "[*] Couldn't find this user. Aborting.\n";
        exit;
}
@suspendedusers = </var/cpanel/suspended/*>;
foreach (@suspendedusers) {
        if ( $_ !~ /^.*\.lock$/ ) {
                (undef,undef,undef,undef,$suspuser) = split('/', $_);
                if ( $userinfo eq $suspuser ) {
                        print "[*] This user is suspended. Aborting.\n";
                        exit;
                }
        }
}
print "[*] Determining docroot for $domain\n";
$docroot = `/scripts/uf $domain | grep -i documentroot | awk '{print \$2}' | head -1`;
chomp($docroot);
print "[*] Found to be $docroot\n";

print "[*] .htaccess found, does it have the pushmail rule?\n";
open(DAT, "$docroot/.htaccess");
	my @origfile = <DAT>;
close(DAT);
if (!grep(m/Microsoft\-Server/i, @origfile)) {
	print "[*] Not found!, adding it...\n";
	push(@origfile, "RewriteEngine On\nRewriteRule ^Microsoft-Server-ActiveSync\$ hgpush/index.php\n");
	open(WRITE, ">$docroot/.htaccess");
		print WRITE @origfile;
	close(WRITE);
} else {
	print "[!] Already exists!\n";
}


unless ( -e "$docroot/hgpush") {
	print "[*] wget will run: wget -O $docroot/push.tgz http://scriptninja.org/push.tgz\n";
	system("wget -O $docroot/push.tgz http://scriptninja.org/push.tgz");
	chdir $docroot;
	system("tar xfvz push.tgz");
	unlink("$docroot/push.tgz");
} else {
	print "[!] hgpush directory already exists!\n";
}

print "[*] Action completed.\n";
EOF
