#! /bin/bash
# MountManager - the Linux program for easy and understandable mounting of USB devices, hard disks and other storage devices
# Copyright(C) 2007-2008 Tikhonov Sergey
# configure script

PACKAGE_NAME="MountManager"
PACKAGE_VERSION="0.2.6"

echo "!---------------------------------------------------------!"
echo "! Thank you for installing of MountManager program!       !"
echo "!              Mounting in Linux is easy!                 !"
echo "!---------------------------------------------------------!"

prefix="/usr"
bindir="${prefix}/bin"
datadir="${prefix}/share"
destdir="${datadir}/${PACKAGE_NAME}"
include_path="${prefix}/include"
lib_path="${prefix}/lib"
qt4_test_dir="Qt4Test/"

usage() {
	echo ""
	echo "  Usage: ./configure [options]"
	echo ""
	echo "  Available options:"
	echo "   --help                      Print this message"
	echo "   --clean                     Clean results of configuring and quit"
	echo "   --prefix=<prefix>           Set prefix (default: /usr)"
	echo "   --datadir=<dir>             Set data dir (default: ${prefix}/share"
	echo "   --qmake=<name>              Set qmake executable"
	echo "   --include_path=<path>       Set include path (default: ${prefix}/include)"
	echo "   --lib_path=<path>           Set lib path (default: ${prefix}/lib)"
	echo ""
}

