location=ftp://ftp.pdc.kth.se/pub/heimdal/binaries/i386-pc-cygwin/latest/

download_dir=/tmp
heimdal=heimdal.tar.gz
xfiles=xfiles.tar.gz
user_settings=".user-settings"

local_heimdal=$download_dir/$heimdal
local_xfiles=$download_dir/$xfiles
default_user_settings="default${user_settings}"

save_old_file()
{
   if [ -f $1 ]; then
      echo "   Renaming $1 to ${1}.old"
      mv $1 ${1}.old
   fi
}

# Make sure we have wget
if [ -x /usr/bin/wget ]; then
   :
else
   echo "Could not find wget! Please install wget and try again."
   exit 1
fi

echo -n "Downloading the heimdal archive... "
rm -f $local_heimdal
(cd $download_dir && wget -nv ${location}${heimdal})
echo "done."

# Install the heimdal files
echo -n "Installing heimdal... "
(cd /usr && tar zxf $local_heimdal && rm -f $local_heimdal)
echo "done."

echo -n "Downloading modified X startup files... "
(cd $download_dir && wget -nv ${location}${xfiles})
echo "done."

if [ -f /usr/X11R6/bin/startx ] || [ -f /etc/X11/xinit/xinitrc ]; then
   echo "Installing the X startup files... "
else
   echo -n "Installing the X startup files... "
fi

save_old_file /usr/X11R6/bin/startx
save_old_file /etc/X11/xinit/xinitrc

(cd / && tar zxf $local_xfiles && rm -f $local_xfiles)
echo "done."

echo -n "Downloading default user settings... "
(cd "$HOME" && wget -nv ${location}${default_user_settings} \
    && mv $default_user_settings $user_settings)
echo "done."

echo -n "Downloading .inputrc... "
(cd "$HOME" && wget -nv ${location}default.inputrc \
    && mv default.inputrc .inputrc)
echo "done."

echo "The settings are stored in "$HOME"/$user_settings."
echo "Please edit this file to set your preferences."
echo
echo "Finished!"
echo "Restart cygwin and type startx to start X windows"
