## Source via csh, dont run directly!
## Sets up shop in /tmp/H for machines that don't have anything yet.
## Mostly for MIT, but could apply most other places.
cd /
umask 077
if ( -d /tmp ) set home = /tmp
if ( -d /var/tmp ) set home = /var/tmp
if ( -d $home/H ) then
  echo "$home/H is ALREADY" 'THERE\!\!'
  exit
endif
cd $home
mkdir H
cd H
set home = ${home}/H
setenv HOME ${home}/H
rehash
cd
cat > ui << 'HHIui'
# Hobbit's general-purpose Unix box init; runs under *csh only.  Revised on
# a continuing basis to deal with SV lookalikes, Solarisn't, new BSDs, etc.
#
# edit: 981120@avian: careful coredumpsize; rearrange PATH construction
# edit: 980909@avian: fix ps-aliases, g9/gd, more whoiz, td, pull more uimd
# edit: 980107@av-lt: tfix/^O, exinit nomodelines:
# edit: 971127@fl-lt, new env-smash; $HBIN; zt/zstr; misc uimd-gloms;
#	  ul=/usr/local now; proc limits; misc solarisisms; whois-fix
# edit: 970207@work, "err", more uimd-merge; errno; LINES; news-rnd; z; ^K...
# edit: 960713@work, path, h4, flusht "dc", flesht tfix, some uimd-adds
# edit: 960409@avian, nuke more env vars; fixed tty-test nesting; misc..
# edit: 960325@shorts, insulated "more" test against noninteractive shells
# edit: 951221@narq, "tw", less dotfile auto-nukage, fixt web & mwt refs
# edit: 951107@shorts, nuke some "~/foo" aliases, assuming we'll "apath" ~/x
# edit: 951012@shorts, added man/preformat and stuff for nc
# edit: 950831@avian, finally put in HOME=here fix
# edit: 950816@avian, collected scattered fixes from bbn/ep + new stuff.
# edit: 950224@shorts, mit-compat:impending-P6
# edit: thousands previous, lost in antiquity
#
# WARNING: transfer in binary mode, since the EXINIT stuff and other random
# embedded ctl chars break if text interpretation is done!
#
# This isn't .login or .cshrc -- you source this after you log in.
# I may want a virgin shell sometimes.  This may not be your idea of the
# "correct" way to do things, so if you're looking for canonical login inits,
# find someone else's.  If you want this to act like .login, drop
# "source ui" into .login, which is easy to nuke when it loses.
# Pay attention to XXX markings when adapting to new machines.

# Job-wide stuff first -- don't take if we did already.
if (! $?INITED ) then
umask 022
# Path stuff.  Construct it carefully based on what's out there, starting with
# something reasonably sane.
setenv PATH /bin:/usr/bin:/etc:/usr/etc:/usr/lib
# Tack everything else on the end, allowing for all the braindead possibilities
# I've run into along the way.  Still by no means all-inclusive, but it's
# gettin' there.  Redone slightly 941205 from SNAPxx "bigpath" hack.
# dot ain't in there for obvious reasons.  No games, either.
# if X lives elsewhere, like .../X11R5/, fix in uimd.
foreach xx ( \
  /sbin /usr/sbin /usr/ucb /usr/bsd /usr/ccs/bin /usr/cs/bin /usr/libexec \
  /usr/lbin /usr/etc/yp /usr/etc/nis /usr/lib/netsvc/yp /usr/lib/netsvc/nis \
  /usr/lib/nfs \
  /usr/local/bin /usr/local/etc /usr/local/sbin /usr/local/lib /usr/local \
  /bin/athena /usr/athena/bin /usr/local/athena/bin \
  /etc/athena /usr/athena/etc /usr/local/athena/etc \
  /usr/openwin/bin /usr/NeWS/bin /usr/X11/bin /usr/bin/X11 /usr/bin/X11/bin \
  /usr/local/X11 /usr/local/X11/bin /usr/local/bin/X11 /usr/X/bin \
  /usr/lib/acct /usr/sbin/acct /usr/lib/sa /usr/contrib/bin /usr/contrib )
  if ( -d $xx ) setenv PATH ${PATH}:${xx}
end
rehash
setenv DPATH ${PATH}
# zorch various annoying things that misc login handlers give us
foreach xx ( \
  HOSTNAME HOST REMOTEHOST RHOSTNAME HOSTTYPE OSTYPE VENDOR MACHTYPE \
  USERNAME LOGNAME GROUP LOGIN SHLVL MANPATH PAGER MINICOM SPLITVT \
  SSH_CLIENT MAIL PWD OWD )
  setenv $xx AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
  unsetenv $xx
end
unset xx
# and some useless local vars
unset addsuffix dirstack uid gid group owd csh tcsh shlvl _ version
unset prompt prompt3 prompt2 prompt1 history
set history = 0
# Since this might be invoked as some other user, set up HOME to be wherever
# we are now.  While a kludge, this is more convenient than having to fix it
# up later.
set home = `\pwd`
setenv HOME $home ; cd $HOME
# Make a diligent attempt to find vi; otherwise, preload a complaint.
# EDITOR needs an abspath...
unsetenv EDITOR
if (-f /usr/ucb/vi) setenv EDITOR /usr/ucb/vi
if (-f /usr/bin/vi) setenv EDITOR /usr/bin/vi
if (-f /bin/vi) setenv EDITOR /bin/vi
if (! ${?EDITOR}) echo "Couldn't set EDITOR?\!"
# but PAGER *doesn't* need an abspath?  Go figger.  Probably popen()ed..
setenv PAGER 'more -l'
# if noninteractive shell, "more" hangs and stty fails, so avoid it all.
# "tty" has to be alone on the line, or ! INITED skips to this "else"!
tty -s
if ( $status ) then
  setenv PAGER 'cat'
  setenv EDITOR e
