Name:           pbench-web-server
Version:     0.67
%define gdist g0ccc2265
Release:     1%{?gdist}%{!?gdist:}
Summary:        The pbench web server bits

License:        GPLv3+
URL:            http://github.com/distributed-systems-analysis/pbench
Source0:        http://pbench.perf.lab.eng.bos.redhat.com/test-repo/pbench-web-server-%{version}.tar.gz
Buildarch:      noarch

Requires:       npm

%define installdir opt/pbench-web-server
%define static %{installdir}/html/static

%description
The pbench web server bits.

%prep
%setup
%build

%install

rm -rf %{buildroot}

mkdir -p %{buildroot}/%{static}/css
mkdir -p %{buildroot}/%{static}/js

install -m 755 -d %{buildroot}/%{static}/css/v0.2
install -m 755 -d %{buildroot}/%{static}/js/v0.2
install -m 755 -d %{buildroot}/%{static}/css/v0.3
install -m 755 -d %{buildroot}/%{static}/js/v0.3
install -m 755 -d %{buildroot}/%{static}/v0.4

cp -r web-server/v0.2/css/* %{buildroot}/%{static}/css/v0.2
cp -r web-server/v0.2/js/* %{buildroot}/%{static}/js/v0.2

cp -r web-server/v0.3/css/* %{buildroot}/%{static}/css/v0.3
cp -r web-server/v0.3/js/* %{buildroot}/%{static}/js/v0.3

%pre

%post

%posttrans
cd /%{installdir}

# install node.js modules under /%{installdir}
npm install d3@3.5.17
npm install d3-queue@3.0.1
npm install nvd3@1.8.2
npm install fastdom@1.0.3
npm install function-bind@1.1.0
npm install save-svg-as-png@1.0.1
npm install c3@0.4.11

# by trial and error: lists of files we need and where they end up
# after the npm installs.
jslist="node_modules/d3/d3.min.js
        node_modules/d3-queue/build/d3-queue.min.js
        node_modules/d3-queue/build/d3-queue.js
        node_modules/nvd3/build/nv.d3.min.js
        node_modules/fastdom/fastdom.min.js
        node_modules/function-bind/implementation.js
        node_modules/save-svg-as-png/saveSvgAsPng.js
        node_modules/c3/c3.js"

csslist="node_modules/nvd3/build/nv.d3.min.css"

# v0.2
# copy them to the appropriate directories for v0.2
for x in ${jslist} ;do
    cp $x /%{static}/js/v0.2
done

for x in ${csslist} ;do
    cp $x /%{static}/css/v0.2
done

mv /%{static}/js/v0.2/implementation.js /%{static}/js/v0.2/function-bind.js

# v0.3
# copy them to the appropriate directories for v0.3
for x in ${jslist} ;do
    cp $x /%{static}/js/v0.3
done

for x in ${csslist} ;do
    cp $x /%{static}/css/v0.3
done

mv /%{static}/js/v0.3/implementation.js /%{static}/js/v0.3/function-bind.js

if [ -f /opt/pbench-server/bin/pbench-server-activate-web-server ] ;then
    /opt/pbench-server/bin/pbench-server-activate-web-server
fi

%preun

%postun
# if uninstalling, rather than updating, remove /opt/pbench-web-server
if [ $1 -eq 0 ] ;then
    rm -rf /%{installdir}
fi

%files
%defattr(755,pbench,pbench,755)
/%{static}/css/v0.2/pbench_utils.css
/%{static}/js/v0.2/pbench_utils.js
/%{static}/js/v0.2/app.js
/%{static}/css/v0.3/LICENSE.TXT
/%{static}/css/v0.3/jschart.css
/%{static}/js/v0.3/LICENSE.TXT
/%{static}/js/v0.3/jschart.js
%doc