#!/usr/bin/perl -w

# Be strict to avoid messy code
use strict;

# Use FindBin module to get script directory
use FindBin;

# Get script directory
my $cwd = $FindBin::Bin;
# Get the homefolder 
my $homedir = $ENV{"HOME"};
# Detect the current OS
my $OS = "$^O";

# Kill script if we run Windows, since this script is
# made for unix platforms like linux, mac, bsd and solaris
die "Installing desktop icons works only on Linux, BSD, MacOSX and Solaris platforms
I suggest you download the runescape client for your platform if you
want it in the StartMenu.\n"
if $OS =~ /(MSWin32)/;
# End of the unsupported platform check

# Check if we are root 
my $isroot = `whoami`;

# Add the rsu module paths (so we can use parts of the api if we need to)
unshift @INC, "$cwd/framework/API";
unshift @INC, "$cwd/framework/modules";
unshift @INC, "$cwd/framework/Perl5lib";

# Settings Start
my $settings = {
	# Name of the uninstaller
	uninstaller => "uninstall-desktop-icons",
	};

# If we are on mac OSX
if ($OS =~ /(darwin)/)
{
	# Run the make_osx_app function
	make_osx_app();
}
# Else we are on other unix platforms
else
{
	# Run install_desktop_files function
	install_desktop_files();
	install_jagex_protocol();
}


#
#---------------------------------------- *** ----------------------------------------
#

sub install_desktop_files
{
	# Make a variable which indicates if Wx is installed
	my $wxloaded = 1;

	# Test to see if Wx can be used and set $wxloaded to 0 if we cannot use Wx
	eval "use Wx"; $wxloaded = 0 if $@;
	
	# Make the folder we are going to place the files inside
	system "mkdir -p $homedir/.local/share/applications";
	
	# Prepare an uninstall header
	system "echo \"#!/bin/sh\" > /tmp/$settings->{uninstaller}";
	
	# Find the desktop file for symlinking
    my $desktopfiles = `find $cwd/templates/launchers/ -name "*.conf"`;
    
    # Split the list by newlines
    my @desktopfiles = split(/\n/, $desktopfiles);
    
    # Make a counter for the desktop files
    my $desktopcounter = 0;
    
	# For each value in the array
	foreach(@desktopfiles)
	{
		# Get the file content
		my $content = `cat $desktopfiles[$desktopcounter]`;
		
		# Replace Icon= with Icon={PATH TO ICONS}
		$content =~ s/Icon=/Icon=$cwd\/share\/img\//;
			
		# Replace Exec= with Exec={PATH TO BINARY}
		$content =~ s/Exec=/Exec=$cwd\//;
			
		# Fix parts using xterm
		$content =~ s/Exec=$cwd\/xterm/Exec=xterm/;

		# Edit the desktop file correctly for system install
		if ($isroot !~ /root/){	
			$content =~ s/update-runescape-client/$cwd\/update-runescape-client/;
		}
		
		# Get the filename
		my $filename = $desktopfiles[$desktopcounter];
		my @filename = split(/\//, $filename);
		@filename = split(/\./, $filename[-1]);
		
		# Write a temp file
		system("echo \"$content\" > /tmp/$filename[0].desktop");
		
		# Move the temp file to its proper location and make it executable
		if ($isroot =~ /root/)
		{
			# Move the desktop file to the system applications folder
			system("mv /tmp/$filename[0].desktop /usr/share/applications/ && chmod 755 /usr/share/applications/$filename[0].desktop && echo sudo rm -fv /usr/share/applications/$filename[0].desktop >> /tmp/$settings->{uninstaller}");
		}
		# Else
		else
		{
			# Move the desktop file to the local applications folder
			system("mv /tmp/$filename[0].desktop $homedir/.local/share/applications/ && chmod 755 $homedir/.local/share/applications/$filename[0].desktop && echo rm -fv $homedir/.local/share/applications/$filename[0].desktop >> /tmp/$settings->{uninstaller}");
		}
		
		# Increase the counter by 1
		$desktopcounter += 1;
	}
	
	# If Wx is able to be loaded
	if ($wxloaded eq '1')
	{
		if ($isroot =~ /root/)
		{
			# Hide the non-wx entries (as the launcher can launch those since we got Wx installed)
			system 'sed -i -e "s/NoDisplay=false/NoDisplay=true/" /usr/share/applications/runescape-oldschool.desktop';
			system 'sed -i -e "s/NoDisplay=false/NoDisplay=true/" /usr/share/applications/runescape-update-client.desktop';
		}
		else
		{
			# Hide the non-wx entries (as the launcher can launch those since we got Wx installed)
			system 'sed -i -e "s/NoDisplay=false/NoDisplay=true/" '.$homedir.'/.local/share/applications/runescape-oldschool.desktop';
			system 'sed -i -e "s/NoDisplay=false/NoDisplay=true/" '.$homedir.'/.local/share/applications/runescape-update-client.desktop';
		}
	}
	
	# Copy the uninstallscript to $cwd and make it executable
	system("mv /tmp/$settings->{uninstaller} \"$cwd/\" && chmod 755 \"$cwd/$settings->{uninstaller}\"");
}

sub make_osx_app
{
	# Download the rsu-query binary for MacOSX
	fetchmacbin();
	
	# Tell user what we are doing
	print "Making application directory.\n";
	
	# Make the client directory
	system "mkdir -v -p /Applications/RuneScape_Unix/";
	
	# Tell user what we are doing
	print "\nCopying the template to the directory.\n";
	
	# Copy the template
	system "cp -R -v \"$cwd/templates/darwin/RuneScape.app\" /Applications/RuneScape_Unix/";
	
	# Tell user what we are doing
	print "\nCopying client files into the template.\n";
	
	# Copy the client files to the files directory 
	system "cp -R -v \"$cwd/\"* /Applications/RuneScape_Unix/RuneScape.app/Contents/Resources/";
	
	# Tell user what we are doing
	print "\nMaking symlinks to the runescape script and RSU icon\n";
	
	# Make symlinks to the RSU icon and the runescape script
	system "cd /Applications/RuneScape_Unix/RuneScape.app/Contents/Resources/ && ln -s rsu/rsu-query script && ln -s share/img/runescape.icns runescape.icns";
	
	# Tell user what we are doing
	print "\nCreating a symlink to Resource folder so that the client files are easily accessable.\n";
	
	# Symlink the client files to the launcher
	system "cd /Applications/RuneScape_Unix/ && ln -v -s RuneScape.app/Contents/Resources ./";
	
	# Tell user what we are doing
	print "\nCreating symlink to the rsu-settings script\n";
	
	# Symlink the rsu-settings script to the RuneScape_Unix folder
	system "cd /Applications/RuneScape_Unix/ && ln -s RuneScape.app/Contents/Resources/rsu-settings ./";
	
	# Tell user we are done
	print "\n\nApplication launcher created and can be run from /Applications/RuneScape_Unix/\n";
}

#
#---------------------------------------- *** ----------------------------------------
#

sub fetchmacbin
{
	# Download and extract the rsu-query binary
	system "$cwd/rsu/bin/rsu-query-unix install_binary";
}

#
#---------------------------------------- *** ----------------------------------------
#

sub install_jagex_protocol
{
	# Launch the protocol integration script inside the terminal
	system ("x-terminal-emulator -e \"perl $cwd/templates/protocol/install-jagex-protocol-linux\"");
}

#
#---------------------------------------- *** ----------------------------------------
#

