#!/bin/sh

cd "$(dirname "$0")"

if pwd | grep " ";
  then alert "Please install from a directory with no spaces in the path name, the current one (`pwd`) has spaces.  Just move all the files to somewhere that doesn't have spaces, like /boot/home/mydirectory.  If there are spaces, the install script malfunctions.  Sorry.";
  exit 1;
fi

RETURN=`alert "This will install the IM Kit in /boot/apps/im_kit. Previous versions of the kit will be deleted." "Continue" "Cancel"`

if [[ $RETURN = Cancel ]]
then
	exit -1;
fi

# quit kit if running


quit "application/x-vnd.beclan.im_kit"
quit "application/x-vnd.m_eiman.im_logger"
quit "application/x-vnd.m_eiman.sample_im_client"
quit "application/x-vnd.xeD.im_emoclient"
quit "application/x-vnd.BeClan.im_binlogger"
quit "application/x-vnd.BeClan.im_binlog_viewer"
quit "application/x-vnd.beclan.IM_InfoPopper"
quit "application/x-vnd.beclan.im_kit-DeskbarIcon"
quit "application/x-vnd.BeClan.IMKit.IconSetter"
quit "application/x-vnd.beclan-IMKitPrefs"



# remove old im kit parts
query -a 'BEOS:APP_SIG == "application/x-vnd.beclan.im_kit" || BEOS:APP_SIG == "application/x-vnd.m_eiman.im_logger" || BEOS:APP_SIG == "application/x-vnd.m_eiman.sample_im_client" || BEOS:APP_SIG == "application/x-vnd.xeD.im_emoclient" || BEOS:APP_SIG == "application/x-vnd.BeClan.im_binlogger" || BEOS:APP_SIG == "application/x-vnd.BeClan.im_binlog_viewer" || BEOS:APP_SIG == "application/x-vnd.beclan.IM_InfoPopper" || BEOS:APP_SIG == "application/x-vnd.beclan.im_kit-DeskbarIcon" || BEOS:APP_SIG == "application/x-vnd.BeClan.IMKit.IconSetter" || BEOS:APP_SIG == "application/x-vnd.beclan-IMKitPrefs"'  | grep -v "`/bin/pwd`" | xargs rm -f

rm ~/config/lib/libim.so

# this is due to a bug that causes the Deskbar icon loading to fail, it's an app_sig thing.
rm ~/config/settings/beos_mime/application/x-vnd.beclan.im_kit

# create indexes
/bin/mkindex -t string IM:connections
/bin/mkindex -t string IM:status

# add attributes to application/x-person
./bin/mimetype_attribute --mime application/x-person --internal-name "IM:status" --public-name "IM Status" --type string --width 80 --viewable --public --not-editable
./bin/mimetype_attribute --mime application/x-person --internal-name "IM:connections" --public-name "IM Connections" --type string --width 80 --viewable --public

# create add-ons and settings dir
mkdir -p ~/config/settings/im_kit/icons
mkdir -p ~/config/settings/im_kit/clients
mkdir -p ~/config/settings/im_kit/add-ons/protocols
mkdir -p ~/config/add-ons/im_kit/protocols

# install apps
mkdir -p /boot/home/config/servers
copyattr -d -m bin/im_server /boot/home/config/servers/im_server
mkdir -p /boot/apps/im_kit
copyattr -d -m bin/clients/im_send_message /boot/apps/im_kit/im_send_message
copyattr -d -m bin/clients/im_client /boot/apps/im_kit/im_client
copyattr -d -m bin/clients/im_emoclient /boot/apps/im_kit/im_emoclient
copyattr -d -m bin/clients/im_logger /boot/apps/im_kit/im_logger
copyattr -d -m bin/clients/im_binlogger /boot/apps/im_kit/im_binlogger
copyattr -d -m bin/clients/im_binlog_viewer /boot/apps/im_kit/im_binlog_viewer
copyattr -d -m bin/clients/im_infopopper /boot/apps/im_kit/im_infopopper
mkdir -p /boot/apps/im_kit/utils
copyattr -d -m bin/IconSetter /boot/apps/im_kit/utils/IconSetter
copyattr -d -m bin/IMDeskbarIcon /boot/apps/im_kit/utils/IMDeskbarIcon
copyattr -d -m bin/mimetype_attribute /boot/apps/im_kit/utils/mimetype_attribute




# install sounds
mkdir -p /boot/apps/im_kit/sounds
copyattr -d -m sounds/Connect.wav /boot/apps/im_kit/sounds/Connect.wav
copyattr -d -m sounds/Disconnect.wav /boot/apps/im_kit/sounds/Disconnect.wav
copyattr -d -m sounds/Message.wav /boot/apps/im_kit/sounds/Message.wav
copyattr -d -m sounds/StatusAvailable.wav /boot/apps/im_kit/sounds/StatusAvailable.wav
copyattr -d -m sounds/StatusAway.wav /boot/apps/im_kit/sounds/StatusAway.wav
copyattr -d -m sounds/StatusOffline.wav /boot/apps/im_kit/sounds/StatusOffline.wav

# install libs, add-ons, settings
copyattr -d -m bin/lib/libim.so ~/config/lib/libim.so
if [ ! -e ~/config/lib/libbsvg.so ]; then
	copyattr -d -m bin/lib/libbsvg.so ~/config/lib/libbsvg.so
fi
copyattr -d -m bin/lib/libexpat.so ~/config/lib/libexpat.so
copyattr -d -m bin/protocols/aim ~/config/add-ons/im_kit/protocols/aim
copyattr -d -m bin/protocols/gtalk ~/config/add-ons/im_kit/protocols/gtalk
copyattr -d -m bin/protocols/jabber ~/config/add-ons/im_kit/protocols/jabber
copyattr -d -m bin/protocols/msn ~/config/add-ons/im_kit/protocols/msn


copyattr -d -m bin/settings/InstantMessaging /boot/apps/im_kit/InstantMessaging
ln -s /boot/apps/im_kit/InstantMessaging ~/config/be/Preferences/InstantMessaging

# install icon-holding person files
unzip -n bin/Icons.zip -d /boot/home/config/settings/im_kit/icons
unzip -n bin/smileys.zip -d /boot/home/config/settings/im_kit/
rm bin/Icons.zip
rm bin/smileys.zip

# install Tracker add-ons
copyattr -d -m bin/tracker-addons/IM_Merge_contacts ~/config/add-ons/Tracker/IM_Merge_contacts
ln -s /boot/apps/im_kit/im_client /boot/home/config/add-ons/Tracker/im_client

# install query to desktop
copyattr -d -m bin/Online_contacts ~/Desktop/Online_contacts

# start im kit
/boot/home/config/servers/im_server &
sleep 2
/boot/apps/im_kit/im_client &
/boot/apps/im_kit/im_emoclient &
/boot/apps/im_kit/im_logger &
/boot/apps/im_kit/im_binlogger &
/boot/apps/im_kit/im_binlogger_viewer &
/boot/apps/im_kit/im_infopopper &

# remove traces
rm Install.sh
rm -rf bin
rm -rf sounds

# done
alert "All done. Click OK to view docs!"

# open docs
cd docs
/system/Tracker index.html