else
# as long as we're doing tty-dependent stuff, do one-shot stty pile here.
# turn off/on losing switches; since sttys vary all over the map, some of
# these may fail, so do them separately.
  stty sane
  stty crt
  stty intr \^C kill \^U erase \^\?
  stty susp \^Z eof \^D
  stty echoe echok
  stty icanon
  stty isig
# stty load -load5 -load15 -pid		# old Rutgers-ism
# ... that's back in another form on bsd4_4 family boxes!
  stty status '^T' >& /dev/null
  stty kerninfo >& /dev/null
# NOTE: osf [at least] breaks "more", thus the following test.
  more -l /dev/null >& /dev/null
  if ( $status ) setenv PAGER 'more -e'
endif
# vi has cute options from Hell too, although it'd never *say* so.
# due to various shell lossage, this has to be one big line.  Sigh..
setenv EXINIT 'map Q :q\!|map B 1G|map K 080lF xi|map U 0i	71lF xi|set ic showmatch terse noexrc nomodeline nomodelines'
# where to find manpages; same design as PATH.  X manpages are often off in
# obscure places..
setenv MANPATH /usr/share/man:/usr/man
foreach xx ( /usr/man/preformat /usr/local/man /usr/openwin/man \
  /usr/lib/X11/man /usr/local/X11/man /usr/bin/X11/man )
  if ( -d $xx ) setenv MANPATH ${MANPATH}:${xx}
end
setenv HBIN ~/x
# Grab system-dependent things here, instead of first off, so we can add
# local funny paths into uimd [which also checks INITED].
if (-f uimd) source uimd
# useful shorthands; mostly for sunos4.0, buglix, other 4.xbfd clones
if (-d /etc/security) setenv sec /etc/security
if (-d /usr/etc/sec) setenv sec /usr/etc/sec
if (-d /etc/security/audit) setenv aud /etc/security/audit
if (-d /var/yp) setenv yp /var/yp
# XXX: wheres yp/nis on solaris, bfd, etc?  stick in a locate-chain
if (-d /usr/spool/news) setenv bb /usr/spool/news
setenv dd /etc/dumpdates
setenv fs /etc/fstab
setenv pc /etc/printcap
setenv tc /etc/termcap
setenv um /usr/spool/mail
setenv mq /usr/spool/mqueue
# new-bsd and such
if (-d /var/mail) setenv um /var/mail
if (-d /var/spool/mqueue) setenv mq /var/spool/mqueue
setenv ul /usr/local
setenv ui /usr/include
setenv uis /usr/include/sys
setenv sys ~/sys
# news junk FROM HELL!! -- find equivalents, if any for vnews or use gnu..
setenv NICKNAME '*Hobbit*'
setenv NAME '*Hobbit*'
setenv ORGANIZATION 'large'
# New-group checking by default turned off.  To turn on, do "rrn +q"
setenv RNINIT '-C8 -d$HOME -e -F\ \ \  -g30 -h +hfrom -L -M -q -s -t -T -v +/'
setenv AUTOSUB \*
# trash the majority of right-widgeted includes; tell rn "v" if you want to
# see it all.  Guess what?  Most of the time you *DON'T* -- go figure, eh?
setenv HIDELINE '^>\|^.>\|^|\| article <\| writes:$\| wrote:$'
# NOTE: some "inews"es might not accept a new Path: header on POSTING.
setenv NEWSHEADER 'Newsgroups: %(%F=^$?%C:%F)\
Path: localhost\!%%s\
Organization: %o\
Sender: root\
Approved: God\
Date: %`${HOME}/idate`\
Expires: 25 Dec 99 12:34\
Subject: %s\n\n'
# swap in ${HOME}/Hinews if available, or maybe use the BLTF script?
# get "idate" from HPKG init stuff
# setenv NEWSPOSTER 'vi %h ;inews -h < %h' -- tossed in favor of...
setenv NEWSPOSTER 'vi %h ; inews -h < %h; mv %h %h.bak'
# Set machine-name-based prompt string that we can tack other things on to.
# This is a horrible kludge, but sed is fun, and there aren't any tr-like
# builtins that I can find.  [tr needs the brackets on some systems.]
set pcap = `hostname | sed 's;^\(.\).*$;\1;' | tr \[a-z\] \[A-Z\]`
setenv pbase ${pcap}`hostname | sed 's;.\([^\.]*\).*$;\1;'`
unset pcap
# Sometimes "user" doesn't get set, like on boot -s...
if (! $?user) then
  set user = `whoami | sed 's/ .*//'`
endif
# address "ls" lossage.  Syntax of -L and -g tends to vary machine to machine.
# L really should mean follow the link; g really should mean show groups.
# SV and its ilk tend to reverse them, bastards..
# Depends on ~/.link -> ~/ui -- create this yourself, dude
setenv LSL L
ls -Ll .link | grep ' ui' > /dev/null
if ( $status ) setenv LSL ''
set yy = `ls -lg /bin/cat | wc -c`
set xx = `ls -l /bin/cat | wc -c`
if ( $yy > $xx ) then
  setenv LSG g
else
  setenv LSG ''
