#!/bin/sh

# Define some variables
VER=0.3.2
MAKE_NAME=Makefile
EXE_NAME=ddnssync
APP_ENV=conf/app_env.php
PREV_OPT=$HOME/.ddnssync.prev

# Process command args
function parse_args ()
{
   if [[ "$1" == "--help" ]]; then
      echo "If you have run configure before you can use --prev to load your "
      echo "previously used configure options for ddnssync.  It must be the "
      echo "first option passed to configure and using --prev will ignore all "
      echo "other command line arguments.  You must specify all options once "
      echo "in 0.2.0 before they are saved for future reference by the --prev "
      echo "option."
      echo
      echo "If not using --prev then the following options are required: "
      echo "--lib-dir <dir>: Where to install all ddnssync libs; "
      echo "/usr/local/lib/ddnssync is the recommended location "
      echo
      echo "--data-dir <dir>: Where to write runtime info; /var/run/ddnssync "
      echo "is the recommended location"
      echo
      echo "--log-dir <dir>: Where to write the logs; /var/log/ddnssync is the "
      echo "recommended location"
      echo
      echo "--bin-dir <dir>: Where to write the ddnssync exe; /usr/local/bin "
      echo "is the recommended location"
      echo
      echo "--ini-dir <dir>: Where to write the ini file; /etc is the "
      echo "recommended location; the ini file will be named ddnssync.ini"
      echo
      echo "--init-dir <dir>: Where to write the startup init script; this is "
      echo "system dependent and is usually /etc/init.d; the provided init "
      echo "script may or may not work for your Linux distribuiton"
      echo
      echo "--help (first argument only): Print this help and exit" 
      exit 1
   fi

   if [[ "$1" == "--prev" ]]; then
      if [[ -f $PREV_OPT ]]; then
         echo Using previously saved configure options from \'$PREV_OPT\'...
         . $PREV_OPT
      else
         echo "Previous opts file not found; try --help for details!"
      fi
   else
      while [[ $# > 0 ]]; do
         case $1 in
            --lib-dir)
               LIB_DIR=$2
               shift 2
               echo LIB_DIR=$LIB_DIR > $PREV_OPT
               ;;
            --data-dir)
               DATA_DIR=$2
               shift 2
               echo DATA_DIR=$DATA_DIR >> $PREV_OPT
               ;;
            --log-dir)
               LOG_DIR=$2
               shift 2
               echo LOG_DIR=$LOG_DIR >> $PREV_OPT
               ;;
            --bin-dir)
               BIN_DIR=$2
               shift 2
               echo BIN_DIR=$BIN_DIR >> $PREV_OPT
               ;;
            --ini-path)
               INI_PATH=$2/ddnssync.ini
               shift 2
               echo INI_PATH=$INI_PATH >> $PREV_OPT
               ;;
            *)
               echo "WARNING: '$1' unrecognzied option - ignored!"
               shift
               ;;
         esac
      done
   fi

   if [[ -z $LIB_DIR ]]; then
      echo "ERROR: LIB_DIR not set!  Use '--lib-dir <dir>' to set!"
      exit 1
   fi
   if [[ -z $DATA_DIR ]]; then
      echo "ERROR: DATA_DIR not set!  Use '--data-dir <dir>' to set!"
      exit 1
   fi
   if [[ -z $LOG_DIR ]]; then
      echo "ERROR: LOG_DIR not set!  Use '--log-dir <dir>' to set!"
      exit 1
   fi
   if [[ -z $BIN_DIR ]]; then
      echo "ERROR: BIN_DIR not set!  Use '--bin-dir <dir>' to set!"
      exit 1
   fi
   if [[ -z $INI_PATH ]]; then
      echo "ERROR: INI_PATH not set!  Use '--ini-path <dir>' to set!"
      exit 1
   fi
}

function find_php ()
{
   PHP_PATH=`which php`
   if [[ "x$PHP_PATH" == "x" ]]; then
      echo "FAILED!"
      echo "Unable to find a PHP binary in the system path!"
      exit 1
   else
      echo $PHP_PATH
   fi
}

function check_php_ver ()
{
   PHP_VER=`$PHP_PATH -r "echo PHP_VERSION;"`
   if [[ `echo $PHP_VER|grep -c "^5"` == "1" ]]; then
      echo $PHP_VER
   else
      echo "FAILED!"
      echo "ddnssync requires PHP 5.x!"
      exit 1
   fi
}

function check_php_cli ()
{
   PHP_SAPI=`$PHP_PATH -r "echo php_sapi_name();"`
   if [[ "$PHP_SAPI" == "cli" ]]; then
      echo $PHP_SAPI
   else
      echo "FAILED!"
      echo "ddnssync requires the CLI binary to operate, but you have '$PHP_SAPI' installed!"
      exit 1
   fi
}

function check_pear ()
{
   `$PHP_PATH -r "require_once('Log.php');"`
   PEAR=$?
   if [[ $? == 0 ]]; then
      echo "found!"
   else
      echo "FAILED!"
      echo "ddnssync requires the PEAR Log package, but it's not found!"
      exit 1
   fi
}

function find_install
{
   INST_PATH=`which install`
   if [[ "x$INST_PATH" == "x" ]]; then
      echo "FAILED!"
      echo "Unable to find a suitable install program in the path!"
      exit 1
   else
      echo $INST_PATH
   fi
}

function bail_on_write ()
{
   echo "Unable to write '$1'!"
   exit 1
}

function update_ddnssync ()
{
   sed -e "s^@phppath@^$PHP_PATH^" \
       -e "s^@approot@^$LIB_DIR^" $1.in > $1
   if [[ $? == 0 && -f $1 ]]; then
      echo "done."
   else
      bail_on_write $1
   fi
}

function update_appenv ()
{
   sed -e "s^@approot@^$LIB_DIR^" \
       -e "s^@appdata@^$DATA_DIR^" \
       -e "s^@version@^$VER^" \
       -e "s^@applogs@^$LOG_DIR^" $1.in > $1
   if [[ $? == 0 && -f $1 ]]; then
      echo "done."
   else
      bail_on_write $1
   fi
}

function update_makefile ()
{
   sed -e "s^@version@^$VER^" \
       -e "s^@approot@^$LIB_DIR^" \
       -e "s^@appdata@^$DATA_DIR^" \
       -e "s^@applogs@^$LOG_DIR^" \
       -e "s^@bindir@^$BIN_DIR^" \
       -e "s^@install@^$INST_PATH^" \
       -e "s^@inipath@^$INI_PATH^" $1.in > $1 
   if [[ $? == 0 && -f $1 ]]; then
      echo "done."
   else
      bail_on_write $1
   fi
}

##### START MAIN #####
parse_args "$@"

echo -n "Finding PHP binary in system path... "
find_php

# Ensure it's PHP5+
echo -n "Checking PHP version... "
check_php_ver

# Ensure it's the CLI binary
echo -n "Checking for CLI binary... "
check_php_cli

# Ensure we have the PEAR Log package installed
echo -n "Checking for PEAR Log package... "
check_pear

# Find the install binary
echo -n "Checking for suitable install program... "
find_install

# Update the path to PHP in the main script
echo -n "Writing ddnssync... "
update_ddnssync $EXE_NAME 

# Write out the app_env.php file
echo -n "Writing app_env.php... "
update_appenv $APP_ENV 

# Write out the Makefile
echo -n "Writing Makefile... "
update_makefile $MAKE_NAME
