###############################################################################
#                                                                             #
# IPFire.org - A linux based firewall                                         #
# Copyright (C) 2007-2018  IPFire Team  <info@ipfire.org>                     #
#                                                                             #
# This program is free software: you can redistribute it and/or modify        #
# it under the terms of the GNU General Public License as published by        #
# the Free Software Foundation, either version 3 of the License, or           #
# (at your option) any later version.                                         #
#                                                                             #
# This program 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 General Public License for more details.                                #
#                                                                             #
# You should have received a copy of the GNU General Public License           #
# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
#                                                                             #
###############################################################################

###############################################################################
# Definitions
###############################################################################

include Config

VER        = 1.1.11

THISAPP    = cdrkit-$(VER)
DL_FILE    = $(THISAPP).tar.gz
DL_FROM    = $(URL_IPFIRE)
DIR_APP    = $(DIR_SRC)/$(THISAPP)
TARGET     = $(DIR_INFO)/$(THISAPP)

CFLAGS    += -fcommon

###############################################################################
# Top-level Rules
###############################################################################

objects = $(DL_FILE)

$(DL_FILE) = $(DL_FROM)/$(DL_FILE)

$(DL_FILE)_BLAKE2 = a1f507819508d0489aba908e1030d5508643cfb48072c6ea929fa5a6516848a34fc41acd2d8952d99e264192170553d45a71700041a07342aa038973c4b69269

install : $(TARGET)

check : $(patsubst %,$(DIR_CHK)/%,$(objects))

download :$(patsubst %,$(DIR_DL)/%,$(objects))

b2 : $(subst %,%_BLAKE2,$(objects))

###############################################################################
# Downloading, checking, b2sum
###############################################################################

$(patsubst %,$(DIR_CHK)/%,$(objects)) :
	@$(CHECK)

$(patsubst %,$(DIR_DL)/%,$(objects)) :
	@$(LOAD)

$(subst %,%_BLAKE2,$(objects)) :
	@$(B2SUM)

###############################################################################
# Installation Details
###############################################################################

$(TARGET) : $(patsubst %,$(DIR_DL)/%,$(objects))
	@$(PREBUILD)
	@rm -rf $(DIR_APP) && cd $(DIR_SRC) && tar zxf $(DIR_DL)/$(DL_FILE)
	cd $(DIR_APP) && patch -Np1 < $(DIR_SRC)/src/patches/cdrkit/cdrkit-1.1.8-werror.patch
	cd $(DIR_APP) && patch -Np1 < $(DIR_SRC)/src/patches/cdrkit/cdrkit-1.1.9-buffer_overflow.patch
	cd $(DIR_APP) && patch -Np1 < $(DIR_SRC)/src/patches/cdrkit/cdrkit-1.1.9-no_mp3.patch
	cd $(DIR_APP) && patch -Np1 < $(DIR_SRC)/src/patches/cdrkit/cdrkit-1.1.9-efi-boot.patch
	cd $(DIR_APP) && patch -Np1 < $(DIR_SRC)/src/patches/cdrkit/cdrkit-1.1.10-build-fix.patch
	cd $(DIR_APP) && patch -Np1 < $(DIR_SRC)/src/patches/cdrkit/cdrkit-1.1.11-cmakewarn.patch
	cd $(DIR_APP) && patch -Np1 < $(DIR_SRC)/src/patches/cdrkit/cdrkit-1.1.11-devname.patch
	cd $(DIR_APP) && patch -Np1 < $(DIR_SRC)/src/patches/cdrkit/cdrkit-1.1.11-dvdman.patch
	cd $(DIR_APP) && patch -Np1 < $(DIR_SRC)/src/patches/cdrkit/cdrkit-1.1.11-format.patch
	cd $(DIR_APP) && patch -Np1 < $(DIR_SRC)/src/patches/cdrkit/cdrkit-1.1.11-handler.patch
	cd $(DIR_APP) && patch -Np1 < $(DIR_SRC)/src/patches/cdrkit/cdrkit-1.1.11-manpagefix.patch
	cd $(DIR_APP) && patch -Np1 < $(DIR_SRC)/src/patches/cdrkit/cdrkit-1.1.11-memset.patch
	cd $(DIR_APP) && patch -Np1 < $(DIR_SRC)/src/patches/cdrkit/cdrkit-1.1.11-paranoiacdda.patch
	cd $(DIR_APP) && patch -Np1 < $(DIR_SRC)/src/patches/cdrkit/cdrkit-1.1.11-ppc64le_elfheader.patch
	cd $(DIR_APP) && patch -Np1 < $(DIR_SRC)/src/patches/cdrkit/cdrkit-1.1.11-readsegfault.patch
	cd $(DIR_APP) && patch -Np1 < $(DIR_SRC)/src/patches/cdrkit/cdrkit-1.1.11-rootstat.patch
	cd $(DIR_APP) && patch -Np1 < $(DIR_SRC)/src/patches/cdrkit/cdrkit-1.1.11-usalinst.patch
	cd $(DIR_APP) && patch -Np1 < $(DIR_SRC)/src/patches/cdrkit/cdrkit-1.1.11-utf8.patch
	cd $(DIR_APP) && patch -Np1 < $(DIR_SRC)/src/patches/cdrkit/cdrkit-1.1.11-werror_gcc5.patch

	cd $(DIR_APP) && sed -i CMakeLists.txt -e "s/icedax//g"

	cd $(DIR_APP) && mkdir -pv build
	cd $(DIR_APP)/build && cmake .. \
		-DCMAKE_INSTALL_PREFIX:PATH=/usr \
		-DBUILD_SHARED_LIBS:BOOL=ON
	cd $(DIR_APP)/build && make $(MAKETUNING)
	cd $(DIR_APP)/build && make install

	ln -svf genisoimage /usr/bin/mkisofs
	ln -svf genisoimage /usr/bin/mkhybrid
	ln -svf icedax /usr/bin/cdda2wav
	ln -svf wodim /usr/bin/cdrecord
	ln -svf wodim /usr/bin/dvdrecord

	@rm -rf $(DIR_APP)
	@$(POSTBUILD)
