#!/bin/bash

# 
# Add Domain (adddom) - This is an end-user tool intended to facilitate the 
#   process of configuring DNS and WWW config files.  This script must be used 
#   on systems setup with the Domain Tool (domtool) and its corresponding 
#   directory structure.
#
# Copyright (c) 2005, Rob Gubler
# All rights reserved.
#
# 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.
#   3. Neither the name of the "Hosting Cooperative" nor the names of its 
#      contributors may be used to endorse or promote products derived from 
#      this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT OWNER 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.
#

g_configfile="~/.adddom"
g_domtool_dir=""
g_my_dns_dir=""
g_my_www_dir=""
g_default_dns_file=""
g_default_www_file=""
g_domain=""

g_func_res=""

assert ()
{
  if [ -z "$2" ]; then
    return 1 
  fi

  lineno=$2

  if [ ! $1 ]  || [ "$1" != "0" ]; then
    echo "Assertion failed:  \"$1\""
    echo "File $0, line $lineno"
    exit 1
  fi  
}

make_path_absolute ()
{
  if [ -z "$1" ]; then
   return 1;
  fi

  if [ "`echo $1 | cut -d/ -f1`" == "~" ]; then
    g_func_res="$HOME`echo $1 | awk -F/ '{ for (i = 2; i <= NF; i++) print "/" $i; }'`"
    g_func_res=`echo $g_func_res | tr -d ' '` # why are there spaces after awk?  
  else
    g_func_res=$1
  fi
}

yes_no_selection ()
{
  temp=0
 
  if [ -z "$1" ]; then
    return 1;
  fi

  while [ $temp == 0 ]
  do
    echo -n "$1 [y/n]: "
    read g_func_res

    if [ "$g_func_res" == "y" ] || [ "$g_func_res" == "n" ]; then
       temp=1
    fi
  done
}

new_dns ()
{
  dirlist="`echo $g_domtool_dir | tr '//' ' '`"
  depth="`echo $dirlist | wc -w`"

  if [ ! -d "$g_domtool_dir" ]; then
    echo "Directory, $g_domtool_dir, does not exist. Creating... "
    assert `mkdir $g_domtool_dir >> /dev/null; echo $?` $LINENO
  fi

  if [ -f "$g_default_dns_file" ]; then
    assert `cp $g_default_dns_file $g_domtool_dir.dns >> /dev/null; echo $?` $LINENO
  else
    echo "No default dns files exists.  Creating empty file..."
    assert `echo " " > $g_domtool_dir.dns >> /dev/null; echo $?` $LINENO
  fi

  if [ "$depth" -gt "4" ]; then
    subdomain=`echo $g_domtool_dir | awk -F/ '{ i = NF - 1; print $i; }'`
    parent=`echo $g_domtool_dir | awk -F/ '{ for (i = 2; i < NF - 1; i++) print "/" $i; }' | tr -d [:space:]`
    parent="`echo $parent/.dns`"
    entry="Alias  $subdomain  www"
    
    yes_no_selection "Append DNS entry \"$entry\" to $parent?"

    if [ "$g_func_res" == "y" ]; then
      
      if [ ! -f "$parent" ]; then
        echo "Parent DNS record, $parent, does not exist. Aborting DNS entry append."
      else
        echo "$entry" >> $parent
      fi
      
    fi
    
  fi
  
}

new_symlink ()
{
  dirlist=`echo $g_my_dns_dir | tr '//' ' '`
  currdir=""

  if [ ! -d "$g_my_dns_dir" ]; then
    echo "Creating local DNS config directory, $g_my_dns_dir..."

    for dirname in $dirlist
    do
      currdir=$currdir/$dirname
  
      if [ ! -d "$currdir" ]; then
        assert `mkdir $currdir >> /dev/null; echo $?` $LINENO
      fi
    done
 
  fi

  echo "Creating symlink $g_my_dns_dir/$g_domain -> $g_domtool_dir.dns..."
  assert `ln -nfs $g_domtool_dir.dns $g_my_dns_dir/$g_domain >> /dev/null; echo $?` $LINENO
}

new_wwwconfig ()
{
  temp="www"
  
  if [ -f "$g_default_www_file" ]; then
    cat $g_default_www_file | sed "s/\[DOMAIN\]/$g_domain/g" > $g_domtool_dir$temp
  else
    echo "No default www file exists.  Creating empty file..."
    assert `echo " " > $g_domtool_dir$temp >> /dev/null; echo $?` $LINENO
  fi
}

