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

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

# Path under which configuration files should be installed
%define confdir %{_sysconfdir}

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

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

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

# Whether or not to include the native infrastructure for the Java viewer
%define native @TVNC_BUILDNATIVE@

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

# Path under which the Java viewer should be installed
%define javadir @TVNC_JAVADIR@

# Whether or not to include the TurboJPEG JNI JAR files (used when launching
# the Java TurboVNC Viewer via Java Web Start)
%define jnijars @TVNC_INCLUDEJNIJARS@

# Directory in which the TurboJPEG JNI JARs can be found
%if "%{jnijars}" == "1"
%define jnijardir @TJPEG_JNIJARPATH@
%endif

# When building from a source RPM, these variables can be passed to rpmbuild in
# order to sign the JAR file(s) using an official code signing certificate:
#   java_keystore = value of CMake JAVA_KEYSTORE variable
#   java_keystore_pass = value of CMake JAVA_KEYSTORE_PASS variable
#   java_keystore_type = value of CMake JAVA_KEYSTORE_TYPE variable
#   java_key_alias = value of CMake JAVA_KEY_ALIAS variable
#   java_key_pass = value of CMake JAVA_KEY_PASS variable
#   java_tsa_url = value of CMake JAVA_TSA_URL variable
# The CMake variables in question are documented in java/CMakeLists.txt in the
# TurboVNC source.

Summary:   A highly-optimized version of VNC that can be used with performance-critical applications
Name:      @CMAKE_PROJECT_NAME_LC@
Version:   @VERSION@
Release:   @BUILD@
URL:       http://www.TurboVNC.org
#-->Source0: http://prdownloads.sourceforge.net/turbovnc/%{name}-%{version}.tar.gz
License:   GPL
Group:     User Interface/Desktops
Requires:  bash >= 2.0
Prereq:    /sbin/chkconfig %{_sysconfdir}/init.d
BuildRoot: %{_blddir}/%{name}-%{version}-root
BuildPrereq: /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

#-->%build
#-->%if %{?java_keystore:1}%{!?java_keystore:0}
#-->  JAVA_KEYSTORE_ARG="-DJAVA_KEYSTORE=%{java_keystore}"
#-->%endif
#-->%if %{?java_keystore_pass:1}%{!?java_keystore_pass:0}
#-->  JAVA_KEYSTORE_PASS_ARG="-DJAVA_KEYSTORE_PASS=%{java_keystore_pass}"
#-->%endif
#-->%if %{?java_keystore_type:1}%{!?java_keystore_type:0}
#-->  JAVA_KEYSTORE_TYPE_ARG="-DJAVA_KEYSTORE_TYPE=%{java_keystore_type}"
#-->%endif
#-->%if %{?java_key_alias:1}%{!?java_key_alias:0}
#-->  JAVA_KEY_ALIAS_ARG="-DJAVA_KEY_ALIAS=%{java_key_alias}"
#-->%endif
#-->%if %{?java_key_pass:1}%{!?java_key_pass:0}
#-->  JAVA_KEY_PASS_ARG="-DJAVA_KEY_PASS=%{java_key_pass}"
#-->%endif
#-->%if %{?java_tsa_url:1}%{!?java_tsa_url:0}
#-->  JAVA_TSA_URL_ARG="-DJAVA_TSA_URL=%{java_tsa_url}"
#-->%endif
#-->%if "%{jnijars}" == "1"
#-->  JNIJARS_ARGS="-DTVNC_INCLUDEJNIJARS=1 -DTJPEG_JNIJARPATH=%{jnijardir}"
#-->%endif
#-->cmake -G"Unix Makefiles" -DCMAKE_INSTALL_PREFIX=%{prefix} -DTVNC_BINDIR=%{bindir} -DTVNC_CONFDIR=%{confdir} -DTVNC_DOCDIR=%{docdir} -DTVNC_MANDIR=%{mandir} -DTVNC_BUILDJAVA=%{java} $JNIJARS_ARGS -DCMAKE_BUILD_TYPE=@CMAKE_BUILD_TYPE@ -DCMAKE_VERBOSE_MAKEFILE=@CMAKE_VERBOSE_MAKEFILE@ $JAVA_KEYSTORE_ARG $JAVA_KEYSTORE_PASS_ARG $JAVA_KEYSTORE_TYPE_ARG $JAVA_KEY_ALIAS_ARG $JAVA_KEY_PASS_ARG $JAVA_TSA_URL_ARG .
#-->make