endif
unset xx
unset yy
# handy stock-X-geometry; use to prevent the wm from ASKING where to put it!
setenv G '-g 80x65+20+0'
# Blindly nuking "~/.rhosts" may screw root if invoked under "su", although
# we shouldn't be using r-protocols anyways.  Test for this file in an assumed
# homedir first, and *then* zorch all the turds if we have it.
unset HHDIR
if ( -f $user/ui ) then
  set HHDIR = ~$user
  rm -f ${HHDIR}/.rhosts ${HHDIR}/.history ${HHDIR}/.sh_history
  rm -f ${HHDIR}/.bash_history ${HHDIR}/.stmp ${HHDIR}/.lastlogin
# rm -f $HOME/.ssh/known_hosts -- not yet
endif
unset HHDIR
# Where I dump my "man" temporaries
setenv MNF ~/xHM$$
rm -f ~/xHM* >& /dev/null
touch $MNF
chmod 600 $MNF
# how much junk mail piled up?  [rehack of "alias from"..]
if (-f $um/$user) then
  echo `egrep "^From " $um/$user | wc -l` new messages.
endif
# doesn't work in all cshes, so suppress errors really hard:
csh -f -c "limit coredumpsize 0" >& /dev/null
if ( ! $status ) limit coredumpsize 0
limit datasize 20000 >& /dev/null
# Okay to do now that ^K is nice and quick, but *only for top level*!
set ignoreeof
# and tell subsequent invocations that we did all this
setenv INITED 1
endif

# Shell-specific things.  Chop this off to create a .cshrc equivalent.
unset noglob
unset autologout
set mail=(60 ${um}/${user})
set history=25
unset savehist
umask 022
set filec
if ($?editmode) then
  unset editmode
endif
set notify
set fignore='.o'
# if I *want* to overwrite things, I don't need shit from the shell..
unset noclobber
# if we're god, change things a bit
if (`whoami | sed 's/ .*//'` == root) then
  set prompt="${pbase}\\!\\! "
# Intrusive -- but we SHOULD be doing this!
#  rm -f /etc/hosts.equiv
else
  set prompt="${pbase}+ "
endif

# aliases, by the fuckload.  I use every single one of these.
if (! $?HBIN) then
  setenv HBIN ~/x
endif
# This in response to OSF 'man's STUPID way of handling escape sequences
# XXX: breaks on freebsd and maybe others; the escape vanishes??
alias h "man \!* | sed 's/\[..//g' > $MNF ; ${PAGER} $MNF"
alias lman 'nroff -man \!* | ${PAGER}'
# in case man -k doesn't work like in Encore's lame-ass distribution
alias apropos "fgrep -i \!* /usr/man/whatis"
# Plugging in the above-derived LSx variables...
alias v "ls -laF${LSL}${LSG}s \!* | ${PAGER}"
alias vd "ls -laF${LSL}${LSG}sd \!* | ${PAGER}"
alias l 'ls -CFa \!*'
# alias ld 'ls -Fda \!*'
alias lsd 'ls -lF \!* | egrep /$'
alias lse 'ls -lF \!* | egrep \\\*$'
alias cdu 'cd ..'
alias cdul 'cd ..;ls -F'
alias pwd 'echo $cwd'
alias apath 'setenv PATH \!*\:${PATH} ; rehash'
alias dpath 'setenv PATH ${DPATH} ; rehash'
alias path 'printenv PATH'
alias sp 'set prompt = '
# greets to "jacko" for needing the following.  Thanks so much, asshole.
# Be sure to check out my "bomb shelter" in /var/tmp.
alias home 'set home = \!* ; setenv HOME \!* ; cd $HOME ; pwd'
alias t "${PAGER}"
# and because I keep doing othercommand | T ...
alias T "${PAGER}"
# if these invoke the wrong "zcat", one can always "alias zcat gzip -d -c"
alias zt 'zcat \!* | $PAGER'
alias zstr 'zcat \!* | strings -a | $PAGER'
alias g9 'gzip -c -9'
alias gd 'gzip -c -d'
# This is a really sick hack to emulate the old "edit" behavior of the 20.
# It works only because SIGSTPing out of emacs stops alias expansion.
# The second "alias g start_g" is to catch the case where you start up a
# new emacs and exit without ever pausing it.
alias start_g 'alias g "%emacs; alias g start_g"; emacs -nw; alias g start_g'
alias g start_g
alias str "strings -a \!* | ${PAGER}"
alias att "cat >>"
alias iv printenv
alias il printenv
# grepping 2-intro doesnt work everywhere, but errno.h is pretty consistent
# XXX: Arrrgh.  /usr/include/errno.h often points at OTHER places, so copy
# this line to uimd and fix after seeing where the real errno.h lives.
set EFILE = 'cant\\ find\\ errno.h'
if ( -f /usr/include/errno.h ) then
  set EFILE = /usr/include/errno.h
endif
if ( -f /usr/include/sys/errno.h ) then
  set EFILE = /usr/include/sys/errno.h
endif
alias err \
  'egrep #def.\*\[\ \	\]\!*\[\ \	\]  '"$EFILE"
unset EFILE
alias grep "fgrep -i"
alias h2 'head -23'
alias t2 'tail -23'
# 49/50 line mode vga screens are common
alias h4 'head -45'
alias t4 'tail -45'
diff -c0 /etc/group /etc/group >& /dev/null
# XXX: osf2 broke "diff", too; won't take -C 0 -- needs -C 1.  Sheeeyit.
# Fix in "uimd" if needed, this is just too ridiculous.
if ( $status ) then
  alias dp 'diff -C 0 \!* | ${PAGER}'
else
  alias dp 'diff -c0 -t \!* | ${PAGER}'
