#!/bin/sh
#
# $OpenBSD: phpxs,v 1.2 2002/08/17 01:39:25 avsm Exp $
#
# Copyright (c) 2002 Anil Madhavapeddy.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS
# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OPENBSD
# PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

set -e
PATH=/bin:/usr/bin:/sbin:/usr/sbin

# variables substituted during package build time
php_version=PHP_VERSION
php_module_path=MODULES_DIR
php_cfg=PHP_CONFIG_FILE
php_example=TRUEPREFIX/share/doc/php4
php_module=${php_module_path}/libphp4.so

showusage() {
    echo
    echo "Usage: $0 -a [ -c <location of php.ini> ] <module>"
    echo "       $0 -r [ -c <location of php.ini> ] <module>" 
    echo "       $0 -s"
    exit 2
}

set -- `getopt sarc: $*`
if test $? != 0 ; then
    showusage;
fi

add=0; remove=0;
for i; do
    case "$i"
    in
        -s)
            setup=1; shift;;
        -a)
            add=1; shift;;
        -r)
            remove=1; shift;;
        -c)
            php_cfg=$2; shift; shift;;
        --)
            shift; break;;
    esac
done

module=$1

if [ $setup ]; then
    if [ ! -f ${php_module} ]; then
        echo "Unable to locate PHP4 module at ${php_module}"
        exit;
    fi

    /usr/sbin/apxs -i -a -n php4 ${php_module}
    echo 
    echo "You should copy the sample configuration files from"
    echo "${php_example} to ${php_cfg}"
    exit;
fi

if [ ! -r $php_cfg ]; then
    echo "PHP config file $php_cfg could not be found."
    showusage;
fi

if [ ! $module ]; then
   echo 'Specify a PHP extension module to activate or deactivate.'
   showusage;
fi

if [ `expr $add + $remove` -gt 1 ]; then
    echo 'Cannot specify -a and -r at the same time.'
    showusage
fi

if [ `expr $add + $remove` -eq 0 ]; then
    echo 'Must specify either -a or -r action.'
    showusage
fi

# regexp to match an extension entry in the config file
extmatch='^\;?[[:space:]]{0,2}extension[[:space:]]*=[[:space:]]*'${module}.so

if [ $add -gt 0 ]; then
    if [ ! -r ${php_module_path}/${module}.so ]; then
        echo "Module ${module}.so was not found on your system in ${php_module_path}"
        echo "Try installing package php4-${module}-${php_version} from your package collection"
        exit;
    fi
    if [ `grep -E "${extmatch}" $php_cfg | wc -l` -gt 0 ]; then
        echo "Activating extension : $module";
        /usr/bin/perl -pi -e "s/${extmatch}/extension=${module}.so/" $php_cfg
    else
        echo "Adding extension : $module";
        echo "extension=${module}.so" >> $php_cfg;
    fi
elif [ $remove -gt 0 ]; then 
    echo "Disabling extension: $module"
    /usr/bin/perl -pi -e "s/${extmatch}/\;extension=${module}.so/" $php_cfg;
fi
