#!/bin/bash -e

# Create new directory for code and executable (not yet clearing old content)
mkdir -p trac-for-bug-report
cd trac-for-bug-report

# Get Trac tar archive
while true; do
    wget -nc http://download.edgewall.org/trac/Trac-0.12.3.tar.gz
    md5=$(md5sum Trac-0.12.3.tar.gz | cut -d\  -f1)
    if [ "${md5}" != 0cef201e223fafb5c3dd99577403f572 ]; then
        rm -f Trac-0.12.3.tar.gz
        continue
    fi
    break
done

# Cleanup old executables
rm -rf bin lib

# Create local install dir
prefix=$(pwd)
pypath=${prefix}/lib/python2.7/site-packages
mkdir -p "${pypath}"

# Unpack Trac
rm -rf Trac-0.12.3
tar zxf Trac-0.12.3.tar.gz
cd Trac-0.12.3
PYTHONPATH=${pypath} python setup.py build install --prefix "${prefix}"
cd ..

# Get AccountManager plugin
#svn co http://trac-hacks.org/svn/accountmanagerplugin/0.11
#cd 0.11
svn co http://trac-hacks.org/svn/accountmanagerplugin/trunk
cd trunk
PYTHONPATH=${pypath} python setup.py build install --prefix "${prefix}"
cd ..

# Minimal trac-admin
cat <<EOF >trac-admin
#!/bin/bash
PYTHONPATH=${pypath} ${prefix}/bin/trac-admin "\$@"
EOF
chmod +x trac-admin

# Minimal tracd
cat <<EOF >tracd
#!/bin/bash
PYTHONPATH=${pypath} ${prefix}/bin/tracd "\$@"
EOF
chmod +x tracd
