##
##  dss.spec -- OpenPKG RPM Package Specification
##  Copyright (c) 2000-2025 OpenPKG Project <http://openpkg.org/>
##
##  Permission to use, copy, modify, and distribute this software for
##  any purpose with or without fee is hereby granted, provided that
##  the above copyright notice and this permission notice appear in all
##  copies.
##
##  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
##  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
##  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
##  IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
##  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
##  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
##  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
##  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
##  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
##  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
##  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
##  SUCH DAMAGE.
##

#   package version
%define       V_openpkg 5.5.1
%define       V_dist    5.5

#   package information
Name:         dss
Summary:      Darwin Streaming Server
URL:          http://developer.apple.com/darwin/projects/streaming/
Vendor:       Apple Computer, Incorporated
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Video
License:      APSL
Version:      %{V_openpkg}
Release:      20080101

#   list of sources
Source0:      http://www.opensource.apple.com/projects/streaming/release/DarwinStreamingSrvr%{V_dist}-Source.tar
Source1:      rc.dss
%NoSource     0

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, perl, make, gcc, sed
PreReq:       OpenPKG, openpkg >= 20160101, perl, perl-ssl

%description
    The Darwin Streaming Server is server technology which allows you
    to send streaming QuickTime data to clients across the Internet
    using the industry standard RTP and RTSP protocols. It is based on
    the same code as Apple's QuickTime Streaming Server.

%track
    prog dss = {
        version   = %{V_dist}
        url       = http://developer.apple.com/darwin/projects/streaming/
        regex     = DarwinStreamingSrvr(__VER__)-Source\.tar
    }

