<?xml version="1.0" encoding="UTF-8"?>
<project name="oStorybook" default="default" basedir=".">
    <description>Builds, tests, and runs the project Storybook.</description>
    <import file="nbproject/build-impl.xml"/>
    <property name="sb.version" value="5.60" />
    <property name="sb.distdir" value="${basedir}/distrib/${sb.version}" />
    <property name="sb.distzip" value="${sb.distdir}/oStorybook-${sb.version}.zip" />
    <property name="sb.file.name" value="oStorybook" />
    <property name="sb.jar.name" value="${sb.file.name}.jar" />
    <property name="sb.linux.deployment.dir" value="${basedir}/deployment/linux" />
    <property name="sb.debian.distdir" value="${sb.distdir}/debian-package" />
    <property name="sb.debian.deployment.dir" value="${sb.linux.deployment.dir}/debian" />
    <property name="sb.debian.app" value="${sb.debian.distdir}/usr/share/ostorybook"/>
    <property name="sb.src.zip.name" value="${sb.distdir}/oStorybook_src-${sb.version}.zip" />
    <property name="sb.lib.zip.name" value="${basedir}/distrib/oStorybook_libs.zip" />
    <!-- mac specific build parameters -->
    <property name="sb.mac.deployment.dir" value="${sb.deployment.dir}/macintosh" />
    <property name="sb.mac.installer.mount.dir" value="/Volumes/tmpStorybook-${TODAY}" />
    <property name="sb.mac.installer.template" value="Storybook_template.dmg" />
    <!-- jarbundler properties -->
    <property name="jarbundler.name" value="${ant.project.name}" />
    <property name="jarbundler.shortname" value="${ant.project.name}" />
    <property name="jarbundler.signature" value="${ant.project.name}, oStorybook.eu" />
    <property name="jarbundler.mainclass" value="storybook.SbApp" />
    <property name="jarbundler.icon" value="storybook-icon.icns" />
    <property name="jarbundler.jvmversion" value="1.8+" />
    <property name="jarbundler.version" value="${sb.version}" />
    <property name="jarbundler.infostring" value="${ant.project.name} ${sb.version}, ${TODAY}" />
    <property name="jarbundler.build" value="" />
    <property name="jarbundler.bundleid" value="storybook.SbApp" />
    <property name="jarbundler.apple.laf.useScreenMenuBar" value="true" />
    <property name="jarbundler.apple.awt.brushMetal" value="true" />
    <property name="jarbundler.apple.awt.showGrowBox" value="true" />
    <property name="jarbundler.apple.awt.textantialiasing" value="true" />

    <!-- ========================================================================================= -->
    <target name="-post-clean">
        <echo level="info" message="******** start of -post-clean"/>
        <!-- <delete dir="${sb.distdir}"/> -->
        <echo level="info" message="******** end of -post-clean"/>
    </target>
    <!-- ========================================================================================= -->
    <target name="-pre-compile">
        <echo level="info" message="******** start of -pre-compile"/>
        <tstamp>
            <format property="time.stamp" pattern="yyyy-MM-dd HH:mm:ss"/>
        </tstamp>
        <propertyfile file="${src.src.dir}/buildinfo.properties">
            <entry key="build.date" value="${time.stamp}"/>
        </propertyfile>
        <echo level="info" message="******** end of -pre-compile"/>
    </target>
    <target name="-post-compile">
        <echo level="info" message="******** start of -post-compile"/>
        <copy file="actions.xml" todir="${build.classes.dir}" />
        <echo level="info" message="******** end of -post-compile"/>
    </target>
    <!-- ========================================================================================= -->
    <target name="build-prepare">
        <echo level="info" message="******** start of build-prepare"/>
        <copy file="ABOUT.txt" todir="${dist.dir}" />
        <copy file="LICENSE.txt" todir="${dist.dir}" />
        <copy file="LICENSE-fr.txt" todir="${dist.dir}" />
        <copy file="README.txt" todir="${dist.dir}" />
        <copy file="THANKS.txt" todir="${dist.dir}" />
        <copy file="splash.png" todir="${dist.dir}" />
        <copy file="oStorybook-icon.icns" todir="${dist.dir}" />
        <copy file="oStorybook-icon.ico" todir="${dist.dir}" />
        <copy file="oStorybook-icon.png" todir="${dist.dir}" />
        <copy file="oStorybook.bat" todir="${dist.dir}" />
        <copy file="oStorybook.sh" todir="${dist.dir}" />
        <copy file="Versions.txt" todir="${dist.dir}" />
        <mkdir dir="${dist.dir}" />
        <mkdir dir="${dist.dir}/Assistant" />
        <copy todir="${dist.dir}/Assistant" flatten="false">
            <fileset dir="${basedir}/Assistant" />
        </copy>
        <echo level="info" message="******** end of build-prepare"/>
    </target>
    <!-- ========================================================================================= -->
    <target name="build-app-zip" depends="jar, build-prepare" >
        <echo level="info" message="******** start of build-app-zip"/>
        <mkdir dir="${dist.dir}" />
        <mkdir dir="${sb.distdir}" />
        <zip destfile="${sb.distzip}" basedir="${basedir}/dist" />
        <echo level="info" message="******** end of build-app-zip"/>
    </target>
    <!-- ========================================================================================= -->
    <target name="build-app-libs" depends="jar, build-prepare" >
        <echo level="info" message="******** start of build-app-libs"/>
        <echo>delete the old libs zip file...</echo>
        <delete file="${sb.lib.zip.name}"/>
        <echo>make the libs zip file...</echo>
        <mkdir dir="${basedir}/distrib" />
        <zip destfile="${sb.lib.zip.name}" basedir="${basedir}/lib" />
        <echo level="info" message="******** end of build-app-libs"/>
    </target>
    <!-- ========================================================================================= -->
    <target name="build-app-src" depends="build-app-libs">
        <echo level="info" message="******** start of build-app-src"/>
        <echo>make the src zip file...</echo>
        <delete file="${sb.src.zip.name}"/>
        <zip zipfile="${sb.src.zip.name}">
            <zipfileset dir="${basedir}">
                <include name="src/**" />
                <include name="build.xml" />
                <include name="ABOUT.txt" />
                <include name="LICENSE.txt" />
                <include name="README.txt" />
                <include name="ROADMAP.html" />
                <include name="THANKS.txt" />
                <include name="Versions.txt" />
                <include name="oStorybook-*.*" />
                <include name="splash.png" />
            </zipfileset>
        </zip>
        <echo level="info" message="******** end of build-app-src"/>
    </target>
    <!-- ========================================================================================= -->
    <target name="build-jarbundler" depends="jar" >
        <echo level="info" message="******** start of build-jarbundler"/>
        <taskdef name="jarbundler"
                     classname="net.sourceforge.jarbundler.JarBundler"
                     classpath="lib/jarbundler-2.2.0.jar" />
        <jarbundler dir="${sb.distdir}"
                        name="${jarbundler.name}"
                        shortname="${jarbundler.shortname}"
                        signature="${jarbundler.signature}"
                        mainclass="${jarbundler.mainclass}"
                        icon="${jarbundler.icon}"
                        jvmversion="${jarbundler.jvmversion}"
                        version="${jarbundler.version}"
                        infostring="${jarbundler.infostring}"
                        build="${jarbundler.build}"
                        bundleid="${jarbundler.bundleid}"
                        workingdirectory="$$APP_PACKAGE/Contents/Resources/Java">
            <jarfileset dir="${dist.dir}">
                <include name="*.txt" />
                <include name="log4j.xml" />
                <include name="log4j.dtd" />
                <include name="${sb.jar.name}" />
                <include name="lib/**" />
                <!-- <include name="resources/**" /> -->
                <include name="splash.png" />
                <include name="oStorybook-icon.icns" />
                <include name="oStorybook-icon.ico" />
                <include name="oStorybook-icon.png" />
            </jarfileset>
            <javaproperty name="apple.laf.useScreenMenuBar"
                                  value="${jarbundler.apple.laf.useScreenMenuBar}" />
            <javaproperty name="apple.awt.brushMetal"
                                  value="${jarbundler.apple.awt.brushMetal}" />
            <javaproperty name="apple.awt.showGrowBox"
                                  value="${jarbundler.apple.awt.showGrowBox}" />
            <javaproperty name="apple.awt.textantialiasing"
                                  value="${jarbundler.apple.awt.textantialiasing}" />
        </jarbundler>
        <echo level="info" message="******** end of build-jarbundler"/>
    </target>
    <!-- ========================================================================================= -->
    <target name="build-app-mac" depends="init,jar,build-prepare,build-jarbundler">
        <echo level="info" message="******** start of build-app-mac"/>
        <property name="sb.macfile" value="${sb.distdir}/oStorybook" />
        <tar basedir="${sb.distdir}/${jarbundler.name}.app" destfile="${sb.macfile}.tar" />
        <gzip src="${sb.macfile}.tar" destfile="${sb.macfile}-${sb.version}-macOSx.tar.gz"/>
        <delete file="${sb.macfile}.tar"/>
        <delete dir="${sb.distdir}/${jarbundler.name}.app"/>
        <echo level="info" message="******** end of build-app-mac"/>
    </target> 
    <!-- ========================================================================================= -->
    <target name="build-app-windows" depends="jar, build-prepare, build-app-zip">
        <echo level="info" message="******** start of build-app-windows"/>
        <echo level="info" message="build-app-zip utilisable seulement si NSIS a été installé (voir la documentation)"/>
        <exec executable="wine">
            <arg value="../../nsis/makensis.exe" />
            <arg value="${build.dir}/../deployment/windows/nsis/oStorybook.nsi" />
        </exec>
        <move file="dist/oStorybook-installer.exe" tofile="${sb.distdir}/oStorybook-${sb.version}.exe" />
        <echo level="info" message="******** end of build-app-windows"/>
    </target>
    <!-- ========================================================================================= -->
    <target name="build-app-debian" depends="jar,build-prepare">
        <echo level="info" message="******** start of build-app-debian"/>
        <echo level="info">Prepare DEBIAN package build</echo>
        <echo file="${sb.linux.deployment.dir}/build-debian.sh" append="false" force="true">#!/bin/sh
            echo Création de la préparation pour la version ${sb.version}
            echo Paramètres:
            echo "- sb.version : " ${sb.version}
            echo "- basedir : " ${basedir}
            echo "- sb.debian.distdir : " ${sb.debian.distdir}
            echo "- sb.debian.app : " ${sb.debian.app}
            echo "- sb.debian.deployment.dir : " ${sb.debian.deployment.dir}
            mkdir ${sb.debian.distdir}
            echo Copie debian-package
            cp -R ${sb.debian.deployment.dir}/DEBIAN ${sb.debian.distdir}
            cp -R ${sb.debian.deployment.dir}/usr ${sb.debian.distdir}
            sed -i -e "s/@version@/${sb.version}/g" ${sb.debian.distdir}/DEBIAN/control
            sed -i -e "s/@version@/${sb.version}/g" ${sb.debian.distdir}/usr/share/applications/ostorybook.desktop
            sed -i -e "s/@version@/${sb.version}/g" ${sb.debian.distdir}/usr/share/doc/ostorybook/changelog
            gzip -9 ${sb.debian.distdir}/usr/share/doc/ostorybook/changelog
            echo Copie application
            mkdir ${sb.debian.app}
            cp -R ${basedir}/dist/* ${sb.debian.app}
            rm ${sb.debian.distdir}/usr/share/ostorybook/LICENSE.txt
            echo Modification des droits
            find ${sb.debian.distdir}/usr -type d -exec chmod 755 {} +
            chmod -R 755 ${sb.debian.distdir}/DEBIAN
            echo Construction du paquet DEBIAN
            dpkg-deb --build ${sb.debian.distdir} ${sb.distdir}/oStorybook-${sb.version}.deb
            echo Nettoyage...
            rm -r -f ${sb.debian.distdir}
            echo Construction du paquet RPM
            cd ${sb.distdir}
            alien -r ${sb.distdir}/oStorybook-${sb.version}.deb
            mv ${sb.distdir}/ostorybook-${sb.version}*.rpm oStorybook-${sb.version}.rpm</echo>
        <echo level="info" message="Construction DEB et RPM:"/>
        <echo level="info" message="    sudo sh ${sb.linux.deployment.dir}/build-debian.sh"/>
        <echo level="info" message="Installation:"/>
        <echo level="info" message="    sudo dpkg -i ${sb.distdir}/oStorybook-${sb.version}.deb"/>
        <echo level="info" message="******** end of build-app-debian"/>
    </target>
    <target name="build-app-linux" depends="jar,build-prepare" >
        <echo level="info" message="******** start of build-app-linux"/>
        <mkdir dir="${sb.distdir}" />
        <exec executable="sh">
            <arg value="deployment/linux/other/makeself.sh" />
            <arg value="${basedir}/dist" />
            <arg value="${sb.distdir}/oStorybook-${sb.version}-linux.bin" />
            <arg value="oStorybook" />
            <arg value="./setup.sh" />
        </exec>
        <echo level="info" message="******** end of build-app-linux"/>
    </target>
    <target name="build-app-all"
                depends="build-app-linux, build-app-zip, build-app-mac, build-app-windows, build-app-src, build-app-debian">
        <echo level="info" message="******** start of build-app-all"/>
        <copy file="Versions.txt" todir="${sb.distdir}" />
        <exec executable="ls" failonerror="true"> 
            <arg line=" -sh ${dist.jar}"/> 
        </exec> 
        <echo level="info" message="******** end of build-app-all"/>
    </target>
    <!-- ========================================================================================= -->
    <target name="-post-jar">
        <echo level="info" message="******** start of -post-jar"/>
        <!-- <echo>copy resources ...</echo>
            <copy todir="${dist.dir}/resources">
            <fileset dir="${build.dir}/resources">
                <exclude name="**/language.properties"/>
            </fileset>
            </copy>
        -->
        <echo level="info" message="******** end of -post-jar"/>
    </target>
    <target name="build-app-statistics">
        <echo level="info" message="******** start of statistics"/>
        <echo level="info" message="Global statistics"/>
        <exec executable="cloc" failonerror="true"> 
            <arg line=" --progress-rate=0 src"/> 
        </exec> 
        <echo level="info" message="oStorybook only statistics"/>
        <exec executable="cloc" failonerror="true"> 
            <arg line=" --progress-rate=0 src/storybook"/> 
        </exec> 
        <echo level="info" message="assistant only statistics"/>
        <exec executable="cloc" failonerror="true"> 
            <arg line=" --progress-rate=0 src/assistant"/> 
        </exec> 
        <echo level="info" message="i18n only statistics"/>
        <exec executable="cloc" failonerror="true"> 
            <arg line=" --progress-rate=0 src/i18n"/> 
        </exec> 
        <echo level="info" message="resources only statistics"/>
        <exec executable="cloc" failonerror="true"> 
            <arg line=" --progress-rate=0 src/resources"/> 
        </exec> 
        <echo level="info" message="shef only statistics"/>
        <exec executable="cloc" failonerror="true"> 
            <arg line=" --progress-rate=0 src/shef"/> 
        </exec> 
        <echo level="info" message="******** end of statistics"/>
    </target>
</project>
