# File:		Makefile
# Purpose:	Project builder
#
# Author:	Copyright 2006-2010 Xavion
#
# License:	This file is part of Q7Z.
#
#			Q7Z is free software: you can redistribute it and/or modify
#			it under the terms of the GNU Lesser General Public License as published by
#			the Free Software Foundation, either version 3 of the License, or
#			(at your option) any later version.
#
#			Q7Z 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 Lesser General Public License for more details.
#
#			You should have received a copy of the GNU Lesser General Public License
#			along with Q7Z.  If not, see <http://www.gnu.org/licenses/>.


# Defines

# Shell
#SHELL = /bin/sh

# Application
App-Name = Q7Z
App-FileName = q7z
App-Prefix = /share/$(App-Name)

# User
ifndef USER
	USER = $(LOGNAME)
endif

# Home
ifndef SUDO_USER
	#ifndef HOME
		#HOME = ~$(USER)
		HOME = /home/$(USER)
	#endif
endif

# Packagers
AutoPackage = $(shell which package 2> /dev/null)
ifeq "$(AutoPackage)" ""
	AutoPackage = $(shell whereis -b package | cut -s -f2 -d' ')
endif
PacMan = $(shell which pacman 2> /dev/null)
ifeq "$(PacMan)" ""
	PacMan = $(shell whereis -b pacman | cut -s -f2 -d' ')
endif
Dpkg = $(shell which dpkg 2> /dev/null)
ifeq "$(Dpkg)" ""
	Dpkg = $(shell whereis -b dpkg | cut -s -f2 -d' ')
endif
RPM = $(shell which rpm 2> /dev/null)
ifeq "$(RPM)" ""
	RPM = $(shell whereis -b rpm | cut -s -f2 -d' ')
endif
RemovePkg = $(shell which removepkg 2> /dev/null)
ifeq "$(RemovePkg)" ""
	RemovePkg = $(shell whereis -b removepkg | cut -s -f2 -d' ')
endif

# Python
Python3 = $(shell which python3 2> /dev/null)
Python3-Ver = $(word 2,$(shell $(Python3) --version 2>&1))

# PyQt
PyQt4-UIC = $(shell which pyuic4 2> /dev/null)
PyQt4-RCC = $(shell which pyrcc4 2> /dev/null)
PyQt4-Ver = $(word 5,$(shell $(PyQt4-UIC) --version))

# P7Zip
ifneq "$(shell which 7z 2> /dev/null)" ""
	P7Zip = $(shell which 7z 2> /dev/null)
else ifneq "$(shell which 7za 2> /dev/null)" ""
	P7Zip = $(shell which 7za 2> /dev/null)
else
	P7Zip = $(shell which 7zr 2> /dev/null)
endif
P7Zip-Ver = $(word 3,$(shell $(P7Zip) 2>&1 | grep "p7zip Version"))

# System
ifndef SysPrefix
	SysPrefix = /usr
endif

ifndef Sys-SBin
	#Sys-SBin = /sbin
	Sys-SBin = $(SysPrefix)/sbin
endif

ifndef Sys-Bin
	Sys-Bin = $(SysPrefix)/bin
endif

# KDE Configs
KDE-Config3 = $(shell which kde-config 2> /dev/null)
KDE-Config4 = $(shell which kde4-config 2> /dev/null)

# KDE 4
ifneq "$(KDE-Config4)" ""
	KDE_SESSION_VERSION = "4"
	KDE-Config = "$(KDE-Config4)"
	KDE-Prefix = $(shell $(KDE-Config) --prefix)
	KDE-LocalPrefix = $(shell $(KDE-Config) --localprefix)
	#Sys-Bin = $(shell $(KDE-Config) --qt-binaries)
else
	# KDE 3
	ifneq "$(KDE-Config3)" ""
		KDE_SESSION_VERSION = "3"
		KDE-Config = "$(KDE-Config3)"
		KDE-Prefix = $(shell $(KDE-Config) --prefix)
		KDE-LocalPrefix = $(shell $(KDE-Config) --localprefix)
	# Unknown
	else
		KDE_SESSION_VERSION = "?"
		KDE-Config = ""
		KDE-Prefix = /usr
		KDE-LocalPrefix = $(HOME)/.kde4
		#Sys-Bin = $(KDE-Prefix)/bin
	endif