%prep
    %setup -q -n DarwinStreamingSrvr%{V_dist}-Source

    #   just a few corrections to source files
    %{l_shtool} subst \
        -e 's;/etc/streaming;%{l_prefix}/etc/dss;g' \
        defaultPaths.h \
        WebAdmin/src/streamingadminserver.pl \
        WebAdmin/streamingadminserver_Darwin.conf
    %{l_shtool} subst \
        -e 's;streamingadminserver.pl;streamingadminserver;g' \
        WebAdmin/StartupItems.bproj/QuickTimeStreamingServer \
        WebAdmin/src/streamingadminserver.pl
    %{l_shtool} subst \
        -e 's;#!/usr/bin/perl;#! %{l_prefix}/bin/perl;g' \
        -e 's;/var/streaming/AdminHtml;%{l_prefix}/var/dss/webadmin;g' \
        -e 's;/var/streaming/playlists/;%{l_prefix}/share/dss/playlists;g' \
        -e 's;"realm", "DSS Admin Server";"realm", "Streaming Admin Server";g' \
        -e 's;/usr/local/sbin/DarwinStreamingServer;%{l_prefix}/sbin/DarwinStreamingServer;g' \
        -e 's;/var/streaming/logs/streamingadminserver.log;%{l_prefix}/var/dss/log/StreamingAdmin.log;g' \
        -e 's;/usr/local/bin/qtpasswd;%{l_prefix}/bin/qtpasswd;g' \
        -e 's;/usr/local/bin/PlaylistBroadcaster;%{l_prefix}/bin/PlaylistBroadcaster;g' \
        -e 's;/usr/local/bin/MP3Broadcaster;%{l_prefix}/bin/MP3Broadcaster;g' \
        -e 's;/etc/streaming/streamingadminserver.pem;%{l_prefix}/etc/dss/streamingadminserver.pem;g' \
        -e 's;"qtssAdmin", "streamingadmin";"qtssAdmin", "dssadmin";g' \
        -e 's;/var/run/streamingadminserver.pid;%{l_prefix}/var/dss/dssadmin.pid;g' \
        -e 's;"runUser", "qtss";"runUser", "%{l_musr}";g' \
        -e 's;"runGroup", "qtss";"runGroup", "%{l_musr}";g' \
        WebAdmin/src/streamingadminserver.pl
    %{l_shtool} subst \
        -e 's;\t;    ;g' \
        -e 's; *$;;g' \
        qtaccess \
        relayconfig.xml-Sample \
        streamingserver.xml-POSIX \
        WebAdmin/streamingadminserver.conf
    %{l_shtool} subst \
        -e 's;/var/streaming/logs;%{l_prefix}/var/dss/log;g' \
        -e 's;/var/run/DarwinStreamingServer.pid;%{l_prefix}/var/dss/dssdaemon.pid;g' \
        -e 's;/usr/local/sbin/StreamingServerModules;%{l_prefix}/share/dss/modules;g' \
        -e 's;/usr/local/movies;%{l_prefix}/share/dss/movies;g' \
        -e 's;/etc/streaming/relayconfig.xml;%{l_prefix}/etc/dss/relayconfig.xml;g' \
        -e 's;/etc/streaming/qtgroups;%{l_prefix}/etc/dss/qtgroups;g' \
        -e 's;/etc/streaming/qtusers;%{l_prefix}/etc/dss/qtusers;g' \
        -e 's;"error_logfile_name">Error;"error_logfile_name">error;g' \
        -e 's;"request_logfile_name">StreamingServer;"request_logfile_name">streamingserver;g' \
        -e 's;http_logfile_name">Http;http_logfile_name">http;g' \
        -e 's;"bind_ip_addr">0;"bind_ip_addr">127.0.0.1;g' \
        -e 's;"run_user_name"></PREF>;"run_user_name">%{l_musr}</PREF>;g' \
        -e 's;"run_group_name"></PREF>;"run_group_name">%{l_mgrp}</PREF>;g' \
        streamingserver.xml-POSIX
    %{l_shtool} subst \
        -e 's;qtssAutoStart=1;qtssAutoStart=0;g' \
        -e "s;host=foo.bar.com;host=`%{l_shtool} echo -e %h%d`;g" \
        -e 's;/Library/QuickTimeStreaming/Logs/streamingadminserver.log;%{l_prefix}/var/dss/log/StreamingAdmin.log;g' \
        -e 's;/Library/QuickTimeStreaming/AdminHtml;%{l_prefix}/var/dss/webadmin;g' \
        -e 's;/Library/QuickTimeStreaming/Playlists;%{l_prefix}/share/dss/playlists;g' \
        -e 's;/usr/sbin/QuickTimeStreamingServer;%{l_prefix}/sbin/DarwinStreamingServer;g' \
        -e 's;streamingadminserver.conf;streamingadmin.conf;g' \
        WebAdmin/streamingadminserver.conf
    %{l_shtool} subst \
        -e 's;^#!/.*/perl;#! %{l_prefix}/bin/perl;' \
        WebAdmin/WebAdminHtml/*.pl \
        WebAdmin/WebAdminHtml/*.cgi \
        WebAdmin/src/streamingadminserver.pl
    %{l_shtool} subst \
        -e 's;^AuthScheme .*;AuthScheme digest;g' \
        -e 's;^AuthName .*;AuthName "Streaming Server";g' \
        -e 's;^AuthUserFile .*;AuthUserFile %{l_prefix}/etc/dss/qtusers;g' \
        -e 's;^AuthGroupFile .*;AuthGroupFile %{l_prefix}/etc/dss/qtgroups;g' \
        -e 's;^require user.*;require any-user;g' \
        -e 's;^\(require group .*\=;#\1;g' \
        -e 's;\(<Limit WRITE>\);#\1;g' \
        -e 's;^\( AuthName "streaming server broadcaster realm"\);#\1;g' \
        -e 's;^\( AuthUserFile /Library/QuickTimeStreaming/Config/qtbroadcastusers\);#\1;g' \
        -e 's;^\( AuthGroupFile /Library/QuickTimeStreaming/Config/qtbroadcastgroups\);#\1;g' \
        -e 's;^ \(require group broadcaster\);#\1;g' \
        -e 's;\(</Limit>\);#\1;g' \
        qtaccess
    %{l_shtool} subst \
        -e 's;if ($wt == "");if ($wt eq "");g' \
        WebAdmin/WebAdminHtml/playlist-lib.pl
    %{l_shtool} subst \
        -e 's;\(\\r\\nConnection:close\\r\\n\);\\r\\n\1;g' \
        WebAdmin/WebAdminHtml/cgi-lib.pl
    %{l_shtool} echo \
        '# OpenPKG additional' \
        >>WebAdmin/streamingadminserver.conf
    %{l_shtool} echo \
        'keyfile=%{l_prefix}/etc/dss/streamingadminserver.pem' \
        >>WebAdmin/streamingadminserver.conf
    %{l_shtool} echo \
        'crtfile=%{l_prefix}/etc/dss/streamingadminserver.pem' \
        >>WebAdmin/streamingadminserver.conf
    %{l_shtool} echo \
        'qtssQTPasswd=%{l_prefix}/bin/qtpasswd' \
        >>WebAdmin/streamingadminserver.conf
    %{l_shtool} echo \
        'qtssPlaylistBroadcaster=%{l_prefix}/bin/PlaylistBroadcaster' \
        >>WebAdmin/streamingadminserver.conf
    %{l_shtool} echo \
        'qtssMP3Broadcaster=%{l_prefix}/bin/MP3Broadcaster' \
        >>WebAdmin/streamingadminserver.conf
    %{l_shtool} echo \
        'pidfile=%{l_prefix}/var/dss/dssadmin.pid' \
        >>WebAdmin/streamingadminserver.conf

%build
    #   standard OpenPKG build variables
    CC="%{l_cc}"
    CXX="%{l_cxx}"
    CFLAGS="%{l_cflags -O}"
    CXXFLAGS="%{l_cxxflags -O}"
    CPPFLAGS="%{l_cppflags}"
    LDFLAGS="%{l_ldflags}"
    export CC CXX CFLAGS CXXFLAGS CPPFLAGS LDFLAGS

    #   special Darwin build variables
    CPLUS="g++"
    CCOMP="gcc"
    LINKER="g++"
    MAKE="%{l_make}"
    INCLUDE_FLAG="-include"
    export CPLUS CCOMP LINKER MAKE INCLUDE_FLAG
    case "%{l_platform -t}" in
        *-sunos* )
            COMPILER_FLAGS="-D__solaris__ -D_REENTRANT -L/usr/local/lib -R/usr/local/lib"
            CORE_LINK_LIBS="-lpthread -ldl -lsocket -lnsl -lresolv -lm -lcrypt -lstdc++"
            SHARED=-G
            MODULE_LIBS=
            ;;
        *-freebsd* )
            COMPILER_FLAGS=
            CORE_LINK_LIBS="-pthread -lm -lcrypt"
            SHARED=-shared
            MODULE_LIBS="-lgcc -lstdc++"
            ;;
        *-linux* )
            COMPILER_FLAGS="-D_REENTRANT -D__USE_POSIX -D__linux__ -pipe"
            CORE_LINK_LIBS="-lpthread -ldl -lstdc++ -lm -lcrypt"
            SHARED=-shared
            MODULE_LIBS=
            if [ -f /usr/include/socketbits.h ]; then
                NEED_SOCKETBITS=1
                export NEED_SOCKETBITS
            fi
            ;;
        * )
            echo "platform %{l_platform -t} not supported" 2>&1
            exit 1
            ;;
    esac
    export COMPILER_FLAGS CORE_LINK_LIBS SHARED MODULE_LIBS

    #   build the program parts (order important)
    for dir in \
        CommonUtilitiesLib \
        QTFileLib \
        . \
        APIModules/QTSSDemoAuthorizationModule.bproj \
        APIModules/QTSSRawFileModule.bproj \
        APIModules/QTSSRefMovieModule \
        APIModules/QTSSSpamDefenseModule.bproj \
        HTTPUtilitiesLib \
        StreamingProxy.tproj \
        qtpasswd.tproj \
        PlaylistBroadcaster.tproj \
        MP3Broadcaster \
        QTFileTools/QTBroadcaster.tproj \
        QTFileTools/QTFileInfo.tproj \
        QTFileTools/QTFileTest.tproj \
        QTFileTools/QTRTPFileTest.tproj \
        QTFileTools/QTRTPGen.tproj \
        QTFileTools/QTSampleLister.tproj \
        QTFileTools/QTSDPGen.tproj \
        QTFileTools/QTTrackInfo.tproj
    do
        ( cd $dir && %{l_make} %{l_mflags -O} -f Makefile.POSIX ) || exit $?
    done

%install

    #   make directory structure
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/bin \
        $RPM_BUILD_ROOT%{l_prefix}/sbin \
        $RPM_BUILD_ROOT%{l_prefix}/etc/dss \
        $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d \
        $RPM_BUILD_ROOT%{l_prefix}/share/dss/movies \
        $RPM_BUILD_ROOT%{l_prefix}/share/dss/modules \
        $RPM_BUILD_ROOT%{l_prefix}/share/dss/playlists \
        $RPM_BUILD_ROOT%{l_prefix}/var/dss/log

    #   install the server and administrative binaries
    %{l_shtool} install -s -c -m 755 \
        DarwinStreamingServer $RPM_BUILD_ROOT%{l_prefix}/sbin/
    %{l_shtool} install -c -m 755 \
        WebAdmin/src/streamingadminserver.pl \
        $RPM_BUILD_ROOT%{l_prefix}/sbin/streamingadminserver
    cp -rp WebAdmin/WebAdminHtml $RPM_BUILD_ROOT%{l_prefix}/var/dss/webadmin
    chmod a+x $RPM_BUILD_ROOT%{l_prefix}/var/dss/webadmin/*.cgi

    #   install miscellaneous binaries
    %{l_shtool} install -s -c -m 755 \
        PlaylistBroadcaster.tproj/PlaylistBroadcaster \
        MP3Broadcaster/MP3Broadcaster \
        qtpasswd.tproj/qtpasswd \
        $RPM_BUILD_ROOT%{l_prefix}/bin/

    #   install configuration files
    %{l_shtool} install -c -m 644 \
        relayconfig.xml-Sample \
        streamingserver.xml-POSIX \
        WebAdmin/streamingadminserver.conf \
        $RPM_BUILD_ROOT%{l_prefix}/etc/dss/
    (
        cd $RPM_BUILD_ROOT%{l_prefix}/etc/dss/
        mv -f relayconfig.xml-Sample relayconfig.xml
        mv -f streamingserver.xml-POSIX streamingserver.xml
        mv -f streamingadminserver.conf streamingadmin.conf
        touch qtusers
        touch qtgroups
    ) || exit $?

    #   install run commands
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE rc.dss} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d/

    #   install sample movies, audio files, and access file
    %{l_shtool} install -c -m 644 \
        qtaccess \
        sample.mp3 \
        sample_100kbit.mov \
        sample_300kbit.mov \
        sample_100kbit.mp4 \
        sample_300kbit.mp4 \
        $RPM_BUILD_ROOT%{l_prefix}/share/dss/movies/

    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT \
        %{l_files_std} \
        '%config %{l_prefix}/etc/dss/*' \
        '%config %attr(0640,%{l_susr},%{l_sgrp}) %{l_prefix}/etc/dss/qtusers' \
        '%config %attr(0640,%{l_susr},%{l_sgrp}) %{l_prefix}/etc/dss/qtgroups' \
        '%config %attr(0640,%{l_susr},%{l_mgrp}) %{l_prefix}/share/dss/movies/qtaccess'

%files -f files

%clean

%post
    #   after first time install, write default admin username and
    #   password to qtusers and add the new admin username to qtgroups
    if [ $1 -eq 1 ]; then
        username='ghandi'; password='india'
        echo "realm Streaming Server" >$RPM_INSTALL_PREFIX/etc/dss/qtusers
        $RPM_INSTALL_PREFIX/bin/qtpasswd -p $password $username
        echo "admin: $username" >$RPM_INSTALL_PREFIX/etc/dss/qtgroups
    fi

    #   after upgrade, restart service
    [ $1 -eq 2 ] || exit 0
    eval `%{l_rc} dss status 2>/dev/null`
    [ ".$dss_active" = .yes ] && %{l_rc} dss restart
    exit 0

%preun
    #   before erase, stop service and remove log files
    [ $1 -eq 0 ] || exit 0
    %{l_rc} dss stop 2>/dev/null
    rm -f $RPM_INSTALL_PREFIX/var/dss/log/* >/dev/null 2>&1 || true
    exit 0

