%define wx_version_major 3
%define wx_version_minor 0
%define wx_version %{wx_version_major}.%{wx_version_minor}

%define force_wx_gtk2 %{nil}
%define force_wx_gtk2 0

################################################################################

%define wx_name_postfix %{nil}
%if 0%{?wx_version_major} >= 3
  %if 0%{?wx_version_minor} == 0
    %define wx_name_postfix %{wx_version_major}
  %else
    %define wx_name_postfix %{wx_version_major}%{wx_version_minor}
  %endif
%endif

# Force toolkit
# e.g. gtk2 instead of gtk3
%define wx_toolkit %{nil}
%if 0%{?force_wx_gtk2}
  %if 0%{?wx_version_major} >= 3
    %if 0%{?wx_version_minor} == 0
      %define wx_toolkit gtk2
    %endif
  %endif
%endif

%undefine update_mime_database_n
%if 0%{?fedora}%{?rhel}
  %define update_mime_database_n 1
%endif

%define debug_package %{nil}

%define doc_license %{nil}
%define doc_license 1
%if 0%{?rhel}%{?fedora}
  %if 0%{?rhel}
    %if 0%{?rhel} < 7
      %define doc_license 0
    %endif
  %else
    %if 0%{?fedora} < 19
      %define doc_license 0
    %endif
  %endif
%endif

%define enable_dumpfullversion %{nil}
%define enable_dumpfullversion 0
%if 0%{?rhel}%{?fedora}
  %if 0%{?rhel}
    %if 0%{?rhel} > 7
      %define enable_dumpfullversion 1
    %endif
  %else
    %if 0%{?fedora} > 25
      %define enable_dumpfullversion 1
    %endif
  %endif
%endif

%define license_files src/License.txt

Name:           veracrypt
Version:        1.21
Release:        3%{?dist}
Group:          File tools
#License:        Microsoft Public License
#License:        Apache License 2.0
License:        ASL 2.0 or TrueCrypt License
Summary:        Disk encryption software 
#URL:            https://veracrypt.codeplex.com
URL:            https://www.veracrypt.fr/en/Home.html
#Source0:        https://github.com/veracrypt/VeraCrypt/archive/VeraCrypt-VeraCrypt_%{version}.tar.gz
Source0:        https://launchpad.net/veracrypt/trunk/%{version}/+download/VeraCrypt_%{version}_Source.tar.bz2
Patch1:         veracrypt-1.0f-2-no-makeself.patch
Patch2:         veracrypt-1.0f-2-desktop.patch
# https://github.com/veracrypt/VeraCrypt/issues/198
# In file included from ../Crypto/cpu.h:175:0,
#                  from ../Crypto/cpu.c:3:
# ../Crypto/cpu.c: In function 'TryAESNI':
# /usr/lib/gcc/x86_64-redhat-linux/7/include/wmmintrin.h:61:1: error: inlining failed in call to always_inline '_mm_aesenc_si128': target specific option mismatch
#  _mm_aesenc_si128 (__m128i __X, __m128i __Y)
Patch3:         veracrypt-1.21-dumpfullversion.patch

#BuildRequires:	wxGTK3-devel
%if "%{?wx_toolkit}" != ""
BuildRequires:  compat-wxGTK%{?wx_name_postfix}-%{wx_toolkit}-devel
%else
BuildRequires:  wxGTK%{?wx_name_postfix}-devel
%endif
BuildRequires:	nasm
BuildRequires:	fuse-devel
BuildRequires:	ImageMagick
BuildRequires:	gzip
BuildRequires:	bzip2
BuildRequires:	yasm

%if 0%{?rhel}%{?fedora}
%if 0%{?rhel}
BuildRequires:	ghostscript
%else
BuildRequires:	ghostscript-core
%endif
%else
BuildRequires:	ghostscript
%endif

%description
Free disk encryption software based on TrueCrypt.

%prep
# for 1.19:
#setup -qn "VeraCrypt-VeraCrypt_%{version}"
# for 1.21:
%setup -q -c
%patch1 -p1
%patch2 -p1
%if 0%{?enable_dumpfullversion}
%patch3 -p1
%endif

%build
%if 0%{wx_version_major} < 3
WXCONFIG="wx-config"
%else
WXCONFIG="wx-config-%{wx_version}"
%endif
export WXCONFIG

%if "%{?wx_toolkit}" != ""
if hash "${WXCONFIG}-%{?wx_toolkit}" >/dev/null 2>&1 ; then
  WXCONFIG="${WXCONFIG}-%{?wx_toolkit}"
  export WXCONFIG
fi
%endif

pushd "src"
%{__make} WX_CONFIG="${WXCONFIG}" %{?_smp_mflags}
popd

pushd src/Resources/Icons
for s in {16,48,128,256} ; do
  convert "VeraCrypt-${s}x${s}.xpm" "VeraCrypt-${s}x${s}.png"
done
popd

find "src/Setup/Linux/usr" -xtype f -iname 'License.txt' -print0 | xargs --null -I{} sed -i -re 's/\r$//' '{}'

