#! /bin/sh
# (c) 1998 Marcelo Magallon <mmagallo@debian.org>
# this script is distributed under the terms and conditions of the GPL.
#
# TODO:
#
# * Fix that ugly hack with fix*
#   Is there something in the shell like Perl's hashes? It could be nice to
#   do something like "foreach $var (keys %fixes)" to associate fixes with
#   file names so I don't have to track things all over the place
#
# * Fix also the code that iterates over user directories to include system
#   directories in one run  
#
# upgrade-windowmaker-defaults (0.3)
# 
# * Handles upgrade to Window Maker 0.19.0
#   Logo.Clip -> Tile.Clip (crashing)
#
# upgrade-windowmaker-defaults (0.2)
# 
# * Handles keyname changes between 0.16.1 and 0.17.2
#
# Tue Jul 21 08:05:00 CST 1998
#
# upgrade-windowmaker-defaults (0.1)
# 
# * Initial release
# * Handles WindowPlaceOrigin syntax change
# * Handles name change Fiend -> Clip
#
# Sat Jun 13 16:18:36 CST 1998

# Fixes (these are not-optional changes)
# this one changes WindowPlaceOrigin = "..." to WindowPlaceOrigin = (...)
fix1='s/\(.*WindowPlaceOrigin = \)"\(.*\)";/\1(\2);/'
# and this one substitutes Fiend with Clip
fix2='s/\(.*\)Fiend\(.*\)=/\1Clip\2=/'
# this one applies to WMState
fix3='s/\( *\)Fiend\( *\)=/\1Clip\2=/'
fix4='s/\(.*\)Logo\.\(WMFiend\|WMClip\)\(.*\)=/\1Tile.WMClip\3=/'
# this one applies to Window Maker
fix5='s/\(.*\)NoSound\(.*\)=/\1DisableSound\2=/'
fix6='s/\(.*\)NoAutoWarp\(.*\)=/\1DontLinkWorspaces\2=/'
# this one is for WMWindowAttributes

# try to screen system accounts in the /etc/passwd file. If somebody
# has a better method for doing this, I'm open to suggestions. Please
# note that Debian Policy states accounts 0-99 are reserved for the
# Project, and 100 onwards *could* be used by the local sysadmin, but
# the default is 1000 and up
users='^[[:alnum:]]*:[[:alnum:]]*:[[:digit:]]\{4,\}:'

ask_n ()
{
  echo -n $*'? [yN] '
  read yn
  test -n "$yn" || yn=n
  case "$yn" in
    [yY]*)
      return 1
    ;;
    *)
      return 0
    ;;
  esac
}

apply_fix ()
{
  file_to_fix=$1; shift
  if [ -e $file_to_fix ] ; then
    echo -n "Fixing $file_to_fix... "
    while [ $# -gt 0 ] ; do
      sed -e "$1" $file_to_fix > $tempfile
      cat $tempfile > $file_to_fix
      shift
    done
    echo done.
  fi
}

ask_permission ()
{
  cat <<EOF
I can try to fix certain configuration parameters that have changed
between previous versions of Window Maker and this one, namely:

 * WindowPlaceOrigin syntax change from "..." to (...)
 * Name change of Fiend to Clip
 * WMWindowAttributes: Logo.Clip -> Tile.Clip (crashing)

NOT fixing this could prevent Window Maker from starting. Please read
/usr/doc/wmaker/NEWS.gz and /usr/doc/wmaker/changelog.gz

I will fix *both* the system defaults and each user's files.

EOF
  if ! ask_n "Do you want to proceed with the changes" ; then
    return 0
  else
    return 1
  fi
}

set -e

gs_dir=GNUstep
[ $GNUSTEP_USER_ROOT ] && gs_dir=$GNUSTEP_USER_ROOT 
gs_defaults=$gs_dir/Defaults
gs_system_defaults=/etc/GNUstep/Defaults

if [ "$1" = "--non-interactive" ] || ask_permission ; then
  tempfile=`tempfile`

  # fix users' files
  for dir in `cut -d : -f 6 /etc/passwd | sort -u` ; do
    apply_fix $dir/$gs_defaults/WindowMaker "$fix1" "$fix2" "$fix5" "$fix6"
    apply_fix $dir/$gs_defaults/WMState "$fix3"
    apply_fix $dir/$gs_defaults/WMWindowAttributes "$fix4"
  done

  # fix system files
  apply_fix $gs_system_defaults/WindowMaker "$fix1" "$fix2" "$fix5" "$fix6"
  apply_fix $gs_system_defaults/WMState "$fix3"
  apply_fix $gs_system_defaults/WMWindowAttributes "$fix4"

  rm $tempfile

cat <<EOF

Done fixing things. If you want to run this script again you can do so by
typing:

$ $0
EOF

else
cat <<EOF

Ok, leaving things as they are now... you can run this script again using:

$ $0
EOF
fi

cat <<EOF

Press [ENTER] to continue...
EOF

read dummy
exit 0
