#
# Copyright (C) 2007-2008 Arnold Krille
# Copyright (C) 2007-2008 Pieter Palmers
#
# This file is part of FFADO
# FFADO = Free Firewire (pro-)audio drivers for linux
#
# FFADO is based upon FreeBoB.
#
# 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 2 of the License, or
# (at your option) version 3 of the License.
#
# 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/>.
#

# from: http://www.qandr.org/quentin/writings/debscons.html
import os

Import('env') # exported by parent SConstruct

# Here's the core info for the package

DEBNAME = "libffado0"
if env['REVISION']:
    DEBVERSION = "%s-%s" % (env['VERSION'], env['REVISION'])
else:
    DEBVERSION = env['VERSION']

DEBMAINT = "Pieter Palmers [pieter.palmers@ffado.org]"
DEBARCH = "i386"
DEBDEPENDS = "libraw1394-8 (>= 1.3.0), libiec61883-0 (>= 1.1.0), libavc1394-0 (>= 0.5.3), dbus (>= 1.1.0)" # what are we dependent on?
DEBDESC = "FFADO: FireWire audio for Linux (Development build SVN r%s)" % env['REVISION']

DEBFILES = [
    # Now we specify the files to be included in the .deb
    # Where they should go, and where they should be copied from.
    # If you have a lot of files, you may wish to generate this
    # list in some other way.
    ("usr/lib/libffado.so",             "#src/libffado.so"),
    ("usr/lib/pkgconfig/libffado.pc",             "#/libffado.pc"),
    ("usr/include/libffado/ffado.h",             "#libffado/ffado.h"),
    ("usr/bin/ffado-dbus-server", "#support/dbus/ffado-dbus-server"),
]

DEBFILES.append(("usr/share/libffado/ffado_driver_genericavc.txt",  "#src/genericavc/ffado_driver_genericavc.txt"))


if env['ENABLE_BEBOB']:
    DEBFILES.append(("usr/share/libffado/ffado_driver_bebob.txt",  "#src/bebob/ffado_driver_bebob.txt"))
    DEBFILES.append(("usr/bin/ffado-bridgeco-downloader", "#support/firmware/ffado-bridgeco-downloader"))
    #DEBFILES.append(("usr/share/libffado/fw410.xml", "#src/bebob/maudio/fw410.xml"))
    #DEBFILES.append(("usr/share/libffado//fwap.xml","#src/bebob/maudio/fwap.xml"))
    #DEBFILES.append(("usr/share/libffado//refdesign.xml","#src/bebob/maudio/refdesign.xml"))
if env['ENABLE_FIREWORKS']:
    DEBFILES.append(("usr/share/libffado/ffado_driver_fireworks.txt",  "#src/fireworks/ffado_driver_fireworks.txt"))
    DEBFILES.append(("usr/bin/ffado-fireworks-downloader", "#support/firmware/ffado-fireworks-downloader"))
if env['ENABLE_MOTU']:
    pass
if env['ENABLE_DICE']:
    pass
if env['ENABLE_METRIC_HALO']:
    pass
if env['ENABLE_RME']:
    pass
if env['ENABLE_BOUNCE']:
    pass



# This is the debian package we're going to create
debpkg = '#%s_%s_%s.deb' % (DEBNAME, DEBVERSION, DEBARCH)

# and we want it to be built when we build 'debian'
env.Alias("debian", debpkg)

DEBCONTROLFILE = os.path.join(DEBNAME, "DEBIAN/control")

# This copies the necessary files into place into place.
# Fortunately, SCons creates the necessary directories for us.
for f in DEBFILES:
    # We put things in a directory named after the package
    dest = os.path.join(DEBNAME, f[0])
    # The .deb package will depend on this file
    env.Depends(debpkg, dest)
    # Copy from the the source tree.
    env.Command(dest, f[1], Copy('$TARGET','$SOURCE'))
    # The control file also depends on each source because we'd like
    # to know the total installed size of the package
    env.Depends(DEBCONTROLFILE, dest)

# Now to create the control file:

CONTROL_TEMPLATE = """
Package: %s
Priority: extra
Section: misc
Installed-Size: %s
Maintainer: %s
Architecture: %s
Version: %s
Depends: %s
Description: %s

"""
env.Depends(debpkg,DEBCONTROLFILE )

# The control file should be updated when the SVN version changes
env.Depends(DEBCONTROLFILE, env.Value(env['REVISION']))

# This function creates the control file from the template and info
# specified above, and works out the final size of the package.
def make_control(target=None, source=None, env=None):
    installed_size = 0
    for i in DEBFILES:
        installed_size += os.stat(str(env.File(i[1])))[6]
    control_info = CONTROL_TEMPLATE % (
        DEBNAME, installed_size, DEBMAINT, DEBARCH, DEBVERSION,
        DEBDEPENDS, DEBDESC)
    with open(str(target[0]), 'w') as f:
        f.write(control_info)

# We can generate the control file by calling make_control
env.Command(DEBCONTROLFILE, None, make_control)

# And we can generate the .deb file by calling dpkg-deb
env.Command(debpkg, DEBCONTROLFILE,
            "dpkg-deb -b %s %s" % ("deb/%s" % DEBNAME, "$TARGET"))
