#!/bin/sh
#firstly, JM's code to setup #puppylinux at IRC, then launch pidgin.
#########################
# gaim-autosetup.sh -- generates autologin configuration for GAIM 
#                      so that starting GAIM will log user into #puppylinux
#
# Copyright 2006 Jonathan Marsden, GNU General Public License version 2
# FSF Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
# bugfix bk 2006. Modified for Pidgin 2008.

BACKTITLE="Welcome, an account will be created automatically for
you in the #puppylinux channel at irc.freenode.net.
Type the nickname that you would like to be known by.
The entry already in the box has been autogenerated."
IRCHOST='irc.freenode.net'
IRCCHANNEL='#puppylinux'
IRCPORT=6667
PREFIX='bionicpup64-'
PURPLECONFIGDIR=~/.purple
PROMPT="Enter the IRC Nickname you wish to use,
as unique as possible to avoid conflict
with already-registered names:"

# Exit if either of the two files we are generating already exists
[ -f $PURPLECONFIGDIR/accounts.xml ] && exec pidgin $@
[ -f $PURPLECONFIGDIR/blist.xml ] && exec pidgin $@

# Generate a semi-random IRC username
USERNAME=$PREFIX`(date ;cat /proc/cpuinfo)|md5sum|sed -e's/^\(......\).*$/\1/'`
NEWNAME=`Xdialog --backtitle "$BACKTITLE" --stdout --no-cancel --title "Pidgin startup" --inputbox "$PROMPT" 0 0 "$USERNAME"`
[ $? -ne 0 ] && exec pidgin $@
[ "$NEWNAME" = "" ] && exec pidgin $@
USERNAME=`echo "$NEWNAME" |tr -cd 'A-Za-z0-9[-\`{-}'` # See RFC 2812 2.3.1

# Create the two config files
cat >$PURPLECONFIGDIR/accounts.xml <<EOF
<?xml version='1.0' encoding='UTF-8' ?>
<account version='1.0'>
	<account>
		<protocol>prpl-irc</protocol>
		<name>$USERNAME@$IRCHOST</name>
		<statuses>
			<status type='available' name='Available' active='true'>
				<attributes/>
			</status>
			<status type='away' name='Away' active='false'>
				<attributes/>
			</status>
			<status type='offline' name='Offline' active='false'>
				<attributes/>
			</status>
		</statuses>
		<settings>
			<setting name='realname' type='string'>$USERNAME</setting>
			<setting name='username' type='string'>$USERNAME</setting>
			<setting name='port' type='int'>6667</setting>
			<setting name='ssl' type='bool'>0</setting>
			<setting name='encoding' type='string'>UTF-8</setting>
			<setting name='autodetect_utf8' type='bool'>0</setting>
		</settings>
		<settings ui='gtk-gaim'>
			<setting name='auto-login' type='bool'>1</setting>
		</settings>
		<current_error/>
	</account>
</account>

EOF

cat >$PURPLECONFIGDIR/blist.xml <<EOF
<?xml version='1.0' encoding='UTF-8' ?>

<purple version='1.0'>
	<blist>
		<group name='Chats'>
			<setting name='collapsed' type='bool'>0</setting>
			<chat proto='prpl-irc' account='$USERNAME@$IRCHOST'>
				<component name='channel'>#puppylinux</component>
				<setting name="gtk-autojoin" type="bool">1</setting>
			</chat>
		</group>
	</blist>
	<privacy>
		<account proto='prpl-irc' name='$USERNAME@$IRCHOST' mode='1'/>
	</privacy>
</purple>
       
EOF

##############################
#now run pidgin...
exec pidgin $@
###END###