%install
rm -rf $RPM_BUILD_ROOT
make install DESTDIR=$RPM_BUILD_ROOT

#-->%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=@TVNC_DOCDIR@
if [ ! "$TVNC_DOCDIR" = "%{docdir}" ]; then
	safedirmove $RPM_BUILD_ROOT/$TVNC_DOCDIR $RPM_BUILD_ROOT/%{docdir} $RPM_BUILD_ROOT/__tmpdoc
fi

%if "%{server}" == "1"
TVNC_CONFDIR=@TVNC_CONFDIR@
if [ ! "$TVNC_CONFDIR" = "%{confdir}" ]; then
	safedirmove $RPM_BUILD_ROOT/$TVNC_CONFDIR $RPM_BUILD_ROOT/%{confdir} $RPM_BUILD_ROOT/__tmpconf
fi
%endif

#-->%endif

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

%clean
rm -rf %{buildroot}

%if "%{server}" == "1"
%post
if [ "$1" = 1 ]; then
  if [ -f /etc/redhat-release ]; then /sbin/chkconfig --add tvncserver; fi
fi

%preun
if [ "$1" = 0 ]; then
  if [ -x /etc/init.d/tvncserver ]; then
    /etc/init.d/tvncserver stop >/dev/null 2>&1
  fi
  if [ -f /etc/redhat-release -a -f /etc/init.d/tvncserver ]; then
    /sbin/chkconfig --del tvncserver
  fi
fi

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

%files
%defattr(-,root,root)
%if "%{server}" == "1"
 %attr(0755,root,root) %config %{confdir}/init.d/tvncserver
 %config(noreplace) %{confdir}/sysconfig/tvncservers
 %config(noreplace) %{confdir}/turbovncserver.conf
 %config(noreplace) %{confdir}/turbovncserver-security.conf
%endif

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

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

%{prefix}/README.txt
%if "%{native}" == "1"
 %{bindir}/checkshmpixmaps
 %{bindir}/vncviewer
 %config(noreplace) /usr/share/applications/tvncviewer.desktop
 %{mandir}/man1/vncviewer.1*
 %if "%{java}" == "1"
  %{javadir}/libturbojpeg.so
  %{javadir}/libturbovnchelper.so
 %endif
%endif
%if "%{server}" == "1"
 %{bindir}/Xvnc
 %{bindir}/tvncconfig
 %{bindir}/vncserver
 %{bindir}/vncpasswd
 %{bindir}/vncconnect
%endif
%if "%{java}" == "1"
 %if "%{server}" == "1"
  %{javadir}/index.vnc
  %{javadir}/VncViewer.jnlp
  %{javadir}/favicon.ico
  %if "%{jnijars}" == "1"
   %{javadir}/ljtlinux32.jar
   %{javadir}/ljtlinux64.jar
   %{javadir}/ljtosx.jar
   %{javadir}/ljtwin32.jar
   %{javadir}/ljtwin64.jar
  %endif
 %endif
 %{javadir}/VncViewer.jar
 %{javadir}/README.txt
%endif
%if "%{server}" == "1"
 %{mandir}/man1/Xvnc.1*
 %{mandir}/man1/Xserver.1*
 %{mandir}/man1/vncserver.1*
 %{mandir}/man1/vncconnect.1*
 %{mandir}/man1/vncpasswd.1*
%endif

%changelog
