# Path under which TurboVNC should be installed
%define prefix @CMAKE_INSTALL_PREFIX@

# Path under which executables and scripts should be installed
%define bindir @CMAKE_INSTALL_FULL_BINDIR@

# Path under which configuration files should be installed
%define sysconfdir @CMAKE_INSTALL_FULL_SYSCONFDIR@

# Path under which docs should be installed
%define docdir %{_defaultdocdir}/%{name}-%{version}

# Path under which icons should be installed
%define datadir %{_datadir}

# Path under which man pages should be installed
%define mandir @CMAKE_INSTALL_FULL_MANDIR@

# Whether or not to include the Java viewer
%define viewer @TVNC_BUILDVIEWER@

# Whether or not to include a custom JRE
%{!?jre: %define jre @TVNC_INCLUDEJRE@}

# Work around "No build ID note found" error caused by some versions of RPM
# trying in vain to extract debug info from the custom JRE
%if "%{jre}" == "1"
%undefine _missing_build_ids_terminate_build
%endif

# Whether or not to include the server
%define server @TVNC_BUILDSERVER@

# Whether or not to include the web server
%define webserver @TVNC_BUILDWEBSERVER@

# Whether or not to include the TurboVNC Server init script
%if "%{server}" == "1"
%define initscript @TVNC_BUILDINITSCRIPT@
%endif

# Path under which the Java viewer should be installed
%if "%{viewer}" == "1"
%define javadir @CMAKE_INSTALL_FULL_JAVADIR@
%endif

# Exclude the custom JRE from automatic dependency processing
%if "%{viewer}" == "1"
%if "%{jre}" == "1"
%define __find_provides %{_tmppath}/%{name}-%{version}-%{release}-find-provides
%define __find_requires %{_tmppath}/%{name}-%{version}-%{release}-find-requires
%define _use_internal_dependency_generator 0
%endif
%endif

Summary:   A highly-optimized version of VNC that can be used with performance-critical applications
Name:      @PKGNAME@
Version:   @VERSION@
Release:   @BUILD@
Vendor:    @PKGVENDOR@
URL:       @PKGURL@
#-->Source0: https://github.com/TurboVNC/turbovnc/releases/download/%{version}/turbovnc-%{version}.tar.gz
License:   GPL
Group:     User Interface/Desktops
Requires:  bash >= 2.0
%if "%{server}" == "1"
%if "%{initscript}" == "1"
Requires:  /sbin/chkconfig /usr/sbin/semanage %{_sysconfdir}/init.d initscripts
%endif
Requires:  /usr/bin/xauth /usr/bin/xkbcomp xkeyboard-config
Requires:  /usr/bin/dbus-launch
%endif
BuildRoot: %{_blddir}/%{name}-%{version}-root
BuildRequires: /usr/bin/perl libjpeg-turbo

%description
Virtual Network Computing (VNC) is a remote display system that allows you to
view and interact with a desktop environment that is running on a remote
computer.  Using VNC, you can run graphical applications on a remote machine
and send only the pixels generated by these applications to your local machine.
VNC is platform-independent and supports a wide variety of operating systems
and architectures as both servers and clients.

TurboVNC is a high-speed version of VNC derived from TightVNC.  It contains
a variant of Tight encoding that is tuned to maximize performance for image-
intensive applications (such as VirtualGL, video applications, and image
editors) while still providing excellent performance for other types of
applications.  TurboVNC, in combination with VirtualGL, provides a complete
solution for remotely displaying 3D applications with interactive performance.

#-->%prep
#-->%setup -q -n @CMAKE_PROJECT_NAME_LC@-%{version}

#-->%build
#-->cmake -G"Unix Makefiles" -DCMAKE_BUILD_TYPE=@CMAKE_BUILD_TYPE@ \
#-->  -DBUILD=%{release} -DCMAKE_INSTALL_BINDIR=%{bindir} \
#-->  -DCMAKE_INSTALL_DATADIR=%{datadir} -DCMAKE_INSTALL_DOCDIR=%{docdir} \
#-->  -DCMAKE_INSTALL_JAVADIR=%{javadir} -DCMAKE_INSTALL_MANDIR=%{mandir} \
#-->  -DCMAKE_INSTALL_PREFIX=%{prefix} \
#-->  -DCMAKE_INSTALL_SYSCONFDIR=%{sysconfdir} \
#-->  -DCMAKE_VERBOSE_MAKEFILE=@CMAKE_VERBOSE_MAKEFILE@ \
#-->  -DTVNC_BUILDVIEWER=%{viewer} \
#-->  -DTVNC_BUILDSERVER=%{server} -DTVNC_BUILDWEBSERVER=%{webserver} \
#-->  -DTVNC_BUILDINITSCRIPT=%{initscript} \
#-->  -DPYTHON_EXECUTABLE=@PYTHON_EXECUTABLE@ -DTVNC_INCLUDEJRE=%{jre} \
#-->  .
#-->export NUMCPUS=`grep -c '^processor' /proc/cpuinfo`
#-->make -j$NUMCPUS --load-average=$NUMCPUS

