#!/bin/sh
# Copyright 1999  Patrick Volkerding, Moorhead, Minnesota USA
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#

if [ "$USER" = "root" ]; then
  TMP=/var/log/setup/tmp
else
  TMP=$HOME/.xwmconfig
fi

if [ ! -d $TMP ]; then
 mkdir -p $TMP
 chmod 700 $TMP
fi

# Remove any previous script:
rm -f $TMP/tmpscript.sh

# Add the top of the script:
cat << EOF > $TMP/tmpscript.sh
#!/bin/sh
dialog --title "SELECT DEFAULT WINDOW MANAGER FOR X" --radiolist \\
"Please select the default window manager to use with the X Window \\
System.  This will define the style of graphical user interface \\
the computer uses.  GNOME and KDE provide the most features. People \\
with Windows or MacOS experience will find either one easy to use.  \\
Other window managers are easier on system \\
resources, or provide other unique features." 16 74 4 \\
EOF

# Add GNOME as the first and default entry:
if [ -r /var/X11R6/lib/xinit/xinitrc.gnome ]; then
  echo "\"xinitrc.gnome\" \"GNOME: GNU Network Object Model Environment\" \"on\" \\" >> $TMP/tmpscript.sh
fi

# Then, we add KDE:
if [ -r /var/X11R6/lib/xinit/xinitrc.kde ]; then
  if [ ! -r /var/X11R6/lib/xinit/xinitrc.gnome ]; then
    echo "\"xinitrc.kde\" \"KDE: K Desktop Environment\" \"on\" \\" >> $TMP/tmpscript.sh
  else
    echo "\"xinitrc.kde\" \"KDE: K Desktop Environment\" \"off\" \\" >> $TMP/tmpscript.sh
  fi
fi

# Add Enlightenment:
if [ -r /var/X11R6/lib/xinit/xinitrc.e ]; then
  echo "\"xinitrc.e\" \"Enlightenment\" \"off\" \\" >> $TMP/tmpscript.sh
fi

# Add WindowMaker:
if [ -r /var/X11R6/lib/xinit/xinitrc.wmaker ]; then
  echo "\"xinitrc.wmaker\" \"WindowMaker\" \"off\" \\" >> $TMP/tmpscript.sh
fi

# Add FVWM2:
if [ -r /var/X11R6/lib/xinit/xinitrc.fvwm2 ]; then
  echo "\"xinitrc.fvwm2\" \"F(?) Virtual Window Manager (version 2.xx)\" \"off\" \\" >> $TMP/tmpscript.sh
fi

# Add FVWM95:
if [ -r /var/X11R6/lib/xinit/xinitrc.fvwm95 ]; then
  echo "\"xinitrc.fvwm95\" \"FVWM2 with a Windows look and feel\" \"off\" \\" >> $TMP/tmpscript.sh
fi

# Add icewm:
if [ -r /var/X11R6/lib/xinit/xinitrc.icewm ]; then
  echo "\"xinitrc.icewm\" \"ICE Window Manager\" \"off\" \\" >> $TMP/tmpscript.sh
fi

# Add twm:
if [ -r /var/X11R6/lib/xinit/xinitrc.twm ]; then
  echo "\"xinitrc.twm\" \"Tab Window Manager (very basic)\" \"off\" \\" >> $TMP/tmpscript.sh
fi

# Add mwm:
if [ -r /var/X11R6/lib/xinit/xinitrc.mwm ]; then
  echo "\"xinitrc.mwm\" \"M*tif WM based on FVWM (broken)\" \"off\" \\" >> $TMP/tmpscript.sh
fi

# Now, add support for the other window managers:
( cd /var/X11R6/lib/xinit
  for file in xinitrc.* ; do
    if [ ! "$file" = "xinitrc.kde" -a ! "$file" = "xinitrc.gnome" \
      -a ! "$file" = "xinitrc.e" -a ! "$file" = "xinitrc.wmaker" \
      -a ! "$file" = "xinitrc.fvwm2" -a ! "$file" = "xinitrc.fvwm95" \
      -a ! "$file" = "xinitrc.icewm" -a ! "$file" = "xinitrc.twm" \
      -a ! "$file" = "xinitrc.mwm" ]; then
      echo "\"$file\" \"$file\" \"off\" \\" >> $TMP/tmpscript.sh
    fi
  done
)

# Then, the tail end:
cat << EOF >> $TMP/tmpscript.sh
2> $TMP/output
if [ \$? = 1 -o \$? = 255 ]; then
  rm -f $TMP/output
  exit
fi
EOF

sh $TMP/tmpscript.sh

if [ ! -r $TMP/output ]; then
  rm -f $TMP/tmpscript.sh
  exit
fi

OUTPUT=`cat $TMP/output`

if [ "$USER" = "root" ]; then # set system-wide default:
  if [ -r /var/X11R6/lib/xinit/$OUTPUT ]; then
    ( cd /var/X11R6/lib/xinit ; ln -sf $OUTPUT xinitrc )
  fi
else # set a default for the non-root user:
  if [ -r /var/X11R6/lib/xinit/$OUTPUT ]; then
    if [ -r $HOME/.xinitrc ]; then
      mv $HOME/.xinitrc $HOME/.xinitrc-backup
    fi
    cat /var/X11R6/lib/xinit/$OUTPUT > $HOME/.xinitrc
  fi
  dialog --title "\$HOME/.xinitrc written" --msgbox "Your default \
  window manager has been selected.  /var/X11R6/lib/xinit/$OUTPUT \
  has been copied to your \$HOME/.xinitrc." 7 50
fi

rm -f $TMP/tmpscript.sh $TMP/output

