Ubuntu starts X-windows from GDM (for systems using the standard ubuntu-desktop) or from KDM (for systems using kubuntu-desktop).
To run a script prior to GDM/KDM startup, you could either
- Write your own Upstart script to run prior to GDM; or
- Modify your system GDM script to run a custom external script (eg, your /etc/init.d/whereami script) as one of its initial tasks.
Ubuntu uses Debian-style runlevels: runlevel 1 is single-user, recovery mode; runlevels 2-5 are all the same (GUI multi-user); and by default the system boots to runlevel 2.
Ubuntu 9.10 uses Upstart, which provides service startup scripts in /etc/init. The more traditional init scripts in /etc/init.d and /etc/rc.d are still available for services that haven't been ported to Upstart-style init scripts, but GDM has one. (Note Ubuntu 9.10 provides /etc/init.d/gdm but doesn't create any /etc/rcX.d symlinks for it.)
GDM startup is controlled by /etc/init/gdm. These lines at the top of the script specify when to start and stop GDM. Notice there's no runlevel specification for starting GDM -- only services that must be started first.
start on (filesystem
and started hal
and tty-device-added KERNEL=tty7
and (graphics-device-added or stopped udevtrigger))
stop on runlevel 
See man 5 init for documentation on Upstart scripts, and examine your system's /etc/init/*.conf files for some examples. Also see the starting event manpage:
A service that wishes to be running whenever another service would be running, started before and stopped after it, might use:
start on starting apache
stop on stopped apache
A task that must be run before another task or service is started might use:
start on starting postgresql
Also note that the GDM/KDM Upstart scripts emit a signal, starting-dm, that might also be useful.
So an Upstart script designed to run before GDM should use one of these start on clauses:
# run only when starting GDM
start on starting gdm
# run when starting GDM or KDM
start on (starting gdm
or starting kdm)
# run when starting any DM
# starting-dm is a custom event emitted by the GDM/KDM/etc scripts
start on starting-dm