# -*- coding: utf-8 -*-

# http://docs.fabfile.org/en/1.5/tutorial.html

import os
from fabric.api import *

#project = "fbone"
#version = "0.1"

# the user to use for the remote commands
#env.user = 'wilson'
# the servers where the commands are executed
env.hosts = ['127.0.0.1']


def reset():
    """
    Reset local debug env.
    """

    local("rm -rf /tmp/fbone.sqlite instance/logs instance/uploads instance/openid")
    local("mkdir instance/logs instance/uploads instance/openid")
    local("python manage.py initdb")


def babel():
    """
    Babel compile.
    """

    local("python setup.py compile_catalog --directory `find -name translations` --locale zh -f")


def run():
    local("python manage.py run")


def r():
    run()


def d():
    """
    Debug.
    """

    reset()
    run()


def wsgi(project="myapp"):
    """
    Deploy in local machine as wsgi.
    """

    project_dir = os.getcwd()
    vhost_name = project + ".vhost"
    with cd(project_dir):
        local("sudo chown $USER -R " + project_dir)
        # logs folder
        local("mkdir logs")
        # setup.py and config.py
        local("perl -pi -e 's/\"fbone\"/\"%s\"/g' setup.py fbone/config.py" % project)
        # and *.py
        local("perl -pi -e 's/^from fbone/from %s/g' `grep -Ilr '^from fbone' --include=*.py`" % project)
        # change fbone/ folder
        local("mv fbone %s" % project)

        # Deploy with mod_wsgi
        # configure wsgi
        local("perl -pi -e 's/fbone/%s/g' app.wsgi MANIFEST.in" % project)
        # configure vhost
        local("perl -pi -e 's!DAEMON_NAME!%s!g' app.vhost" % project)
        local("perl -pi -e 's!PATH_TO_WSGI!%s!g' app.vhost" % os.path.join(project_dir, "app.wsgi"))
        local("perl -pi -e 's!PATH_TO_PROJECT!%s!g' app.vhost" % project_dir)
        local("sudo cp app.vhost /etc/apache2/sites-available/%s" % vhost_name)
        local("sudo a2ensite %s" % vhost_name)
        local("sudo service apache2 reload")

        # virtualenv
        local("virtualenv env")
        activate_this = os.path.join(project_dir, "env/bin/activate_this.py")
        execfile(activate_this, dict(__file__=activate_this))

        # Save downloading time, test only.
        #local("cp -r ~/.virtualenvs/fbone/lib/python2.7/site-packages/ /srv/www/myapp/env/lib/python2.7/")

        local("mkdir instance instance/logs instance/uploads instance/openid")
        local("python setup.py install")
        local("python manage.py initdb")

        # make db readable
        local("sudo chmod o+w /tmp/%s.sqlite" % project)

@@ -70,18 +70,8 @@
 ml%0A%0A
-import os%0A
 from
@@ -92,17 +92,16 @@
 port *%0A%0A
-#
 project 
@@ -113,25 +113,8 @@
 one%22
-%0A#version = %220.1%22
 %0A%0A# 
@@ -153,17 +153,16 @@
 ommands%0A
-#
 env.user
@@ -169,14 +169,8 @@
  = '
-wilson
 '%0A# 
@@ -231,17 +231,8 @@
 = %5B'
-127.0.0.1
 '%5D%0A%0A
@@ -745,16 +745,16 @@
 reset()%0A
+
     run(
@@ -759,1769 +759,4 @@
 n()%0A
-%0A%0Adef wsgi(project=%22myapp%22):%0A    %22%22%22%0A    Deploy in local machine as wsgi.%0A    %22%22%22%0A%0A    project_dir = os.getcwd()%0A    vhost_name = project + %22.vhost%22%0A    with cd(project_dir):%0A        local(%22sudo chown $USER -R %22 + project_dir)%0A        # logs folder%0A        local(%22mkdir logs%22)%0A        # setup.py and config.py%0A        local(%22perl -pi -e 's/%5C%22fbone%5C%22/%5C%22%25s%5C%22/g' setup.py fbone/config.py%22 %25 project)%0A        # and *.py%0A        local(%22perl -pi -e 's/%5Efrom fbone/from %25s/g' %60grep -Ilr '%5Efrom fbone' --include=*.py%60%22 %25 project)%0A        # change fbone/ folder%0A        local(%22mv fbone %25s%22 %25 project)%0A%0A        # Deploy with mod_wsgi%0A        # configure wsgi%0A        local(%22perl -pi -e 's/fbone/%25s/g' app.wsgi MANIFEST.in%22 %25 project)%0A        # configure vhost%0A        local(%22perl -pi -e 's!DAEMON_NAME!%25s!g' app.vhost%22 %25 project)%0A        local(%22perl -pi -e 's!PATH_TO_WSGI!%25s!g' app.vhost%22 %25 os.path.join(project_dir, %22app.wsgi%22))%0A        local(%22perl -pi -e 's!PATH_TO_PROJECT!%25s!g' app.vhost%22 %25 project_dir)%0A        local(%22sudo cp app.vhost /etc/apache2/sites-available/%25s%22 %25 vhost_name)%0A        local(%22sudo a2ensite %25s%22 %25 vhost_name)%0A        local(%22sudo service apache2 reload%22)%0A%0A        # virtualenv%0A        local(%22virtualenv env%22)%0A        activate_this = os.path.join(project_dir, %22env/bin/activate_this.py%22)%0A        execfile(activate_this, dict(__file__=activate_this))%0A%0A        # Save downloading time, test only.%0A        #local(%22cp -r ~/.virtualenvs/fbone/lib/python2.7/site-packages/ /srv/www/myapp/env/lib/python2.7/%22)%0A%0A        local(%22mkdir instance instance/logs instance/uploads instance/openid%22)%0A        local(%22python setup.py install%22)%0A        local(%22python manage.py initdb%22)%0A%0A        # make db readable%0A        local(%22sudo chmod o+w /tmp/%25s.sqlite%22 %25 project)%0A

remove rename and deploy project in fabfile, too complicated.