JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
clean up my urls
[wfpl.git] / namespacify.sh
1 #!/bin/bash
2
3 # This script is designed to make it easier to use wfpl in other projects, and
4 # not worry about include paths or name collisions.
5 #
6 # This script packs most of wfpl into a single stream (to stdout) and makes it
7 # work inside the \wfpl namespace
8 #
9 # Run like so:  bash ./namespacify > wfpl.php
10 #
11 # or:           bash ./namespacify wordpress > wfpl.php
12 #
13 # The "wordpress" option breaks/obsoletes db_connect and makes db_*() use the
14 # wordpress database and automatically prepends the wordpress table name prefix
15 #
16 # And use in your project like so:
17 #
18 #     require_once('/path/to/wfpl.php');
19 #     $template = new \wfpl\tem();
20 #     $records = \wfpl\db_get_assocs('records', 'name,quantity,date');
21
22 wfpl_rev=""
23 if [ -d .git ]; then
24         wfpl_rev="-$(git rev-parse HEAD)"
25         if ! git diff --quiet; then
26                 wfpl_rev="$wfpl_rev-dirty"
27         fi
28 fi
29
30 echo '<?php'
31 echo
32 echo '#  Copyright (C) 2005,2013 Jason Woofenden <jason@jasonwoof.com>,'
33 echo '#                2008,2009 Joshua Grams <josh@qualdan.com>'
34 echo '#'
35 echo "#  This file is generated from wfpl$wfpl_rev"
36 echo '#'
37 # output license
38 head -n 16 "template.php" | tail -n +5
39 echo
40 echo 'namespace wfpl;'
41 wordpress_regex=''
42 if [ "$1" = 'wordpress' ]; then
43         echo
44         echo '# wordpress tables are supposed to have a prefix.'
45         echo 'function add_wp_table_prefix($table) {'
46         echo '  # only add the wordpress table prefix once, just in case.'
47         echo '  if (substr($table, 0, strlen($GLOBALS['\''wpdb'\'']->prefix)) == $GLOBALS['wpdb']->prefix) {'
48         echo '          return $table;'
49         echo '  } else {'
50         echo '          return $GLOBALS['\''wpdb'\'']->prefix . $table;'
51         echo '  }'
52         echo '}'
53         wordpress_regex='s/function db_\(send_get\|insert_ish\|update\|delete\|\)(.*{/\0\n\t$table = add_wp_table_prefix($table);/;  s/\$GLOBALS\['\''wfpl_db_handle'\''\]/$GLOBALS['\''wpdb'\'']->dbh/g;'
54 fi
55 cat atexit.php csv.php db.php email.php encode.php file.php file_run.php format.php http.php messages.php misc.php template.php time.php | grep -v -e '<?php' -e '?>' -e '^\s*//' -e 'require_once' -e '^\s*$' | grep -v -P -e '^\s*#\s*((?!NAMESPACIFY).)*$' | sed -e "$wordpress_regex"'s/\(register_shutdown_function\|function_exists\|call_user_func_array\)(/\0'\''\\\\wfpl\\\\'\'' . /;  s/^\(\s*\)# NAMESPACIFY \(.*\)/\1\2 = '\''\\\\wfpl\\\\'\'' . \2;/'