find "src/Setup/Linux/usr" "doc/chm" -xtype f \( -iname '*.pdf' -o -iname '*.chm' \) -print0 | while IFS= read -r -d $'\0' file ; do
  d="$( dirname "${file}" )"
  f="$( basename "${file}" )"
  
  g="$( sed -re 's/[[:blank:]]/_/g' <<< "${f}" )"
  
  if [[ "${f}" != "${g}" ]]; then
    mv "${file}" "${d}/${g}"
  fi
done

[[ -f "src/Setup/Linux/usr/bin/veracrypt-uninstall.sh" ]] && rm -f "src/Setup/Linux/usr/bin/veracrypt-uninstall.sh" || :

[[ -f "src/Setup/Linux/usr/share/pixmaps/veracrypt.xpm" ]] && rm -f "src/Setup/Linux/usr/share/pixmaps/veracrypt.xpm" || :

%install
[[ -d "src/Setup/Linux/usr" ]] && cp -r "src/Setup/Linux/usr" "%{buildroot}"/ || :

[[ -f "src/Setup/Linux/veracrypt.desktop" ]] && install -p -D -m 0644 "src/Setup/Linux/veracrypt.desktop" "%{buildroot}%{_datadir}/applications/%{name}.desktop" || :
[[ -f "src/Main/veracrypt" ]] &&  install -p -D -m 0755 "src/Main/veracrypt"  "%{buildroot}%{_bindir}/%{name}" || :

for s in {16,48,128,256} ; do
  [[ -f "src/Resources/Icons/VeraCrypt-${s}x${s}.png" ]] || continue
  install -p -D -m 0644 "src/Resources/Icons/VeraCrypt-${s}x${s}.png" "%{buildroot}%{_datadir}/icons/hicolor/${s}x${s}/apps/%{name}.png"
done

for e in {xpm,png} ; do
  [[ -f "src/Resources/Icons/VeraCrypt-256x256.${e}" ]] || continue
  install -p -D -m 0644 "src/Resources/Icons/VeraCrypt-256x256.${e}" "%{buildroot}%{_datadir}/pixmaps/%{name}.${e}"
done

%post
/bin/touch --no-create "%{_datadir}/icons/hicolor" &>/dev/null || :
/usr/bin/update-mime-database "%{_datadir}/mime" &>/dev/null || :
update-desktop-database &>/dev/null || :
/sbin/ldconfig

%postun
if [[ "${1}" -eq "0" ]] ; then
  /bin/touch --no-create "%{_datadir}/icons/hicolor" &>/dev/null
  /usr/bin/gtk-update-icon-cache "%{_datadir}/icons/hicolor" &>/dev/null || :
  /usr/bin/update-mime-database "%{_datadir}/mime" &>/dev/null || :
fi
update-desktop-database &>/dev/null || :
/sbin/ldconfig

%posttrans
/usr/bin/gtk-update-icon-cache "%{_datadir}/icons/hicolor" &>/dev/null || :
/usr/bin/update-mime-database %{?update_mime_database_n:-n} %{_datadir}/mime &>/dev/null || :

%files
%if 0%{?doc_license}
%license %{license_files}
%else
%doc %{license_files}
%endif
%doc README.md doc/chm/VeraCrypt_User_Guide.chm doc/EFI-DCS/ doc/html/
%{_bindir}/%{name}
#%{_datadir}/%{name}
%{_datadir}/applications/*.desktop
%{_datadir}/pixmaps/*
%{_datadir}/icons/hicolor/*/apps/*

%changelog
* Thu Nov 02 2017 Tomasz Tomasik <scx.mail@gmail.com> - 1.21-3
- Add additional macros for wxGTK
- Add post, postun and posttrans sections
- Rebuild for EL7

* Tue Oct 24 2017 Tomasz Tomasik <scx.mail@gmail.com> - 1.21-2
- Fix for Fedora < 26
- Rebuild for EL7

* Tue Oct 24 2017 Tomasz Tomasik <scx.mail@gmail.com> - 1.21-1
- Update to version 1.21
- Update License and URL
- Change source
- Backport patch from GitHub: https://github.com/veracrypt/VeraCrypt/issues/198
- Rewrite prep, build and install sections
- Use doc and license macro
- Rebuild for EL7

* Mon Oct 23 2017 Tomasz Tomasik <scx.mail@gmail.com> - 1.19-5
- Update changelog
- Rebuild for EL7

* Mon Apr 24 2017 Tomasz Tomasik <scx.mail@gmail.com> - 1.19-4
- Update requirements
- Rewrite build and install sections
- Rebuilt for EL7

* Thu Nov 17 2016 Huaren Zhong <huaren.zhong@gmail.com> - 1.19
- Rebuild for Fedora

* Tue Oct 18 2016 Denis Silakov <denis.silakov@rosalab.ru> - 1.19-1
- (9623fad) Merge pull request #4 from tremod/veracrypt:rosa2016.1
- (9623fad) Update to 1.19

* Sun Oct 18 2015 Denis Silakov <dsilakov@gmail.com> - 1.16-1
- (eac346d) Updated to 1.16