#! /bin/sh
#
# c2z - environment conversion tool
# Contributed by Bart Schaefer
# (Tweaked a bit by Paul Falstad)
# (Tweaked again by Bart Schaefer)
#
# This is a quick script to convert csh aliases to zsh aliases/functions.
# It also converts the csh environment and local variables to zsh.  c2z
# uses the csh to parse its own dot-files, then processes csh output to
# convert the csh settings to zsh.
#
# When run as a zsh fuction, c2z runs csh as if it were an interactive
# shell whenever the parent zsh is interactive.  When run as a shell
# script, the -i switch can be used to force this behavior.
#
# The -l (login) switch causes csh to run as if it were a login shell.
# This is done "properly" if c2z is used as a zsh function, otherwise
# it's faked by explicitly sourcing .login.  Use with caution if your
# .login initializes an X server or does other one-time-only startup
# procedures.
#
# usage:
#	c2z [-i | -l | filename]
#
# You can use this script in your .zshrc or .zlogin files to load your
# regular csh environment into zsh; for example, in .zlogin:
#
#	. =(c2z -l)
#
# This is not perfect, but it gets most common aliases and variables.
# It's also rather time-consuming to do this every time you log in.
# However, if you're moving from csh to zsh for the first time, this
# can get you started with a familiar environment right away.
#
# In case your mailer eats tabs, $T is set to expand to a tab.
#
T="`echo x | tr x '\011'`"

# If we're zsh, we can run "- csh" to get the complete environment.
#
MINUS=""
LOADFILE=""
INTERACT=""
CSH=csh
case "$ZSH_NAME$ZSH_VERSION$VERSION" in
zsh*)
    case $1 in
    -l*) MINUS="-" ;;
    -i*) INTERACT="-i" ;;
    *) LOADFILE="source $1" CSH="csh -f";;
    esac
    if [[ -o INTERACTIVE ]]; then INTERACT="-i"; fi
    setopt nobanghist
    ;;
*)
    case $1 in
    -l*) LOADFILE="source ~/.login" ;;
    -i*) INTERACT="-i" ;;
    *) LOADFILE="source $1" CSH="csh -f";;
    esac
    ;;
esac

( eval $MINUS $CSH $INTERACT ) <<EOF 2>&1 >/dev/null
$LOADFILE
alias >! /tmp/cz$$.a
setenv >! /tmp/cz$$.e
set >! /tmp/cz$$.v
EOF

# save stdin
exec 9<&0

# First convert aliases
exec < /tmp/cz$$.a

# Taken straight from ctoz except for $T and "alias --"
sed -e 's/'"$T"'(\(.*\))/'"$T"'\1/' >/tmp/cz$$.1
grep ! /tmp/cz$$.1 >/tmp/cz$$.2
grep -v ! /tmp/cz$$.1 >/tmp/cz$$.3
sed -e "s/'/'"\\\\"''"/g \
    -e 's/^\([^'"$T"']*\)'"$T"'\(.*\)$/alias -- \1='"'\2'/" \
    /tmp/cz$$.3
sed -e 's/>\(&*\)!/>\1|/g' \
    -e 's/!\*:q/"$@"/g' \
    -e 's/\(![:#]*[^'"$T"']*\):q/"\1"/g' \
    -e 's/!\([-0-9][0-9]*\)\(:x\)*/$\2(fc -nl \1)/g' \
    -e 's/\$:x(fc/$=(fc/g' \
    -e 's/![:#]*\([^'"$T"']\)/$==\1/g' \
    -e 's/\$=\(=[^'"$T"']*\):x/$\1/g' \
    -e 's/\$cwd/$PWD/' \
    -e 's/^\([^'"$T"']*\)'"$T"'\(.*\)$/\1 () { \2 }/' \
    /tmp/cz$$.2

# Next, convert environment variables
exec < /tmp/cz$$.e

# Would be nice to deal with embedded newlines, e.g. in TERMCAP, but ...
sed -e '/^SHLVL/d' \
    -e '/^PWD/d' \
    -e '/^_=/d' \
    -e "s/'/'"\\\\"''"/g \
    -e "s/^\([A-Za-z0-9_]*=\)/export \1'/" \
    -e "s/$/'/"

# Finally, convert local variables
exec < /tmp/cz$$.v

sed -e 's/'"$T"'/=/' \
    -e "s/'/'"\\\\"''"/g \
    -e '/^[A-Za-z0-9_]*=[^(]/{
	s/=/='"'/"'
	s/$/'"'/"'
	}' |
sed -e '/^argv=/d' -e '/^cwd=/d' -e '/^filec=/d' -e '/^status=/d' \
	 -e '/^autolist=/s/.*/setopt autolist/' \
	 -e '/^correct=all/s//setopt correctall/' \
	 -e '/^correct=/s//setopt correct/' \
	 -e '/^histchars=/s//HISTCHARS=/' \
	 -e '/^history=/s//HISTSIZE=/' \
	 -e '/^home=/s//HOME=/' \
	 -e '/^ignoreeof=/s/.*/setopt ignoreeof/' \
	 -e '/^noclobber=/s/.*/setopt noclobber/' \
	 -e '/^notify=/d' \
	 -e '/^prompt=/s/!/%h/' \
	 -e 's/^prompt/PROMPT/' \
	 -e '/^showdots=/s/.*/setopt globdots/' \
	 -e '/^savehist=/s//HISTFILE=\~\/.zhistory SAVEHIST=/' \
	 -e '/^who=/s//WATCHFMT=/'


exec 0<&9

rm /tmp/cz$$.?
exit