clean_all() {
	rm -f sources/gui/const.h sources/mountmanager.pro Makefile
 	rm -f sources/build/*
}

############################################################
#	Read parameters
############################################################

for arg in ${@}; do
	case ${arg} in
	
	--help)
		usage
		exit 0
		;;

	--clean)
		clean_all
		rm -f configure.log
		exit 0
		;;
	
	--prefix=*)
		arg_prefix=${arg:9}
		;;

	--datadir=*)
		arg_datadir=${arg:10}
		;;

	--qmake=*)
		arg_qmake_cmd=${arg:8}
		;;

	--include_path=*)
		arg_include_path=${arg:15}
		;;

	--lib_path=*)
		arg_lib_path=${arg:11}
		;;

	--qt_include_path=*)
		arg_qt_include_path=${arg:18}
		;;

	--qt_lib_path=*)
		arg_qt_lib_path=${arg:14}
		;;

	--qsci_include_path=*)
		arg_qsci_include_path=${arg:20}
		;;

	--qsci_lib_path=*)
		arg_qsci_lib_path=${arg:16}
		;;

	*)
		echo "Unrecognized option: ${arg}"
		usage
		exit 1
		;;

	esac

done

clean_all
rm -f configure.log

if [ -n "${arg_prefix}" ]; then
	prefix="${arg_prefix}"
	bindir="${prefix}/bin"
	datadir="${prefix}/share"
	destdir="${datadir}/${PACKAGE_NAME}"
	include_path="${prefix}/include"
	qt_include_path="${include_path}/qt4"
	lib_path="${prefix}/lib"
	qt_lib_path="${lib_path}"
fi

if [ -n "${arg_datadir}" ]; then
	datadir="${arg_datadir}"
	destdir="${datadir}/${PACKAGE_NAME}"
fi

if [ -n "${arg_include_path}" ]; then
	include_path="${arg_include_path}"
	qt_include_path="${include_path}/qt4"
fi

if [ -n "${arg_lib_path}" ]; then
	lib_path="${arg_lib_path}"
	qt_lib_path="${lib_path}"
fi

if [ -n "${arg_qt_include_path}" ]; then
	qt_include_path="${arg_qt_include_path}"
fi

if [ -n "${arg_qt_lib_path}" ]; then
	qt_lib_path="${arg_qt_lib_path}"
fi

#if [ -n "${}" ]; then
#fi



############################################################
#	Check for commands
############################################################

#	g++
echo -n "checking for g++... "
if [ -n "${gpp_cmd}" ]; then
	gpp_cmd=`which ${gpp_cmd}`
else
	gpp_cmd=`which g++`
fi
if [ -n "${gpp_cmd}" ]; then
	echo "${gpp_cmd}"
else
	echo "no"
	exit 1
fi

#	make
echo -n "checking for make... "
if [ -n "${make_cmd}" ]; then
	make_cmd=`which ${make_cmd}`
else
	make_cmd=`which make`
	if [ -z "${make_cmd}" ]; then
		make_cmd=`which gmake`
	fi
fi
if [ -n "${make_cmd}" ]; then
	echo "${make_cmd}"
else
	echo "no"
	exit 1
fi

#	qmake
echo -n "checking for qmake... "
if [ -n "${arg_qmake_cmd}" ]; then
	qmake_cmd=`which ${arg_qmake_cmd}`
else
	qmake_cmd=`which qmake-qt4`
	if [ -z "${qmake_cmd}" ]; then
		qmake_cmd=`which qmake`
	fi
fi
if [ -n "${qmake_cmd}" ]; then
	echo "${qmake_cmd}"
else
	echo "no"
	exit 1
fi

#	lrelease
echo -n "checking for lrelease... "
if [ -n "${arg_lrelease_cmd}" ]; then
	lrelease_cmd=`which ${arg_lrelease_cmd}`
else
	lrelease_cmd=`which lrelease-qt4`
	if [ -z "${lrelease_cmd}" ]; then
		lrelease_cmd=`which lrelease`
	fi
fi
if [ -n "${lrelease_cmd}" ]; then
	echo "${lrelease_cmd}"
else
	echo "no"
	exit 1
fi

#	lshal
echo -n "checking for lshal... "
lshal_cmd=`which lshal`
if [ -n "${lshal_cmd}" ]; then
	echo "${lshal_cmd}"
else
	echo "no"
	exit 1
fi

#	chmod
echo -n "checking for chmod... "
chmod_cmd=`which chmod`
if [ -n "${chmod_cmd}" ]; then
	echo "${chmod_cmd}"
else
	echo "no"
	exit 1
fi


#============Check Qt4 libraries============#

echo -n "checking for Qt4 libraries... "

cd ${qt4_test_dir}
${qmake_cmd} 2> configure.log
${make_cmd} 1> /dev/null 2>> configure.log

test_output=`cat configure.log`
${make_cmd} clean > /dev/null
rm -f Makefile
rm -f qt4test
cd ../

if [ -z "${test_output}" ]; then
	echo "passed."
else
	echo "failed."
	echo "Please, install Qt4 libraries(Modules: QtXml, QtCore, QtGui, QtDBus)"
	exit 1
fi

############################################################
#	const.h
############################################################

echo '#ifndef CONST_H
#define CONST_H

#define VERSION "0.2.6"
#define AUTHOR "Tikhonov Sergey"
#define AUTHOR_EMAIL "sstikhonov@gmail.com"
#define PROGRAM_NAME "MountManager"
#define ORGANIZATION "Vialinx"
#define PROGRAM_NAME_WITH_VERSION PROGRAM_NAME " " VERSION
#define PLUGINS_URL "http://vialinx.org/index.php?option=com_remository&Itemid=73&func=select&id=15"

// Hal constants
#define HAL_SERVICE "org.freedesktop.Hal"
#define HAL_PATH "/org/freedesktop/Hal/Manager"
#define HAL_INTERFACE "org.freedesktop.Hal.Manager"
#define HAL_DEVICE_INTERFACE "org.freedesktop.Hal.Device"

// Pathes
#define PREFIX "'${prefix}'"
#define SHARE_PATH PREFIX "/share/mountmanager/"
#define ICONS_PATH SHARE_PATH "icons/"
#define IMAGES_PATH SHARE_PATH "images/"
#define OPTIONS_PATH SHARE_PATH "options/"
#define PLUGINS_PATH PREFIX "/lib/mountmanager/plugins/"
#define TRANS_PATH PREFIX "/lib/mountmanager/trans/"
#define PROGRAM_ICON_PATH PREFIX "/share/icons/mm.png"
#define DOC_PATH PREFIX "/share/doc/MountManager/"
#define BACKUPS_PATH SHARE_PATH "/BackUps/"

#endif' > sources/gui/const.h

############################################################
#	Starting script
############################################################

echo '#!/bin/bash

KDESU_PROG=`which kdesu 2> /dev/null`
KDESUDO_PROG=`which kdesudo 2> /dev/null`
GTKSU_PROG=`which gtksu 2> /dev/null`
GKSU_PROG=`which gksu 2> /dev/null`
KDIALOG_PROG=`which kdialog 2> /dev/null`
GTKDIALOG_PROG=`which gtkdialog 2> /dev/null`

ID=`id -u`
if [ $ID -eq 0 ]; then
	'${prefix}'/bin/mountmanager
elif [ -n "$KDESU_PROG" ]; then
	if [[ "$1" = "--debug" ]]; then
		$KDESU_PROG -d -n '${prefix}'/bin/mountmanager
	else
		$KDESU_PROG -d -n "'${prefix}'/bin/mountmanager 2> /var/log/MountManager.log"
	fi
elif [ -n "$KDESUDO_PROG" ]; then
	if [[ "$1" = "--debug" ]]; then
		$KDESUDO_PROG -d -n '${prefix}'/bin/mountmanager
	else
		$KDESUDO_PROG -d -n "'${prefix}'/bin/mountmanager 2> /var/log/MountManager.log"
	fi
elif [ -n "$GTKSU_PROG" ]; then
	if [[ "$1" = "--debug" ]]; then
		$GTKSU_PROG '${prefix}'/bin/mountmanager
	else
		$GTKSU_PROG "'${prefix}'/bin/mountmanager 2> /var/log/MountManager.log"
	fi
elif [ -n "$GKSU_PROG" ]; then
	if [[ "$1" = "--debug" ]]; then
		$GKSU_PROG '${prefix}'/bin/mountmanager
	else
		$GKSU_PROG "'${prefix}'/bin/mountmanager 2> /var/log/MountManager.log"
	fi
elif [ -n "$KDIALOG_PROG" ]; then
	$KDIALOG_PROG "Install kdesu, kdesudo,gtksu or gksu program to get root permissions"
elif [ -n $GTKDIALOG_PROG ]; then
	$GTKDIALOG_PROG "Install kdesu, kdesudo,gtksu or gksu program to get root permissions"
else
	echo "Cannot find kdialog or gtkdialog; kdesu or kdesudo or gtksu programs\n"
fi
' > mm

############################################################
#	Desktop file
############################################################

echo '# MountManager - mounting in linux is easy
# Copyright (C) 2007-2016 Tikhonov Sergey
# See file "LICENSE" for details
[Desktop Entry]
Type=Application
Name=MountManager
Name[ru]=MountManager
GenericName=Tool for mounting of partitions
GenericName[ru]=Инструмент для монтирования разделов
Comment=the program for mounting of storage devices,flash cards, removable disks in Linux
Comment[ru]=программа для монтирования устройств хранения данных, флэш карт и съёмных дисков
Exec=mm
Icon=mm
Terminal=false
' > resources/desktop/mountmanager-gtk.desktop

cp -r resources/desktop/mountmanager-gtk.desktop resources/desktop/mountmanager-kde.desktop

echo 'Categories=GTK;System;Settings;
NotShowIn=KDE;
' >> resources/desktop/mountmanager-gtk.desktop

echo 'Categories=System;Qt;
OnlyShowIn=KDE;
' >> resources/desktop/mountmanager-kde.desktop

############################################################
#	Makefile
############################################################
echo "FAKE_ROOT=
BIN="${prefix}"/bin
SHAREDIR="${prefix}"/share/mountmanager
OPTIONS=\$(SHAREDIR)/options
ICONS=\$(SHAREDIR)/icons
IMAGES=\$(SHAREDIR)/images
DOC="${prefix}"/share/doc/MountManager
APPLICATIONS="${prefix}"/share/applications
MANS="${prefix}"/share/man/man1
TRANSDIR="${prefix}"/lib/mountmanager/trans
ALL_ICONS="${prefix}"/share/icons
PLUGINS_DIR="${prefix}"/lib/mountmanager/plugins

CHMOD="${chmod_cmd}"
QMAKE="${qmake_cmd}"
MAKE="${make_cmd}"
LRELEASE="${lrelease_cmd}"

sources/mountmanager:
	cd sources && \$(QMAKE) && \$(MAKE)
	@echo \"+---------------------------------------------------------------------+\"
	@echo \"|               Compiling of MountManager plugins...                  |\"
	@echo \"+---------------------------------------------------------------------+\"
	-for dir in \"ImagesMounting\" \"FstabPreview\" \"DocumentationPanel\" \"Tips\" \"SharesMount\"; do \\
		cd plugins/\$\$dir; \\
		\$(QMAKE); \\
		\$(MAKE); \\
		\$(LRELEASE) translations/*.ts; \\
		cd ../../; \\
	done
	cd sources && \$(LRELEASE) mountmanager.pro
	@echo \"+---------------------------------------------------------------------+\"
	@echo \"|               OK. Type \"make install\" to install.                   |\"
	@echo \"+---------------------------------------------------------------------+\"

distclean:
	rm -f mountmanager
	rm -f mm
	-for dir in \"ImagesMounting\" \"FstabPreview\" \"DocumentationPanel\" \"Tips\" \"SharesMount\"; do \\
		cd plugins/\$\$dir; \\
		\$(QMAKE) distclean; \\
		\$(MAKE) distclean; \\
		rm -f translations/*.qm; \\
		cd ../../; \\
	done
	cd sources && make clean
	rm -f sources/Makefile
	rm -f trans/*.qm

clean:
	cd sources && make clean
	rm -f sources/Makefile

install:
#	Directories
	install -d \$(SHAREDIR)
	install -d \$(OPTIONS)
	install -d \$(ICONS)
	install -d \$(IMAGES)
	install -d \$(DOC)
	install -d \$(BIN)
	install -d \$(TRANSDIR)
	install -d \$(MANS)
	install -d \$(PLUGINS_DIR)
#	Copy files
	install -m 755 mm \$(BIN)
	install -m 644 options/* \$(OPTIONS)/
	install -m 644 resources/icons/* \$(ICONS)/
	install -m 644 resources/images/* \$(IMAGES)/
	install -m 644 trans/*.qm \$(TRANSDIR)/
	cp -R doc/* \$(DOC)/
	cp -R mans/* \$(MANS)/
	-for dir in \"ImagesMounting\" \"FstabPreview\" \"DocumentationPanel\" \"Tips\" \"SharesMount\"; do \\
		cd plugins/\$\$dir; \\
		cp -f translations/*.qm \$(TRANSDIR)/; \\
		for file in \`ls *.so\`; do \\
			cp \$\$file \$(PLUGINS_DIR)/; \\
		done; \\
		cd ../../; \\
	done
#	Binary
	install -m 755 mountmanager \$(BIN)/
#	App icon and .desktop file
	install -d \$(ALL_ICONS)
	install -d \$(APPLICATIONS)
	install -m 644 resources/desktop/mm.png \$(ALL_ICONS)
	install -m 644 resources/desktop/mountmanager-gtk.desktop \$(APPLICATIONS)/
	install -m 644 resources/desktop/mountmanager-kde.desktop \$(APPLICATIONS)/
	@echo \"+---------------------------------------------------------------------+\"
	@echo \"|               OK. Start the program by typing \"mm\"                  |\"
	@echo \"+---------------------------------------------------------------------+\"

uninstall:
	rm -rf \$(SHAREDIR)
	rm -f \$(BIN)/mm
	rm -f \$(ALL_ICONS)/mm.png
	rm -f \$(APPLICATIONS)/mountmanager-gtk.desktop.desktop
	rm -f \$(APPLICATIONS)/mountmanager-kde.desktop.desktop
	rm -rf "${prefix}"/lib/mountmanager/
	rm -f \$(MANS)/mountmanager.1
	rm -f \$(MANS)/mm.1
	rm -rf \$(DOC)
" > Makefile


############################################################
#	mountmanager.pro
############################################################

echo 'TEMPLATE = app
TARGET = ../mountmanager
DEPENDPATH += . core gui
INCLUDEPATH += . core gui

CONFIG += qdbus
QT+=xml

OBJECTS_DIR = build
MOC_DIR = build

# Input
HEADERS += core/diskdevice.h \
		core/diskfstab.h \
		core/diskhal.h \
		core/diskoptionsparser.h \
		core/diskoptionscenter.h \
		core/diskbackup.h \
		core/diskcore.h \
		\
		gui/mainwindow.h \
		gui/const.h \
		gui/centralwidget.h \
		gui/treewidget.h \
		gui/menu.h \
		gui/messagebar.h \
		gui/volumewidget.h \
		gui/optionsview.h \
		gui/popupwindow.h \
		gui/infobutton.h \
		gui/specialcomboboxwidgets.h \
		gui/permissionswidget.h \
		gui/swapwidget.h \
		gui/diskwidget.h \
		gui/manual.h \
		gui/aboutprogramdialog.h \
		gui/widgetformount.h \
		gui/choosemountpointwidget.h \
		gui/restorationsystem.h \
		gui/trayicon.h \
		gui/pluginsmanager.h \
		gui/plugininterface.h \
		gui/welcomedialog.h \
		gui/usbwizard.h \
		gui/optionsforfswidget.h \
		gui/usbmanager.h \
		gui/udevrule.h

SOURCES += core/diskdevice.cpp \
		core/diskfstab.cpp \
		core/diskhal.cpp \
		core/diskoptionsparser.cpp \
		core/diskoptionscenter.cpp \
		core/diskbackup.cpp \
		core/diskcore.cpp \
		\
		gui/main.cpp \
		gui/mainwindow.cpp \
		gui/centralwidget.cpp \
		gui/treewidget.cpp \
		gui/menu.cpp \
		gui/messagebar.cpp \
		gui/volumewidget.cpp \
		gui/optionsview.cpp \
		gui/popupwindow.cpp \
		gui/infobutton.cpp \
		gui/specialcomboboxwidgets.cpp \
		gui/permissionswidget.cpp \
		gui/swapwidget.cpp \
		gui/diskwidget.cpp \
		gui/manual.cpp \
		gui/aboutprogramdialog.cpp \
		gui/widgetformount.cpp \
		gui/choosemountpointwidget.cpp \
		gui/restorationsystem.cpp \
		gui/trayicon.cpp \
		gui/pluginsmanager.cpp \
		gui/welcomedialog.cpp \
		gui/usbwizard.cpp \
		gui/optionsforfswidget.cpp \
		gui/usbmanager.cpp \
		gui/udevrule.cpp
		
TRANSLATIONS = \
	../trans/ru.ts \
	../trans/pl.ts \
	../trans/tr.ts \
	../trans/es.ts \
	../trans/pt.ts \
	../trans/br.ts
' > sources/mountmanager.pro



############################################################
#	configure.log
############################################################
echo "# ${PACKAGE_NAME} was configured with the following parameters:" >> configure.log
echo "#   --prefix=${arg_prefix}" >> configure.log
echo "#   --datadir=${arg_datadir}" >> configure.log
echo "#   --qmake=${arg_qmake}" >> configure.log
echo "#   --include_path=${arg_include_path}" >> configure.log
echo "#   --lib_path=${arg_lib_path}" >> configure.log
echo "#   --qt_lib_path=${arg_qt_lib_path}" >> configure.log
echo "#   --qt_include_path=${arg_qt_include_path}" >> configure.log
echo "#   --qsci_lib_path=${arg_qsci_lib_path}" >> configure.log
echo "#   --qsci_include_path=${arg_qsci_include_path}" >> configure.log
echo "" >> configure.log
echo "# Current file 'configure.log' is a script that can reconfigure ${PACKAGE_NAME} with last used parameters" >> configure.log
echo "# Just type './configure.log' in your shell" >> configure.log
echo "" >> configure.log
echo "./configure --prefix=${arg_prefix} --include_path=${arg_include_path} --lib_path=${arg_lib_path} --qt_include_path=${arg_qt_include_path} --qt_lib_path=${arg_qt_lib_path} --qsci_include_path=${arg_qsci_include_path} --qsci_lib_path=${arg_qsci_lib_path}" >> configure.log
chmod +x configure.log



############################################################
#	OK
############################################################
echo "!-------------------------------------------------!"
echo "!   OK. Type \"make\" to compile the package.       !"
echo "!-------------------------------------------------!"