endif
set PSP = ps
set PSG = g
# solaris fix: two *different* flavors of ps, the default being that almost
# useless sysv style...
if ( -f /usr/ucb/ps && -f /usr/bin/ps ) set PSP=/usr/ucb/ps
# Assuming that ps -w is a valid indicator of bsd-ish PSes -- or rather, -ww
# updated: using -ax instead, and -g into ucb-flavor [show grp leaders on
# solaris, no-op on most others, but *errors* on some such as openBSD?!]
${PSP} ax >& /dev/null
if ( $status ) then
  alias sy "${PSP} -ef | ${PAGER}"
  alias fp "${PSP} -ef | fgrep -i"
  alias ip "${PSP} -f"
else
  ${PSP} g >& /dev/null || set PSG = ''
  alias sy "${PSP} auxw${PSG} | ${PAGER}"
  alias fp "${PSP} auxw${PSG} | fgrep -i"
  alias ip "${PSP} ux${PSG}"
  alias ups /usr/ucb/ps
endif
unset PSP
unset PSG
alias d rm
alias fix 'rm -f core; rm -f xHM*; rm -f .*.CKP .*.BAK *.CKP *.BAK \#*\# *\~'
alias sink sync
# tty stuff.  Lotta ancient artifacts in here.
alias  clear
alias _ 'source '
alias tit 'setenv TERM'
alias xt66 'setenv TERMCAP ~/xtermcap; setenv TERM xt66'
# this came later, after dumb machines started honoring the stty settings
alias xt68 "setenv TERM xterm; stty rows 68"
alias huge 'setenv TERMCAP ~/xtermcap; setenv TERM xt110'
# old xterm, etc aliases nuked in favor of ...
alias xi "~/xi"
alias bxt 'xterm -g 80x65+30+0 -n \?\?\? \!* &'
# A more elegant solution to the general xterm problem: Give it the arg.
alias xt "setenv TERM xterm ; stty rows \!*"
# this one's gotta stay in just for posterity:
alias i400 'echo "        I        I        I        I        I        I        I        I"; setenv TERM i400; stty tabs'
alias h19 'setenv TERM h19u'
alias vt100 'setenv TERM vt100'
# newer vi clones look at LINES and default to 24 *despite* stty rows setting
alias lines 'setenv LINES \!* ; stty rows \!*'
# x86-ix flavored console ttys [notably freebsd]
alias ansi25 'setenv TERM ansi ; setenv LINES 25 ; stty rows 25'
alias ansi50 'setenv TERM ansi ; setenv LINES 50 ; stty rows 50'
alias wt100 'setenv TERM vt100 ; stty rows 49 ; setenv LINES 49'
alias cons50 'setenv TERM cons25 ; setenv LINES 50 ; stty rows 50'
alias iter 'stty everything'
# needs to be stty -a on some SV-family boxes
stty everything >& /dev/null
if ($status) alias iter 'stty -a'
alias nec 'stty -echo'
alias ec 'stty echo'
# $[s and $[1r fixes lusing sun consoles
alias tfix 'reset; stty sane; stty erase \^\?; clear; echo "[s[1r\[0m[c ...Fixed\\!"'
alias bold 'echo "[1m"'
alias j jobs -l
alias su echo You do not have permission to su root
alias hang echo use Z
alias z suspend
alias hup 'kill -HUP'
alias nuke 'kill -9'
# aix admin crock
alias shit 'smit -s /dev/null -l /dev/null -C'
# fastest no-fuss way to log out
alias  'exec clear'
alias w 'w -h'
alias f finger
# some systems have yp, some don't..
### probably broke for NIS+..
tail -2 /etc/passwd | fgrep -s +:
if ($status == 0) then
  alias wu "ypcat passwd | fgrep -i"
else
  alias wu "cat /etc/passwd | fgrep -i"
endif
# Mail/communication stuff.  It's expected that ~/m/ exists and ~/mbox
# points down into it
alias m Mail
if (-d ~/m) alias m 'cd ~/m ; Mail'
# use a tmp file so we can send things to people again if they luse, or the
# same msg to other people
alias s 'cat > ~/.stmp; write \!* < ~/.stmp'
alias sa 'write \!* < ~/.stmp'
alias sm sendmail
alias mwt ${HBIN}/mwt
# stock "from" loses if you dont have a mail file...
alias from 'if (-f $um/$user) egrep "^From " $um/$user'
alias fromc 'if (-f $um/$user) egrep "^From " $um/$user | wc -l'
alias biff 'tail -1f $um/${user} | egrep "^(From |Subject: )" &'
# only if you have "host".  If you don't, get it.
# if you still lose, you can make them do "nslookup -q=any" instead.
alias mx 'host -v -t mx'
alias ns 'host -v -t ns'
alias hh "host -v -t any"
which host | fgrep -s /host >& /dev/null
if ( $status ) then
  alias hh "nslookup -q=any"
