#!/bin/sh

#    Copyright (c) 2006 David Faure <faure@kde.org>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.


# Lets the user choose between kmail and kontact as the Kolab IMAP resource backend
# To be used with KDEPIM 3.5 or proko2.
#
# Simply run this script, first without arguments to check your current setup, 
# and then with either kmail or kontact as argument.

if ! kde-config ; then
  echo 'kde-config not found! Aborting. Please fix your $PATH [or install kde?].'
  exit 1
fi

if test $# -gt 1 -o "$1" = "--help"; then
  echo "Usage: $0 [kmail|kontact]"
  exit 2
fi

apps=`kde-config --path xdgdata-apps`

kmail=
kontact=
IFS=:
for dir in $apps; do
  dir=`echo $dir | sed -e 's,/$,,'`
  test -z "$kmail" -a -f "$dir/kde/KMail.desktop" && kmail="$dir/kde/KMail.desktop"
  test -z "$kontact" -a -f "$dir/kde/kontactdcop.desktop" && kontact="$dir/kde/kontactdcop.desktop"
done

if test -z "$kmail" -a -z "$kontact"; then
  echo "Neither KMail.desktop not kontactdcop.desktop was found! Please check $apps"
  exit 3
fi

kmail_is_backend=
kontact_is_backend=
if test -n "$kmail"; then
  kmail_is_backend=`grep DCOP/ResourceBackend/IMAP "$kmail"`
fi
if test -n "$kontact"; then
  kontact_is_backend=`grep DCOP/ResourceBackend/IMAP "$kontact"`
fi

if test -n "$kmail_is_backend" -a -n "$kontact_is_backend"; then
  echo "WARNING: both kmail and kontact are set as imap backends"
fi
if test -z "$kmail_is_backend" -a -z "$kontact_is_backend"; then
  echo "WARNING: neither kmail and kontact are set as imap backends"
fi
currentbackend=kmail
test -n "$kontact_is_backend" && currentbackend=kontact

echo "Current backend: $currentbackend"

test $# -eq 0 && exit 0

wanted="$1"
if test "$wanted" = "$currentbackend"; then
  echo "OK, nothing to do."
  exit 0
fi

if test "$wanted" = kmail; then

  if test -z "$kmail"; then
    echo "ERROR: KMail.desktop wasn't found"
    exit 4
  fi
  if test -n "$kontact"; then
    echo "Deleting $kontact..."
    rm -f "$kontact"
  fi
  echo "Adjusting $kmail..."
  if grep -q ^ServiceTypes "$kmail"; then
    echo "ERROR: $kmail has a ServiceTypes line already"
    exit 5
  fi
  echo 'ServiceTypes=DCOP/ResourceBackend/IMAP,DCOP/Mailer' >> "$kmail"
  echo "OK, kmail is set as the IMAP backend now."
  exit 0

elif test "$wanted" = kontact; then

  if test -n "$kontact"; then
    echo "ERROR: kontactdcop.desktop exists already"
    exit 6
  fi
  
  kontact=`echo $kmail | sed -e 's/KMail.desktop$/kontactdcop.desktop/'`
  echo "Creating $kontact..."
# Note that proko2 adds --nosplash to kontact, but it's not in 3.5 so I'm not using it here
  cat > "$kontact" <<EOF
[Desktop Entry]
Encoding=UTF-8
Name=Kontact
Exec=kontact --iconify
Type=Application
Icon=kontact
NoDisplay=true
X-DCOP-ServiceType=Unique
X-DCOP-ServiceName=kontact
ServiceTypes=DCOP/ResourceBackend/IMAP,DCOP/Mailer
X-KDE-StartupNotify=false
Categories=Qt;KDE;Office
EOF

  if test -n "$kmail"; then
    echo "Adjusting $kmail..."
    TMPFILE=`mktemp` || exit 7
    grep -v ^ServiceTypes "$kmail" > $TMPFILE
    mv -f $TMPFILE "$kmail"
  fi

  echo "OK, kontact is set as the IMAP backend now."

else

  echo "ERROR: you must select either kmail or kontact."
  exit 10

fi

# notreached
exit 42