new_wwwassoc ()
{
  dirlist=`echo $g_my_www_dir/$g_domain | tr '//' ' '`
  currdir=""

  if [ ! -d "$g_my_www_dir/$g_domain" ]; then
    echo "Creating local WWW directory, $g_my_www_dir/$g_domain..."

    for dirname in $dirlist
    do
      currdir=$currdir/$dirname

      if [ ! -d "$currdir" ]; then
        assert `mkdir $currdir >> /dev/null; echo $?` $LINENO
      fi
    done

  fi


  temp="www"
  echo "Creating symlink $g_my_www_dir/$g_domain/http.conf -> $g_domtool_dir$temp..."
  assert `ln -nfs $g_domtool_dir$temp $g_my_www_dir/$g_domain/http.conf >> /dev/null; echo $?` $LINENO

  echo "Creating symlink $g_my_www_dir/$g_domain/logs -> /var/log/apache2/www.$g_domain..."
  assert `ln -nfs /var/log/apache2/www.$g_domain $g_my_www_dir/$g_domain/logs >> /dev/null; echo $?` $LINENO

  if [ ! -d "$g_my_www_dir/$g_domain/cgi-bin" ]; then
    echo "Creating cgi-bin directory..."
    assert `mkdir $g_my_www_dir/$g_domain/cgi-bin >> /dev/null; echo $?` $LINENO
  fi

  if [ ! -d "$g_my_www_dir/$g_domain/htdocs" ]; then
    echo "Creating htdocs directory..."
    assert `mkdir $g_my_www_dir/$g_domain/htdocs >> /dev/null; echo $?` $LINENO
  fi
  
}


#main ()
{
  if [ -z "$1" ]; then
    echo "No domain specifed!"
    echo 
    echo "Add Domain:"
    echo "  Syntax: $0 [domain name]"
    echo
  
   exit 1;
  fi
  
  g_domain=$1
  
  make_path_absolute $g_configfile; g_configfile=$g_func_res

  assert `cat $g_configfile | grep domtool_dir >> /dev/null; echo $?` $LINENO
  assert `cat $g_configfile | grep my_dns_dir >> /dev/null; echo $?` $LINENO
  assert `cat $g_configfile | grep my_www_dir >> /dev/null; echo $?` $LINENO
  assert `cat $g_configfile | grep default_dns_file >> /dev/null; echo $?` $LINENO
  assert `cat $g_configfile | grep default_www_file >> /dev/null; echo $?` $LINENO

  g_domtool_dir=`cat $g_configfile | grep domtool_dir | cut -d= -f2 | tr -d ' '`
  g_domtool_dir=$g_domtool_dir/`echo $1 | tr '.' '\n' | tac | tr '\n' '//'`
  g_my_dns_dir=`cat $g_configfile | grep my_dns_dir | cut -d= -f2 | tr -d ' '`
  g_my_www_dir=`cat $g_configfile | grep my_www_dir | cut -d= -f2 | tr -d ' '`
  g_default_dns_file=`cat $g_configfile | grep default_dns_file | cut -d= -f2 | tr -d ' '`
  g_default_www_file=`cat $g_configfile | grep default_www_file | cut -d= -f2 | tr -d ' '`

  make_path_absolute $g_domtool_dir; g_domtool_dir=$g_func_res
  make_path_absolute $g_my_dns_dir; g_my_dns_dir=$g_func_res
  make_path_absolute $g_my_www_dir; g_my_www_dir=$g_func_res
  make_path_absolute $g_default_dns_file; g_default_dns_file=$g_func_res
  make_path_absolute $g_default_www_file; g_default_www_file=$g_func_res

  if [ ! -f "$g_domtool_dir.dns" ]; then
    yes_no_selection "No DNS config file found for $g_domain. Create it?"

    if [ "$g_func_res" == "y" ]; then
      new_dns
    fi

  else

    yes_no_selection "Replace DNS config $g_domtool_dir.dns with $g_default_dns_file?"

    if [ "$g_func_res" == "y" ]; then
      new_dns
    fi

  fi
    
  new_symlink
  
  temp="www"
  if [ ! -f "$g_domtool_dir$temp" ]; then
   
    yes_no_selection "No WWW config found for $g_domain. Create it?"
  
    if [ "$g_func_res" == "y" ]; then
      new_wwwconfig 
    fi
  
  else
    
   yes_no_selection "Replace WWW config $g_domtool_dir$temp with $g_default_www_file?"

   if [ "$g_func_res" == "y" ]; then
     new_wwwconfig
   fi
   
  fi
  
  yes_no_selection "Create WWW associations?"
  
  if [ "$g_func_res" == "y" ]; then
    new_wwwassoc 
  fi

  /usr/local/bin/domtool
}