endif
# if you don't have netcat, you're a complete wanker.  Get it.
alias nc '\nc -v -r -w 3'
alias nz '\nc -v -r -w 3 -z'
alias ncs '\nc -v -w 1 -n 127.0.0.1'
# hopefully just trap the [single?] ether-addr; can override in uimd
# is followed with port number and other options [eg, host-restrict arg?]
alias ptrap '\nc -v -l -s `hostname` -p'
alias SA 'setenv SRCADDR'
# and the old equivalents...
alias tn telnet
alias tns 'telnet 127.0.0.1'
alias tnh 'telnet `hostname`'
alias rl rlogin
# Read my scrips: The Web SUCKS!  This is how I normally deal with it.
alias web "${HBIN}/web"
alias hwt "${HBIN}/hwt"
# and if I *have* to use a browser...
alias lx 'lynx -telnet -vikeys /dev/null'
alias netscam netscape
# they took the *real* NIC away, snif; and many "whois"es are still broken
alias ww 'whois -h rs.internic.net'
alias wa 'whois -h whois.arin.net'
# for CIDR aggregates, but a little behind the NIC sometimes
# alias wm 'whois -h prdb.merit.edu' -- supplanted with..
alias wm 'whois -h whois.ra.net'
# and for the EU side of things ...
alias we 'whois -h whois.ripe.net'
# fuqn broken linux "fwhois" wants different syntax; try to autodetect
if ( -f /boot/boot.b || -f /etc/lilo.conf ) then
  alias ww 'whois \!*@rs.internic.net'
  alias wm 'whois \!*@whois.ra.net'
  alias we 'whois \!*@whois.ripe.net'
  alias wa 'whois \!*@whois.arin.net'
endif
# there are many traceroutes that don't allow wait times of less than 2 sec..
alias tw 'traceroute -w 2 -q 2 -p 51586'
alias twn 'traceroute -w 2 -n -q 2 -p 1'
# some pings are different: -s means size of packet, with -c count,
# and on sv-ish kernels, packets < 64 bytes never get sent!!
ping 127.0.0.1 70 1 >& /dev/null
if ( $status ) then
  alias pz 'ping -s 70 -c 1 \!*'
  alias bp 'ping -s 1460 \!*'
else
  alias pz 'ping \!* 70 1'
  alias bp 'ping -s \!* 1460'
endif
# and when all else fails, or ICMP is filtered, or whatever...
alias pnc '\nc -v -v -w 4 -r -z \!* 11121-11122'
alias port 'fgrep -i \!* /etc/services /etc/inetd.conf'
# solaris-ism:
if ( -d /etc/inet ) \
  alias port 'fgrep -i \!* /etc/inet/services /etc/inet/inetd.conf'
alias nn 'netstat -n'
# common tcpdump stuff: the bare-minimum, and the full wazoo
# [-x is more useful with the hex-n-ascii patch, of course]
alias td 'tcpdump -tnql'
alias tda 'tcpdump -xlvents 1500'
# all you need now is -Plpnn; postscript flagged by %! at top of input
# on lpds that invoke the psfilters
alias qp 'lpr -h -l'
alias ni 'date "+%a  %y%m%d.%H%M%S "'
# if we have ntpq..
alias rni 'ntpq -c rv \!* | fgrep clock='
# hit some known timeservers
alias rnil 'ntpq -n -c rv 128.4.1.1 128.224.1.172 192.5.41.41 | fgrep clock='
# other services
alias weather "sh ${HBIN}/weather"
alias alta "sh ${HBIN}/alta"
# CNN page is kind of a moving target, but wtf
alias cnn 'echo GET /QUICKNEWS/print.html | \nc -w 14 www.cnn.com 80 | \\
  sed -f $sys/de-html.sed | $PAGER'
# if running under X, idiot-proof ourselves *again*
if ( ${?XPID} ) set ignoreeof

# Call this again in case we were already inited.  It's small and checks
# INITED itself, so this should be harmless.  Unless you drop weird stuff
# into it.  Done down here so you can override any of the above aliases.
if ( -f uimd ) source uimd


'HHIui'
cat > xi << 'HHIxi'
#! /bin/sh
## Start X my way, using my path [preset via UI] all the way down.
## This is multi-purpose: it's the startup script, and it's also the startup
## script and the other startup script depending on what name it was run as.
## But wait, there's more!  You also get this X-protector script absolutely
## free, only through this special TV offer.  Now how much would you pay?!
##
## edit: 981102,avian: more window/icon mapping-lossage aversion attempts
## edit: 981021,avian: predef XIAM; final xi9 tweaks; call it done f' now..
## edit: 981013@avian: tweak to xi9; smaller console-window; tee XP output
## edit: 981008+@avian: include xp; idiot-checks; menu fixups; MOVED twmrc;
##   don't dink DISPLAY; dont allow this-hostname unless *really* needed 
## edit: 970206@avian: minor cleanups; more 127.0.0.1; in-prog-UT-P8...
## edit: 950824@avian; fix 127.0.0.1:0 and minor invoc 3 cleanup stuff
## edit: 950815@avian; pull in sep. fixes; hope loopback-xhost still works
## edit: 950504@epil; sun fixups, diff. approach to "xhost localhost"..
## edit: 950503@mit; P6-compat; consolidation, openwin-re-compat...
## edit: 941205@mit et al; finally got AIX working, and rlogin -x!!!

## A little history: switching with environment variables or magic arguments
## was unreliable at best.  $0, our own progname, is about the only thing that
## has any chance of surviving xinit's random braindeath.  Thus, this is called
## "xi" with symlinks "xi.s" and "xi.c" for server and client.
## Later hack: if it's called .xsession, the right thing happens.  We hope.
## even later hack: if it's called "xi.p", do X-Protect listen on 6000

test ! -d "$HOME" && echo 'No HOME, cant run' && exit 1
test ! -f "$HOME/xi" && echo "No $HOME/xi, cant run" && exit 1

# first figure out how we were invoked; suck out the basename
XPN=`echo $0 | sed 's/.*\///'`
XSES=''
if test "${XPN}" = ".xsession" ; then
  XSES="y"
  XPN="xi"
fi
if test "${XPN}" != "xi"; then
  if test -z "${XPID}" ; then
    echo Cannot invoke $XPN directly, use \"xi\"
    exit 1
  fi
fi