%install
rm -rf $RPM_BUILD_ROOT
DESTDIR=$RPM_BUILD_ROOT @CMAKE_MAKE_PROGRAM@ install

#-->%if 0

# This is only needed to support in-tree RPM generation via 'make rpm'.  When
# building from a SRPM, we control where things are installed via CMake
# variables.

safedirmove ()
{
	if [ "$1" = "$2" ]; then
		return 0
	fi
	if [ "$1" = "" -o ! -d "$1" ]; then
		echo safedirmove: source dir $1 is not valid
		return 1
	fi
	if [ "$2" = "" -o -e "$2" ]; then
		echo safedirmove: dest dir $2 is not valid
		return 1
	fi
	if [ "$3" = "" -o -e "$3" ]; then
		echo safedirmove: tmp dir $3 is not valid
		return 1
	fi
	mkdir -p $3
	mv $1/* $3/
	rmdir $1
	mkdir -p $2
	mv $3/* $2/
	rmdir $3
	return 0
}

TVNC_DOCDIR=@CMAKE_INSTALL_FULL_DOCDIR@
if [ ! "$TVNC_DOCDIR" = "%{docdir}" ]; then
	safedirmove $RPM_BUILD_ROOT/$TVNC_DOCDIR $RPM_BUILD_ROOT/%{docdir} $RPM_BUILD_ROOT/__tmpdoc
fi
%if "%{viewer}" == "1"
TVNC_DATADIR=@CMAKE_INSTALL_FULL_DATADIR@
if [ ! "$TVNC_DATADIR" = "%{datadir}" ]; then
	safedirmove $RPM_BUILD_ROOT/$TVNC_DATADIR/icons $RPM_BUILD_ROOT/%{datadir}/icons $RPM_BUILD_ROOT/__tmpicons
fi
%endif

#-->%endif

%if "%{viewer}" == "1"
mkdir -p %{buildroot}/usr/share/applications
cat > %{buildroot}/usr/share/applications/tvncviewer.desktop << EOF
[Desktop Entry]
Name=@CMAKE_PROJECT_NAME@ Viewer
Comment=@CMAKE_PROJECT_NAME@ client application
Exec=%{bindir}/vncviewer
Terminal=false
Icon=turbovnc
Type=Application
Categories=Application;Utility;X-Red-Hat-Extra;
EOF
%endif

%if "%{server}" == "1"
%if "%{sysconfdir}" == "%{_sysconfdir}"
mkdir -p %{buildroot}/%{sysconfdir}/polkit-1/localauthority/50-local.d
cat > %{buildroot}/%{sysconfdir}/polkit-1/localauthority/50-local.d/45-turbovnc-gnome3.pkla << EOF
[Eliminate GNOME 3+ dialogs in TurboVNC]
Identity=unix-user:*
Action=org.freedesktop.color-manager.create-device;org.freedesktop.color-manager.create-profile;org.freedesktop.color-manager.delete-device;org.freedesktop.color-manager.delete-profile;org.freedesktop.color-manager.modify-device;org.freedesktop.color-manager.modify-profile;org.debian.pcsc-lite.access_pcsc;org.freedesktop.packagekit.system-sources-refresh;org.freedesktop.packagekit.system-network-proxy-configure
ResultAny=no
EOF
mkdir -p %{buildroot}/%{sysconfdir}/polkit-1/rules.d
cat > %{buildroot}/%{sysconfdir}/polkit-1/rules.d/45-turbovnc-gnome3.rules << EOF
// Eliminate GNOME 3+ dialogs in TurboVNC
polkit.addRule(function(action, subject) {
  if ((action.id == "org.freedesktop.color-manager.create-device" ||
       action.id == "org.freedesktop.color-manager.create-profile" ||
       action.id == "org.freedesktop.color-manager.delete-device" ||
       action.id == "org.freedesktop.color-manager.delete-profile" ||
       action.id == "org.freedesktop.color-manager.modify-device" ||
       action.id == "org.freedesktop.color-manager.modify-profile" ||
       action.id == "org.debian.pcsc-lite.access_pcsc" ||
       action.id == "org.freedesktop.packagekit.system-sources-refresh" ||
       action.id == "org.freedesktop.packagekit.system-network-proxy-configure") &&
      !subject.local)
    return polkit.Result.NO;
});
EOF
%endif
%endif

%if "%{viewer}" == "1"
%if "%{jre}" == "1"
echo 'grep -v %{buildroot}%{javadir}/jre|/usr/lib/rpm/find-provides' >%{_tmppath}/%{name}-%{version}-%{release}-find-provides
echo 'grep -v %{buildroot}%{javadir}/jre|/usr/lib/rpm/find-requires' >%{_tmppath}/%{name}-%{version}-%{release}-find-requires
chmod 755 %{_tmppath}/%{name}-%{version}-%{release}-find-provides
chmod 755 %{_tmppath}/%{name}-%{version}-%{release}-find-requires
%endif
%endif

%clean
rm -rf %{buildroot}

%if "%{initscript}" == "1"
%post
%if "%{sysconfdir}" == "%{_sysconfdir}"
if [ -x %{_sysconfdir}/rc.d/init.d/tvncserver ]; then
  semanage fcontext -a -t bin_t %{_sysconfdir}/rc.d/init.d/tvncserver
  restorecon -R -v %{_sysconfdir}/rc.d/init.d/tvncserver
else
  semanage fcontext -a -t bin_t %{_sysconfdir}/init.d/tvncserver
  restorecon -R -v %{_sysconfdir}/init.d/tvncserver
fi
if [ "$1" = 1 ]; then
  /sbin/chkconfig --add tvncserver
fi
%endif

%preun
%if "%{sysconfdir}" == "%{_sysconfdir}"
if [ "$1" = 0 ]; then
  if [ -x %{_sysconfdir}/init.d/tvncserver ]; then
    %{_sysconfdir}/init.d/tvncserver stop >/dev/null 2>&1
  fi
  if [ -f %{_sysconfdir}/init.d/tvncserver ]; then
    /sbin/chkconfig --del tvncserver
  fi
  if [ -x %{_sysconfdir}/rc.d/init.d/tvncserver ]; then
    semanage fcontext -d %{_sysconfdir}/rc.d/init.d/tvncserver
    restorecon -R -v %{_sysconfdir}/rc.d/init.d/tvncserver
  else
    semanage fcontext -d %{_sysconfdir}/init.d/tvncserver
    restorecon -R -v %{_sysconfdir}/init.d/tvncserver
  fi
fi
%endif

%postun
%if "%{sysconfdir}" == "%{_sysconfdir}"
if [ "$1" -ge "1" ]; then
  if [ -x %{_sysconfdir}/init.d/tvncserver ]; then
    %{_sysconfdir}/init.d/tvncserver condrestart >/dev/null 2>&1
  fi
fi
%endif
%endif

%files
%defattr(-,root,root)
%if "%{server}" == "1"
 %if "%{sysconfdir}" != "%{_sysconfdir}"
  %dir %{sysconfdir}
  %if "%{initscript}" == "1"
   %dir %{sysconfdir}/init.d
   %dir %{sysconfdir}/sysconfig
  %endif
 %endif
 %if "%{initscript}" == "1"
  %attr(0755,root,root) %config %{sysconfdir}/init.d/tvncserver
  %config(noreplace) %{sysconfdir}/sysconfig/tvncservers
 %endif
 %config(noreplace) %{sysconfdir}/turbovncserver.conf
 %config(noreplace) %{sysconfdir}/turbovncserver-security.conf
 %if "%{sysconfdir}" == "%{_sysconfdir}"
  %dir %{sysconfdir}/polkit-1
  %attr(0750,root,polkitd) %dir %{sysconfdir}/polkit-1/localauthority
  %dir %{sysconfdir}/polkit-1/localauthority/50-local.d
  %config %{sysconfdir}/polkit-1/localauthority/50-local.d/45-turbovnc-gnome3.pkla
  %config %{sysconfdir}/polkit-1/rules.d/45-turbovnc-gnome3.rules
 %endif
%endif

%dir %{docdir}
%doc %{docdir}/*

%dir %{prefix}
%dir %{bindir}
%dir %{mandir}
%dir %{mandir}/man1
%if "%{viewer}" == "1"
 %dir %{javadir}
%endif

%if "%{prefix}" == "@CMAKE_INSTALL_DEFAULT_PREFIX@"
  %{prefix}/README.md
%else
  %{docdir}/README.md
%endif
%if "%{viewer}" == "1"
 %{bindir}/vncviewer
 %config(noreplace) /usr/share/applications/tvncviewer.desktop
 %{mandir}/man1/vncviewer.1*
 %{datadir}/icons/hicolor/128x128/apps/turbovnc.png
 %{datadir}/icons/hicolor/48x48/apps/turbovnc.png
 %{datadir}/icons/hicolor/16x16/apps/turbovnc.png
 %{javadir}/libturbovnchelper.so
%endif
%if "%{server}" == "1"
 %{bindir}/Xvnc
 %{bindir}/tvncconfig
 %{bindir}/vncserver
 %{bindir}/xstartup.turbovnc
 %{bindir}/vncpasswd
 %{bindir}/vncconnect
 %if "%{webserver}" == "1"
  %{bindir}/webserver
 %endif
%endif
%if "%{viewer}" == "1"
 %{javadir}/VncViewer.jar
 %{javadir}/README.md
 %if "%{jre}" == "1"
  %{javadir}/jre
 %endif
%endif
%if "%{server}" == "1"
 %{mandir}/man1/Xvnc.1*
 %{mandir}/man1/Xserver.1*
 %{mandir}/man1/tvncconfig.1*
 %{mandir}/man1/vncserver.1*
 %{mandir}/man1/vncconnect.1*
 %{mandir}/man1/vncpasswd.1*
%endif

%changelog
