#!/usr/bin/perl
# This script will download and install the latest wine compatible version
# of the IMVUClient

# Be strict to avoid messy code 
use strict; 

# Use FindBin module to get script directory 
use FindBin; 

# Get script directory 
my $cwd = $FindBin::Bin; 

#fallback client url incase the check for new wine compatible client fails
my $fallback_client_url = "http://static-akm.imvu.com/imvufiles/installers/InstallIMVU_453.8.exe";

main();

sub main 
{
	# If --force argument is not provided 
    if ($ARGV[0] !~ '--force') 
    { 
		#tell what we will do
		print "--force not applied!\nChecking for compatible wine version.\n\n";
		
		#run a check for compatible wine version
		checkwine();
    }
    
    #try to get the latest wine compatible imvu client url
    my $url = get_latest_wine_compatible_client_url();
    
    #finnish off the installation by integrating imvu with linux by adding the url-handler protocol
	integrate_with_linux();
    
    #download and install through xterm and then remove the installation file
    system 'xterm -e "echo Downloading latest wine compatible version of IMVUClient && wget -O'.$cwd.'/InstallIMVU.exe '.$url.' && clear && echo Installing IMVUClient using wine && '."wine $cwd/InstallIMVU.exe".' && clear && echo Please press enter when the install is done && read i && '."rm $cwd/InstallIMVU.exe\"";
	
}

sub checkwine
{
	# get the wine version
	my $wineversion = `wine --version`;
	
	
	if ($wineversion eq m/(wine-1.[01].*|[com])/)
	{
		die "Wine version too old, you need wine-1.2 or newer\nBut you can try at your own risk\nby starting this script with --force at the end\n\n";
	}
	
}

sub integrate_with_linux
{
	#get some variables we need for the integration
	my $HOME = $ENV{"HOME"};
	#get a variable from wine which we only need the last part of
	my $APPDATA = `wine cmd /c echo "%APPDATA%"`;
	
	#convert the appdata variable to unix path
	$APPDATA =~ s/(C:\\|\\)/\//g;
	
	#remove some stupid newlines in the variable(thanks alot windows!)
	$APPDATA =~ s/(\r\n|\r|\n)//g;
	
	#add imvu protocol to the url-hanlder in linux (xdg-open)
	system "gconftool-2 --set --type=string /desktop/gnome/url-handlers/imvu/command 'wine $HOME/.wine/drive_c$APPDATA/IMVUClient/IMVUClient.exe \"%s\"'";
	system "gconftool-2 --set --type=bool /desktop/gnome/url-handlers/imvu/enabled true";
	system "gconftool-2 --set --type=bool /desktop/gnome/url-handlers/imvu/need-terminal false";
	
	#now add the above commands to a script file $HOME/.local/bin/imvu-url-handler
	system "mkdir -p $HOME/.local/bin";
	system 'echo "#!/bin/sh" > $HOME/.local/bin/imvu-url-protocol && echo gconftool-2 --set --type=string /desktop/gnome/url-handlers/imvu/command \\\'wine $HOME/.wine/drive_c$APPDATA/IMVUClient/IMVUClient.exe \\"%s\\"\\\' >> $HOME/.local/bin/imvu-url-protocol && echo gconftool-2 --set --type=bool /desktop/gnome/url-handlers/imvu/enabled true >> $HOME/.local/bin/imvu-url-protocol && echo gconftool-2 --set --type=bool /desktop/gnome/url-handlers/imvu/need-terminal false >> $HOME/.local/bin/imvu-url-protocol && chmod 755 $HOME/.local/bin/imvu-url-protocol';
	
	#now add the desktop file to add the above script to be executed at login(because gconf clears the setting at logout for some reason)
	system "echo [Desktop Entry] > $HOME/.config/autostart/imvu-protocol.desktop && echo Type=Application >> $HOME/.config/autostart/imvu-protocol.desktop && echo Exec=$HOME/.local/bin/imvu-url-protocol >> $HOME/.config/autostart/imvu-protocol.desktop && echo Name=Imvu Protocol Integration >> $HOME/.config/autostart/imvu-protocol.desktop && echo Terminal=false >> $HOME/.config/autostart/imvu-protocol.desktop && echo GenericName=Imvu Protocol Integration >> $HOME/.config/autostart/imvu-protocol.desktop && echo StartupNotify=false >> $HOME/.config/autostart/imvu-protocol.desktop";
}

sub get_latest_wine_compatible_client_url
{
	#url to the latest wine compatible client (link to latest wine compatible client is hosted on my dropbox account)
	my $url = `wget -qO- http://dl.dropbox.com/u/11631899/imvu4linux/latest_imvu_wine.txt`;
	
	#remove any newlines from the url
	$url =~ s/(\r\n|\r|\n)//g;
	
	#if we failed to get the url(due to some firewall) we will use a fallback client url
	if ($url eq '')
	{
		#use fallback client url
		$url = "$fallback_client_url";
		
		#tell that we could not get the latest url and will use a fallback url
		print "Failed to fetch the url to the latest wine compatible client, we will instead use this download url:\n$fallback_client_url\n\n";
	}
	
	#return download url
	return $url
}