endif

# Local
ifndef LocalPrefix
	LocalPrefix = $(HOME)/.local
endif

ifndef Local-SBin
	Local-SBin = $(LocalPrefix)/sbin
endif

ifndef Local-Bin
	Local-Bin = $(LocalPrefix)/bin
endif


# Aliases

all: echo clean interfaces resources permissions
	@echo ""
	@echo "Before installing $(App-Name), see the documentation for more info."
	@echo ""


echo:
	@echo ""
	@echo "Viewing the $(App-Name) environment and dependencies ..."
	@echo ""

	# Environment
	@echo "App-Name = $(App-Name)"
	@echo "USER = $(USER)"
	@echo "LOGNAME = $(LOGNAME)"
	@echo "HOME = $(HOME)"
	@echo "Python3 = $(Python3)"
	@echo "Python3-Ver = $(Python3-Ver)"
	@echo "PyQt4-UIC = $(PyQt4-UIC)"
	@echo "PyQt4-RCC = $(PyQt4-RCC)"
	@echo "PyQt4-Ver = $(PyQt4-Ver)"
	@echo "P7Zip = $(P7Zip)"
	@echo "P7Zip-Ver = $(P7Zip-Ver)"
	@echo "KDE_SESSION_VERSION = $(KDE_SESSION_VERSION)"
	@echo "KDE-Config = $(KDE-Config)"
	@echo "KDE-Prefix = $(KDE-Prefix)"
	@echo "KDE-LocalPrefix = $(KDE-LocalPrefix)"
	@echo "SysPrefix = $(SysPrefix)"
	@echo "Sys-SBin = $(Sys-SBin)"
	@echo "Sys-Bin = $(Sys-Bin)"
	@echo "LocalPrefix = $(LocalPrefix)"
	@echo "Local-SBin = $(Local-SBin)"
	@echo "Local-Bin = $(Local-Bin)"
	@echo ""

	# Dependencies
	$(if $(Python3),,$(error Error: The required 'Python v3' executable file can't be found; ensure it's installed and visible before building))
	$(if $(PyQt4-UIC),,$(error Error: The required 'PyQt4 UIC' executable file can't be found; ensure it's installed and visible before building))
	$(if $(PyQt4-RCC),,$(error Error: The required 'PyQt4 RCC' executable file can't be found; ensure it's installed and visible before building))
	$(if $(shell $(Python3) -c "import PyQt4" 2>&1 | grep "No module named PyQt4"),$(error Error: The required 'PyQt4' module can't be found; ensure it's attached to 'Python v3' before building),)
	$(if $(P7Zip),,$(error Error: The required 'P7Zip' executable file can't be found; ensure it's installed and visible before building))


