#! /bin/sh

cat << EOF > compyle
#! /usr/bin/env python

import sys
from py_compile import compile

if len(sys.argv) <= 1:
   sys.exit(1)

for file in sys.argv[1:]:
   compile(file)
EOF
chmod a+x compyle

#echo "Byte-compiling .py => .pyc..."
#find . -name \*.py -print | xargs `pwd`/compyle

echo "Compiling Zope..."
CC=gcc CFLAGS="-O2 $CFLAGS" CXX=gcc CXXFLAGS="-O2 $CXXFLAGS" LDFLAGS="-s $LDFLAGS" python w_pcgi.py $*

echo
echo "Create an inituser, please"
python zpasswd.py inituser

echo
echo "Create an emergency user"
python zpasswd.py access

sed 's/Z2\.pid/pcgi.pid/' stop > stop-pcgi

echo "Setting up root config file..."

echo '#! /bin/sh' > root.sh
echo 'cd "`dirname $0`"' >> root.sh
echo '' >> root.sh
echo 'chown -R root.root .' >> root.sh
echo 'chmod -R u=rwX,go=rX .' >> root.sh
echo 'chmod ug=rwx,o=x Extensions import' >> root.sh
echo '' >> root.sh
echo 'chown -R root.www-data var' >> root.sh
echo 'chmod -R ug=rwX,o=rX var' >> root.sh
echo '' >> root.sh
echo 'find . -name \*.so -print | xargs chmod a-w' >> root.sh
echo 'echo "Done!"' >> root.sh

chmod u=rwx,go=rx stop-pcgi root.sh

echo "Done!"
echo "Login as root and run root.sh"