## Invocation 1: from the top level shell when we start.
if test "${XPN}" = "xi" ; then
# idiot check
  if test "${XPID}" ; then
    echo X is already running, dickweed!
    exit 1
  fi
  XPID=$$ ; export XPID
  umask 077
# optional arg: name or IP of myself, in case of confusion or DNS hoseage
  test "$1" && XIAM="$1" && export XIAM

# if under XDM, skip the server startup poop
  if test "$XSES" ; then
    cd
    exec ./xi.c > $HOME/Xerrors 2>&1
    echo "XSESSION exec of ${0}.c fucked up!"
  fi
  rm -fr /tmp/.X11*
  rm -fr /tmp/.NeWS*
  sync
# Call xinit, which takes "client -args -- server -args" ... seems kinda
# bass-ackwards, but hey...  This blocks until everything under it exits.
  xinit ${0}.c -- ${0}.s > $HOME/Xerrors 2>&1
  kbd_mode -a ; reset
  rm -f $HOME/.twmrc
  echo X server xit...
  exit 0
fi

## Invocation 2: server startup from above.  Path all pre-set.
## Blocks until the server exits.  Xnews is invoked here; add searches for
## a different one if you like.  The server might just be called "X" on
## some machines.  This can't be done as a "for" because servers take
## different args.  At least we can reuse *some* of the code..
## Dismal "which | egrep" kludge because "which" always exits 0.
if test "${XPN}" = "xi.s" ; then
# this crock seems to make nee-wiss work better...
  if test -d /usr/openwin/bin ; then
    OPENWINHOME=/usr/openwin
    export OPENWINHOME
  fi
# aix is a special case, in the educational sense of the word; it will need
# a completely different section.  "which aixterm" might be a good test
  srv=`which aixterm | egrep -v '^[Nn]o '`
  if test -f "${srv}" ; then
    echo Starting aix X ...
    exec X -f 3 nologo r -s 15 || echo "AIX SUX!!"
    exit 0
  fi
# Buglix doesn't take quite the same args as everyone else, so the same
# sort of test should sniff it out and deal
  srv=`which dxterm | egrep -v '^[Nn]o '`
  if test -f "${srv}" ; then
    echo Starting Buglix X ...
    exec X -auth /dev/null || echo "Buglix Blew its Breakfast!"
    exit 0
  fi
# try and run xnews instead [SunlOSs]
  srv=`which xnews | egrep -v '^[Nn]o '`
  if test -f "${srv}" ; then
    echo Starting $srv ...
    exec $srv :0 -nobanner -auth /dev/null || echo $srv fucked up: status $?
    exit 0
  fi
# try and find a default system "X".  Using -f, because sometimes "test" is
# buggy and returns true on "-x null-string"...
  srv=`which X | egrep -v '^[Nn]o '`
  if test -f "${srv}" ; then
    echo Starting $srv ...
# this is now right for solarisn't, may break on other things.
    exec $srv -nobanner -auth /dev/null \
      || echo $srv isnt configured right in this script!
    exit 0
  fi
  echo "Cant find any X server?!"
  exit 1
# end xi.s
fi

## Invocation 3: The server is running; now fire off everything else.
## This can be carved out as .xsession for XDM-handled xterms if this
## whole file doesn't work as such for some reason.  Set XIAM if needed.
#
if test "${XPN}" = "xi.c" ; then
## BEGIN standalone .xsession if needed:
#! /bin/sh
  cd
  umask 077
  echo Server running, old DISPLAY ${DISPLAY}.
  xhost
# try and secure ourselves; things can sometimes talk to loopback!  Some
# systems may not grok this, and woe betide the user of an xterm whose
# shell and apps are really on a different server...
  xhost 127.0.0.1
# don't dink DISPLAY if it's already set
  case "$DISPLAY" in
    "" ) DISPLAY='127.0.0.1:0' ; export DISPLAY ;;
  esac
# need to also allow our own external [settable] IP, or sunloss cant deal
  case "$XIAM" in
    "" ) IAM=`hostname` ;;
    ?* ) IAM="$XIAM" ;;
  esac
  case "$IAM" in
    "" ) echo "WARNING: cant get my own hostname?!" ;;
## XXX: really want a way to force unix-socket only, dammit..
    ?* ) xhost "${IAM}" ;;
  esac
  xhost -
# Zap some magic options into the server first.
# cool cursors: box_spiral cross diamond_cross iron_cross plus spider 
xrdb -merge << EoTT
BorderWidth: 2
Background: wheat
! bordercolor is apparently overridden by twm in most cases
BorderColor: yellow
xterm*iconGeometry: 50x30-50+130
! overrideable with +ut :
xterm*utmpInhibit: on
xterm*background: lightgray
xterm*foreground: black
xterm*borderColor: yellow
xterm*cursorColor: white
xterm*internalBorder: 1
xterm*jumpScroll: true
xterm*pointerColor: orchid
xterm*pointerShape: plus
! nuff for now, add to this more later
! fer instance: xterm keybinding for secure() -- a royal bitch?!
!! want the equiv of:  -s  -rw  -j  +sb  -ut
xterm*waitForMap: true
xterm*loginShell: false
xterm*multiScroll: true
xterm*reverseWrap: true
xterm*rightScrollBar: false
xterm*scrollbar: false
xlock*mode: hop
xlock*saturation: 1
xload*highlight: green
EoTT
# Put any one-shot stuff here: psh invocations, xrdb, etc...
  chmod 700 /tmp/.X11*
  chmod 700 /tmp/.NeWS*
  chmod 700 /tmp/smdb*