clean:
	@echo ""
	@echo "Cleaning the $(App-Name) objects ..."
	@echo ""

	rm -f ../Image/*_rc.py
	rm -f ../Source/*_rc.py
	rm -f ../Source/Ui_*.py
	rm -f ../Source/*.pyc


made:
	@echo ""
	@echo "Checking for the $(App-Name) objects ..."
	@echo ""

	$(if $(wildcard ../Source/Ui_*.py),,$(error Error: The interfaces haven't been made; run 'make all' before installing))
	$(if $(wildcard ../Source/*_rc.py),,$(error Error: The resources haven't been made; run 'make all' before installing))


install: Install.AllUsers


install-user: Install.CurrentUser


uninstall: UnInstall.AllUsers


uninstall-user: UnInstall.CurrentUser


# Installers

Install.AllUsers :
	make made
	make uninstall
	#make clean
	#make echo

	@echo ""
	@echo "Installing $(App-Name) for all users ..."
	@echo "Only root can run 'make install'.  Others should instead run: 'make install-user'"
	@echo ""

	# KDE
	mkdir -p $(DESTDIR)$(SysPrefix)$(App-Prefix)/
	cp -r ../Build/ $(DESTDIR)$(SysPrefix)$(App-Prefix)/
	#cp -r ../Doc/ $(DESTDIR)$(SysPrefix)$(App-Prefix)/
	cp -r ../Desktop/ $(DESTDIR)$(SysPrefix)$(App-Prefix)/
	cp -r ../Image/ $(DESTDIR)$(SysPrefix)$(App-Prefix)/
	cp -r ../License/ $(DESTDIR)$(SysPrefix)$(App-Prefix)/
	cp -r ../Lists/ $(DESTDIR)$(SysPrefix)$(App-Prefix)/
	cp -r ../Profiles/ $(DESTDIR)$(SysPrefix)$(App-Prefix)/
	#install -m 777 -d $(DESTDIR)$(SysPrefix)$(App-Prefix)/Source/
	cp -r --preserve=mode ../Source/ $(DESTDIR)$(SysPrefix)$(App-Prefix)/
	cp -r ../Options/ $(DESTDIR)$(SysPrefix)$(App-Prefix)/
	cp -r ../Testing/ $(DESTDIR)$(SysPrefix)$(App-Prefix)/
	mkdir -p $(DESTDIR)$(Sys-Bin)/
	ln -fs $(SysPrefix)$(App-Prefix)/Source/$(App-Name).pyw $(DESTDIR)$(Sys-Bin)/
	#mkdir -p $(DESTDIR)$(SysPrefix)/share/apps/konqueror/servicemenus/
	mkdir -p $(DESTDIR)$(SysPrefix)/share/kde4/services/ServiceMenus/
	#cp ../Desktop/KDE/*.desktop $(DESTDIR)$(SysPrefix)/share/apps/konqueror/servicemenus/
	cp ../Desktop/KDE/*.desktop $(DESTDIR)$(SysPrefix)/share/kde4/services/ServiceMenus/
	mkdir -p $(DESTDIR)$(SysPrefix)/share/applications/
	cp ../Desktop/Menu/* $(DESTDIR)$(SysPrefix)/share/applications/
	mkdir -p $(DESTDIR)$(SysPrefix)/share/icons/hicolor/32x32/actions/
	cp ../Image/actions/ark* $(DESTDIR)$(SysPrefix)/share/icons/hicolor/32x32/actions/
	cp ../Image/actions/endturn* $(DESTDIR)$(SysPrefix)/share/icons/hicolor/32x32/actions/
	mkdir -p $(DESTDIR)$(SysPrefix)/share/icons/hicolor/32x32/apps/
	ln -fs $(SysPrefix)$(App-Prefix)/Image/apps/$(App-Name).png $(DESTDIR)$(SysPrefix)/share/icons/hicolor/32x32/apps/
	-rm -rf `find $(DESTDIR)$(SysPrefix)$(App-Prefix)/ -type d -name .svn`

	# Gnome
	#mkdir -p $(DESTDIR)$(HOME)/.gnome2/nautilus-scripts/Archiving/$(App-Name)/
	#cp -r ../Desktop/Gnome/* $(DESTDIR)$(HOME)/.gnome2/nautilus-scripts/Archiving/$(App-Name)/

	# Xfce
	#mkdir -p $(DESTDIR)$(HOME)/.config/Thunar/
	#if test -f $(DESTDIR)$(HOME)/.config/Thunar/uca.xml ; \
	#	then mv -f $(DESTDIR)$(HOME)/.config/Thunar/uca.xml $(DESTDIR)$(HOME)/.config/Thunar/uca.xml.orig ; \
	#fi
	#cp -r ../Desktop/Xfce/uca.xml $(DESTDIR)$(HOME)/.config/Thunar/uca.xml.$(App-Name)

	# Misc
	#rm -rf $(DESTDIR)$(HOME)/Profiles/

	@echo ""
	@echo "To run $(App-Name), enter: $(App-Name).pyw"
	@echo ""

Install.CurrentUser :
	make made
	make uninstall-user
	#make clean
	#make echo
	#make all

	@echo ""
	@echo "Installing $(App-Name) for the current user ..."
	@echo ""

	# KDE
	mkdir -p $(LocalPrefix)$(App-Prefix)/
	cp -r ../Build/ $(LocalPrefix)$(App-Prefix)/
	#cp -r ../Doc/ $(LocalPrefix)$(App-Prefix)/
	cp -r ../Desktop/ $(LocalPrefix)$(App-Prefix)/
	cp -r ../Image/ $(LocalPrefix)$(App-Prefix)/
	cp -r ../Lists/ $(LocalPrefix)$(App-Prefix)/
	cp -r ../License/ $(LocalPrefix)$(App-Prefix)/
	cp -r ../Profiles/ $(LocalPrefix)$(App-Prefix)/
	cp -r --preserve=mode ../Source/ $(LocalPrefix)$(App-Prefix)/
	cp -r ../Options/ $(LocalPrefix)$(App-Prefix)/
	cp -r ../Testing/ $(LocalPrefix)$(App-Prefix)/
	mkdir -p $(Local-Bin)/
	ln -fs $(LocalPrefix)$(App-Prefix)/Source/$(App-Name).pyw $(Local-Bin)/
	#mkdir -p $(KDE-LocalPrefix)/share/apps/konqueror/servicemenus/
	mkdir -p $(KDE-LocalPrefix)/share/kde4/services/ServiceMenus/
	#rm -rf $(HOME)/Profiles/
	#cp ../Desktop/KDE/*.desktop $(KDE-LocalPrefix)/share/apps/konqueror/servicemenus/
	cp ../Desktop/KDE/*.desktop $(KDE-LocalPrefix)/share/kde4/services/ServiceMenus/
	mkdir -p $(LocalPrefix)/share/applications/
	cp ../Desktop/Menu/* $(LocalPrefix)/share/applications/
	mkdir -p $(LocalPrefix)/share/icons/hicolor/32x32/actions/
	cp ../Image/actions/ark* $(LocalPrefix)/share/icons/hicolor/32x32/actions/
	cp ../Image/actions/endturn* $(LocalPrefix)/share/icons/hicolor/32x32/actions/
	mkdir -p $(LocalPrefix)/share/icons/hicolor/32x32/apps/
	ln -fs $(LocalPrefix)$(App-Prefix)/Image/apps/$(App-Name).png $(LocalPrefix)/share/icons/hicolor/32x32/apps/
	-rm -rf `find $(LocalPrefix)$(App-Prefix)/ -type d -name .svn`

	# Gnome
	#mkdir -p $(HOME)/.gnome2/nautilus-scripts/Archiving/$(App-Name)/
	#cp -r ../Desktop/Gnome/* $(HOME)/.gnome2/nautilus-scripts/Archiving/$(App-Name)/

	# Xfce
	#mkdir -p $(HOME)/.config/Thunar/
	#if test -f $(HOME)/.config/Thunar/uca.xml ; \
	#	then mv -f $(HOME)/.config/Thunar/uca.xml $(HOME)/.config/Thunar/uca.xml.orig ; \
	#fi
	#cp -r ../Desktop/Xfce/uca.xml $(HOME)/.config/Thunar/uca.xml.$(App-Name)

	# Misc
	#rm -rf $(DESTDIR)$(HOME)/Profiles/

	@echo ""
	@echo "You should add $(Local-Bin) to the PATH variable in your ~/.bash_profile or equivalent."
	@echo "Then restart KDE.  To run $(App-Name), enter: $(App-Name).pyw"
	@echo ""


# Uninstallers

UnInstall.AllUsers :
	@echo ""
	@echo "Removing $(App-Name) for all users ..."
	@echo "Only root can run 'make uninstall'.  Others should instead run: 'make uninstall-user'"
	@echo ""

	#make echo

	# Remove package
	-$(if $(AutoPackage),$(AutoPackage) remove $(App-FileName),)
	-$(if $(PacMan),$(PacMan) -R --noconfirm $(App-FileName),)
	-$(if $(Dpkg),$(Dpkg) -r $(App-FileName),)
	-$(if $(RPM),$(RPM) -e $(App-Name),)
	-$(if $(RemovePkg),$(RemovePkg) $(App-Name),)

	# KDE
	rm -f $(DESTDIR)$(Sys-Bin)/?7Z*
	rm -f $(DESTDIR)$(SysPrefix)/share/icons/hicolor/32x32/actions/ark*
	rm -f $(DESTDIR)$(SysPrefix)/share/icons/hicolor/32x32/actions/endturn*
	rm -f $(DESTDIR)$(SysPrefix)/share/icons/hicolor/32x32/apps/?7Z*
	rm -f $(DESTDIR)$(SysPrefix)/share/apps/konqueror/servicemenus/?7Z*
	rm -f $(DESTDIR)$(SysPrefix)/share/kde4/services/ServiceMenus/?7Z*
	rm -f $(DESTDIR)$(SysPrefix)/share/applnk/Utilities/File/?7Z*
	rm -f $(DESTDIR)$(SysPrefix)/share/applications/?7Z*
	rm -rf $(DESTDIR)$(SysPrefix)$(App-Prefix)/
	# User
	rm -f $(DESTDIR)$(HOME)/.kde*/share/apps/konqueror/servicemenus/?7Z*
	rm -f $(DESTDIR)$(HOME)/.kde*/share/kde4/services/ServiceMenus/?7Z*

	# Gnome
	rm -rf $(DESTDIR)$(HOME)/.gnome2/nautilus-scripts/Archiving/$(App-Name)/

	# Xfce
	#if test -f $(DESTDIR)$(HOME)/.config/Thunar/uca.xml ; \
	#	then mv -f $(DESTDIR)$(HOME)/.config/Thunar/uca.xml $(DESTDIR)$(HOME)/.config/Thunar/uca.xml.$(App-Name) ; \
	#fi
	#if test -f $(DESTDIR)$(HOME)/.config/Thunar/uca.xml.orig ; \
	#	then mv -f $(DESTDIR)$(HOME)/.config/Thunar/uca.xml.orig $(DESTDIR)$(HOME)/.config/Thunar/uca.xml ; \
	#fi
	#mv -f $(DESTDIR)$(HOME)/.config/Thunar/uca.xml.orig $(DESTDIR)$(HOME)/.config/Thunar/uca.xml
	rm -f $(DESTDIR)$(HOME)/.config/Thunar/uca.xml.$(App-Name)

