#! /usr/local/bin/perl -w

#
#       This is not fully tested. It worked for my display
#       which started from xdm on suna.
######################################################
#       First look at your own processes

$me = `whoami`;
$me =~ s/\n//;
$home = $ENV{HOME};
$xroot = "/usr";
@lines = `ps -ef | grep $me`;   # ps -aux should also work
#
#       find the window manager
#
($wm) = grep (m/[fv|t|m]wm/, @lines);
$wm =~ m/(\d+).+?([fv|t|m|ol]wm)/;
$wm = $2;
print "User: $me, DISPLAY=$ENV{DISPLAY}, Window Manager=$wm (pid=$1)\n";
#
#       how did we start the xsession? - startx, xinit, X, xdm?
#
if (grep(/(\d+).+startx/, @lines)       ){ $how="startx";}
elsif (grep(/(\d+).+xinit/, @lines)     ){ $how="xinit";}
elsif (grep(/(\d+).+xdm/, @lines)       ){ $how="xdm";}
elsif (grep(/(\d+).+X/, @lines)         ){ $how="X";}
else                                    { $how ="unknown";}

print "X Session was started by: $how (pid=$1) on ".`hostname`."\n";

if ($how eq "startx") {
        if ( -f "$home/.xinitrc" ) {
                $xinitrc = `ls -l $home/.xinitrc`;
                print "Starting X by startx, using $home/.xinitrc\n",
                        " $xinitrc\n";
        }
        elsif ( -f "$xroot/lib/X11/xinit/xinitrc") {
                $xinitrc = `ls -l $xroot/lib/X11/xinit/xinitrc`;
                print "Starting X by startx, using $xroot/lib/X11/xinit/xinitrc\n",
                        " $xinitrc\n";
        }
        else {
                print "No xinitrc files for starting X by startx\n";
        }
        if ( -f "$home/.xserverrc" ) {
                $xserverrc = `ls -l $home/.xserverrc`;
                print "Starting X by startx, using $home/.xserverrc\n",
                        " $xserverrc\n";
        }
        elsif ( -f "$xroot/lib/X11/xinit/xserverrc") {
                $xserverrc = `ls -l $xroot/lib/X11/xinit/xserverrc`;
                print "Starting X by startx, using $xroot/lib/X11/xinit/xserverrc\n",
                        " $xserverrc\n";
        }
        else {
                print "No xserverrc files for startx - use X\n";
        }
}
elsif ($how eq "xinit") {
        if ( -f "$home/.xinitrc" ) {
                $xinitrc = `ls -l $home/.xinitrc`;
                print "Starting X by xinit, using $home/.xinitrc\n",
                        " $xinitrc\n";
        }
        else {
                print "Starting X by xinit , with no $home/.xinitrc -> run X\n";
        }
        if ( -f "$home/.xserverrc" ) {
                $xserverrc = `ls -l $home/.xserverrc`;
                print "Starting X by xinit, using $home/.xserverrc\n",
                        " $xserverrc\n";
        }
        else {
                print "No .xserverrc files for xinit - use X\n";
        }
}
elsif ($how eq "X") {
        print "Starting by an X command - why?\n";
}
elsif ($how eq "xdm") {
        if ( -f "$home/.xsession") {
                $xsession = `ls -l $home/.xsession`;
                print "Starting X by xdm, using $home/.xsession\n",
                        " $xsession\n";
        }
        if ( -d "$xroot/lib/X11/app-defaults") {
                $xappdef = `ls -ld $xroot/lib/X11/app-defaults`;
                print "... xdm is using $xroot/lib/X11/app-defaults\n",
                        " $xappdef\n";
        }
}
else {
        print "Unknown method of starting - is X running ?\n";
}
#
#       X resources defined by ...
#
print "X Resources come from:\n";
if ( -f "$home/.Xresources") {
        $xsession = `ls -l $home/.Xresources`;
        print "$xsession\n";
}
if ( -f "$home/.Xdefaults*") {
        $xsession = `ls -l $home/.Xdefaults*`;
        print "$xsession\n";
}
if ( -d "$xroot/lib/X11/app-defaults") {
        $xsession = `ls -dl $xroot/lib/X11/app-defaults`;
        print "with system default resources from:\n$xsession\n";
}
#
#       X session errors and X Authority
#
if ( -f "$home/.xsession-errors") {
        $xsession = `ls -dl $home/.xsession-errors`;
        print "X Sessions errors are located in:\n$xsession\n";
}
if ( -f "$home/.Xauthority") {
        $xsession = `ls -dl $home/.Xauthority`;
        print "X authorization is located in:\n$xsession\n";
}

#
#       Clients running are:
#
print "The client programs running are:\n", `xlsclients`, "\n";
#
#       The x settings are:
#
print "The x settings are:\n", `xset -q`, "\n";
#
#       Window manager files:
#
$wmrc = $wm . "rc";
if ( -f "$home/.$wmrc") {
        $xwm = `ls -l $home/.$wmrc`;
        print "Starting $wm Window Manager using $home/.$wmrc\n",
                "$xwm\n";
}
elsif ( -f "$xroot/lib/X11/$wm/system.$wmrc") {
        $xwm  = `ls -l $xroot/lib/X11/$wm/system.$wmrc`;
        print "Starting $wm Window Manager using $xroot/lib/X11/$wm/system.$wmrc\n",
                " $xwm\n";
}

#Perl Script of the Day

#       Write a perl script called xconfig to print
#       information about your (a general user) X
#       configuration - from within X. The information
#       should include:
#       - X process information
#       - how X was started (startx, xinit, X, xdm)
#       - what window manager is running
#       - environment variables
#       - home directory .files as appropriate
#       - system wide default files as appropriate
#       - font server location
######################################################

#Sample Answer