# setting beep freq doesnt work everywhere, and keyclick is just annoying
  xset b 40 4000 50 c 0 m 10 10 r
  xsetroot -cursor_name iron_cross -solid saddlebrown
# This sometimes has permanent effects on xterms.  Too fucking bad.  Deal.
# It must go in this order, i.e. keysyms last, and separate invocations
# so the whole deal doesn't get skipped.  May still make capslock work as
# "control lock" instead, depending on kb driver.  DEATH to CAPS-LOCK!!
  xmodmap -e 'clear Lock' -e 'clear Control'
  xmodmap -e 'add Control = Control_L Caps_Lock'
  xmodmap -e 'keysym Caps_Lock = Control_L'
  xmodmap -e 'keysym BackSpace = Delete'
## spin off .twmrc from here, so we don't need a separate file
  cat > $HOME/.twmrc << EoTT2
## least-intrusive-possible TWM init ...
borderwidth 1
### COLORS are defined in .../lib/rgb.txt
color {
# Defining bordercolor here seems to win over xrdb.
  bordercolor "yellow"
  iconbordercolor "green"
  menubackground "navyblue"
  menuforeground "yellow"
}
### CURSORS are defined in .../include/X11/cursorfont.h.
# set root cursor via "xsetroot", nothing available here.
cursors {
  button "dot"
  icon "crosshair"
  select "hand1"
  destroy "X_cursor"
}
iconborderwidth 1
# very important to me:
notitle
maxwindowsize "1000x1000"
opaquemove
nomenushadows
# things that I want to flip around between real fast with f.warpring
windowring { "xterm" "dxterm" "aixterm" "emacs" }
# menus -- don't need no stinkin menus, really, but they're sometimes fun
menu "Gah" {
  "What?!" f.title
  "Local ->" f.menu "Hell"
  "Refresh" ( "black" : "cyan" ) f.refresh
  "Move" ( "black" : "cyan" ) f.move
  "Reset" f.restart
  "Nuke..." f.delete
# Complete bailout: hunt down and kill the server.  Drastic...
  "Eject!!!" ( "cyan":"red" ) ! "kill -9 \${XPID}"
}
# the obligatory "machines" menu.  "!" is f.exec; optional-colors are (fg:bg)
# delicate pastel colors: turquoise salmon pink lavender wheat skyblue
#   powderblue khaki sandybrown rosybrown plum thistle
# SYSDEP: MIT-pub and various other servers.  put your own pile here
menu "Hell" {
  "Hosts ->" f.menu "Damnation"
  "Tall " ( "black":"thistle" ) ! \
    "xterm -g 80x65+0+0 -bg thistle -n Local &"
  "   Tall" ( "black":"wheat" ) ! \
    "xterm -g 80x65+360+0 -bg wheat -n Local &"
  "Short " ( "black":"powderblue" ) ! \
    "xterm -g 80x34+10+10 -bg powderblue -n lcl &"
  "   Short" ( "black":"khaki" ) ! \
    "xterm -g 80x34+370+10 -bg khaki -n lcl &"
  "dl" ( "black":"pink" ) ! \
    "xterm -g 80x65+0+0 -bg pink -n DL -e $HOME/x/dl &"
}
# XXX: fix this crud, it's hopelessly outa date
menu "Damnation" {
  "localhost" ( "black":"salmon" ) ! \
    "xterm -g 80x34+34+34 -bg salmon -n TNS -e telnet localhost &"
  " ----- " ( "white":"red" ) ! "echo invalid from here down"
  "Asylum" ( "black":"powderblue" ) ! \
    "xterm -g 80x65+0+0 -bg powderblue -n Asylum -e /usr/ucb/rlogin \
      asylum.sf.ca.us -l skey &"
  "Axylum" ( "black":"skyblue" ) ! \
    "xterm -g 80x65+0+0 -bg skyblue -n Axylum -e /usr/athena/bin/rlogin \
      asylum.sf.ca.us -l hobbit -k ELF.COM -x &"
  "Bronze" ( "black":"khaki" ) ! \
    "xterm -g 80x65+0+0 -bg khaki -n Bronze -e rlogin bronze.lcs.mit.edu \
      -l skey &"
}
# buttons.  This is gonna make it feel kinda like Windows, believe it or not.
# Not that I particularly like windows, but the idea here is to minimize
# use of the fucking RODENT to do what I want.
### KEYSYMS can be seen via "xmodmap -pk".  "Meta" is usually the little
### diamond-splat key next to space on sun keyboards; compose on maxines;
### poke around, you'll find it.
button1 = m : root : f.menu "Gah"
button1 = m : icon | window : f.move
button1 = m : frame : f.resize
# attempt to prevent the vanishing windows problem.  The twm manpage
# mumbles something about wanting *some* menu-binding to "TwmWindows" to
# prevent windows from randomly being completely unmapped to "withdrawn"
# state and lost.  So we set one up, but sorta hide it obscurely ...
button2 = c : frame : f.menu "TwmWindows"
# and dink other likely things to try and keep *everything* mapped somehow
# Donticonifybyunmapping { "xterm" "dxterm" "aixterm" "xclock" "xload" }
IconifyByUnmapping { "Hmmph" }
IconManagerDontShow { "Hmmph" }
"Tab" = m : icon | window : f.warpring "next"
"Tab" = s | m : icon | window : f.warpring "prev"
# warpring doesn't do the right thing if you're sitting in the root
"Tab" = m : root : f.warpto "xterm"
# "Esc" = c : all : f.warptoiconmgr ""
"F4" = c : window : f.iconify
"F4" = m : all : f.destroy
"L" = m : all : f.refresh
### end of .twmrc
EoTT2
# clean up any here-document turds
  rm -f /tmp/sh[0-9]*
  rm -f /var/tmp/sh[0-9]*
  sync