UnInstall.CurrentUser :
	@echo ""
	@echo "Removing $(App-Name) for the current user ..."
	@echo ""

	#make echo

	# KDE
	rm -f $(Local-Bin)/?7Z*
	rm -f $(LocalPrefix)/share/icons/hicolor/32x32/actions/ark*
	rm -f $(LocalPrefix)/share/icons/hicolor/32x32/actions/endturn*
	rm -f $(LocalPrefix)/share/icons/hicolor/32x32/apps/?7Z*
	rm -f $(LocalPrefix)/share/applnk/Utilities/File/?7Z*
	rm -f $(LocalPrefix)/share/applications/?7Z*
	rm -rf $(LocalPrefix)$(App-Prefix)/
	# User
	rm -f $(KDE-LocalPrefix)/share/apps/konqueror/servicemenus/?7Z*
	rm -f $(KDE-LocalPrefix)/share/kde4/services/ServiceMenus/?7Z*

	# Gnome
	rm -rf $(HOME)/.gnome2/nautilus-scripts/Archiving/$(App-Name)/

	# Xfce
	#mv -f $(HOME)/.config/Thunar/uca.xml.orig $(HOME)/.config/Thunar/uca.xml
	#mv -f $(HOME)/.config/Thunar/uca.xml $(HOME)/.config/Thunar/uca.xml.$(App-Name)
	rm -f $(HOME)/.config/Thunar/uca.xml.$(App-Name)


# Objects

interfaces :
	@echo ""
	@echo "Making the $(App-Name) interfaces ..."
	@echo ""

	$(PyQt4-UIC) ../Source/Main.ui -o ../Source/Ui_Main.py
	$(PyQt4-UIC) ../Source/Settings.ui -o ../Source/Ui_Settings.py

resources :
	@echo ""
	@echo "Making the $(App-Name) resources ..."
	@echo ""

	$(PyQt4-RCC) -py3 ../Image/Main.qrc -o ../Source/Main_rc.py
	$(PyQt4-RCC) -py3 ../Image/Settings.qrc -o ../Source/Settings_rc.py

permissions:
	@echo ""
	@echo "Setting the $(App-Name) permissions ..."
	@echo ""

	chmod 777 ../Source/
	chmod 755 ../Source/$(App-Name).*