# now, let's use all that
# keep track of processes we fire off so we can nuke them later
  XPL=
  xclock -g 80x80-0+0 -u 1 -bw 0 -fg blue -hd SpringGreen -hl red &
  XPL="${XPL} $!"
  xload -bg cadetblue -jump 1 -update 2 -g 100x100-0-0 &
  XPL="${XPL} $!"
  twm -v -s &
  XPL="${XPL} $!"
# XProtect should be able to crap into the console window
  { $HOME/xi.p "$IAM" 2>&1 || \
    echo "WARNING: XProtect NOT running for some reason" ; } | \
   tee -a /dev/console &
  XPL="${XPL} $!"
  echo Killpids: $XPL
# and the last thing that blocks; unwind starts when this goes away.  I'm
# blocking on the console xterm, NOT the window manager.
  xterm -g 80x8+4+4 -C -n console || echo "Bugger!  no console"
#  xterm -g 80x65+0+5 -C -n console || echo "Bugger!  no console"
  echo Unwinding...
  kill $XPL
  sleep 2
  cd
  rm -f .Xauthority .xsession-errors
# these separated because of braindead shell globbing errors
  rm -fr .mosaic*
# this blows away .netscape/preferences.js [for 4.x at least, maybe others],
# but we can regen a magic fixed version from elsewhere
  rm -fr .netscape*
  rm -fr /tmp/.X11*
  rm -fr /tmp/.NeWS*
  rm -fr /tmp/smdb*
  sync
  exit 0
## END .xsession, or xi.c
fi

## Invocation 3a might be to construct .netscape/preferences.js et al..

## Invocation 4: self-protect our external IP addr, cuz we can't tell the
## fuckhead server itself to *not* listen there.  Fortunately, we can get in
## its way and catch inbound connections
if test "${XPN}" = "xi.p" ; then
## BEGIN external "xp" section, should it need customization..
#! /bin/sh
yep () {
  return 0 ;
}
MYTHING=`hostname`
# allow specific addr or name passed in...
  case "$1" in
    ?* ) MYTHING="$1" ;;
  esac

# wait for stuff to settle down
  sleep 10
# "nc" needs to be findable and be able to listen the right way
  nc -z $MYTHING 60143 59880 || exit 1
  echo XProtect:
  xhost
  nc -v -s $MYTHING -l -n -p 2000 127.0.0.4 310 < /dev/null &
  XPP="$!"
  trap "kill $XPP $XPA ; exit 0" 1 2 3 13 14 15 16
  while yep ; do
# iterate three quickly, and then insert some dead-time
    nc -v -s $MYTHING -l -n -p 6000 127.0.0.5 218 < /dev/null &
    XPA="$!"
    wait $XPA
    echo '        ###  INTRUDER  ALERT  ###'
    nc -v -s $MYTHING -l -n -p 6000 127.0.0.4 218 < /dev/null &
    XPA="$!"
    wait $XPA
    echo '        ###  INTRUDER  ALERT  ###'
    nc -v -s $MYTHING -l -n -p 6000 127.0.0.4 218 < /dev/null &
    XPA="$!"
    wait $XPA
    echo '        ###  INTRUDER  ALERT  ###'
    sleep 4
  done
  exit 0
## END xp
fi

echo "Oops!  Invoked as $XPN, which is bogus"
exit 1

## remaining stuph...
## other os-specific xterm-substitutes?
## fuq with twm IconMapping stuff?  distant xterms are *still* vanishing

'HHIxi'
cat > uimd << 'HHIuimd'
### Machine-specific stuff for MIT publics
if (! ${?INITED}) then
# make "mt" less painful; plug appropriate device in here
# setenv TAPE /dev/rmt0
### where do we usually read netluse from? [site-dep]
# setenv NNTPSERVER newsbox
more /etc/motd
# MIT: who knows who last used this WS
last | head -6
grep :: /etc/passwd ; grep :0: /etc/passwd
# Things to make the MIT environment more useful:
attach -n -h sipb outland consult graphics gnu
setenv MANPATH /usr/athena/man:/mit/sipb/man:/mit/consult/man:${MANPATH}
# Real path-builder is "mapath" below, but doing this again cant hurt
rehash
# and since some things *still* luse, make where they might live available
setenv UA /usr/athena
setenv UAB /usr/athena/bin
## the grand endif
endif

# shell-specifics below.
# the neurons might still fire the following way
rm -f /etc/hosts.equiv
# if running under X, idiot-proof ourselves
if (${?XPID}) set ignoreeof
# etherfind/snoop/whatever is highly machine-dependent.
# snoop - a turns on the speaker; you may not want this
alias EF 'snoop -a -d le0 -s 500'
# alias EF1 'snoop -d le1 -s 500'
# alias EF 'etherfind -v -i ie0'  ...etc...
## additions below?
alias weather "finger weather@cirrus.mit.edu | $PAGER"
alias rlb 'rlogin bronze.lcs.mit.edu'
# MIT hack: to add paths where stuff really lives.  Depends on their
# having been previously attached above...
alias mapath \
  'setenv PATH /mit/\!*/`machtype`bin:/mit/\!*/`machtype`etc:${PATH}; rehash'
alias zw "zwrite -s '*Hobbit*'"
'HHIuimd'

# additional fixups
echo "alias ui 'source ~/ui'" > .cshrc
ln -s ui .link
ln -s xi xi.c
ln -s xi xi.s
ln -s xi .xsession
chmod 755 xi
source ./ui
