#!/bin/bash

# ----------------------------------------------------------------------------------
#
# sitelookup replacement/enhancement script for Ensim Pro
#
# Version 1.2.2
#
# (c) Copyright 2004 Doobla (Jonathon Jones). All rights reserved.
#
# This script may only be distributed unmodified.  If you have a modification please
# submit to ensim@doobla.com for the benefit of the community.
#
# This script is intended to be a complete replacement for Ensim's sitelookup
# script along with a few enhancements and improved speed.
#
# NOTE:  THIS SCRIPT IS NOT GPL
#
# ----------------------------------------------------------------------------------

#
# help() To print help
#
help()
{
  echo "usage: sitelookup [ -w <wp_user> ] [ -s <site_handle> ] [ -d <domain> ]"
  echo "                  [ -u <site_admin> ] [ -a ]"
  echo "                  [ domain,wp_user,site_handle,site_root,site_admin,<site_option>:<variable> ]"
  echo ""
  echo "                  -r:reseller displays info for that reseller only"
  echo "                  -w:wp_user is the top level unix user (admin1)"
  echo "                  -s:site_handle is the site equivalent of wp_user (site1)"
  echo "                  -d:domain is the hostname for a site (myco.com)"
  echo "                  -u:site_admin is the username for a site (bob)"
  echo "                  -a:returns info on all sites"
  exit 1
}

#
#if no argument
#
if [ $# -lt 1 ]; then
  help
fi

while getopts r:w:s:d:u:a opt
do
        case "$opt" in
                r) if [ "$with" == "" ] ; then
                        with=" AND ("
                   else
                        with="$with OR "
                   fi
		   i=0
		   for OPTARG in `psql appldb -c "select reseller_id from reseller_info where username = '$OPTARG';" | sed "s/|//g"` ; do
			i=`expr $i + 1`
			if [ $i -eq 3 ] ; then
				break
			fi
		   done
		   if [ "$OPTARG" == "(0" ] ; then
			echo "An invalid reseller name was specified."
			exit 1
		   fi
                   with="$with reseller_id = '$OPTARG'";;
		w) if [ "$with" == "" ] ; then
		   	with=" AND ("
		   else
		   	with="$with OR "
		   fi
		   with="$with'admin' || siteinfo.site_id = '$OPTARG'";;
                s) if [ "$with" == "" ] ; then
                        with=" AND ("
                   else
                        with="$with OR "
                   fi
		   with="$with'site' || siteinfo.site_id = '$OPTARG'";;
                d) if [ "$with" == "" ] ; then
                        with=" AND ("
                   else
                        with="$with OR "
                   fi
                   with="$with domain = '$OPTARG'";;
		u) if [ "$with" == "" ] ; then
                        with=" AND ("
                   else
                        with="$with OR "
                   fi
                   with="$with admin_user = '$OPTARG'";;
		a) with=''
		   break;;
                \?) help;;
        esac
done

head=''
i=0
result=''
if [ "$with" != "" ] ; then
	with="$with)"
fi

wp_user="'admin' || siteinfo.site_id as wp_user"
site_handle="'site' || siteinfo.site_id as site_handle"
domain="domain"
site_admin="admin_user"
site_root="'/home/virtual/' || domain as path"

if [ $# -ge 2 ] ; then
	shift `expr $# - 2`
	checklast="true"
	if [ "`echo $1 | head -c 1`" == "-" ] ; then
		if [ "$1" != "-a" ] ; then 
			checklast="false"
		fi
	fi
	shift
fi

if [ "`echo $1 | grep ","`" != "" ] || [ "$checklast" == "true" ] ; then
	for opt in `echo $1 | sed "s/,/ /g"` ; do
		if [ "$select" != "" ] ; then
			select="$select, "
		fi
		case "$opt" in
			domain) select="$select$domain";;
			wp_user) select="$select$wp_user";;
			site_handle) select="$select$site_handle";;
			site_admin) select="$select$site_admin";;
			site_root) select="$select$site_root";;
			*) select="$select'{#$opt#}' as `echo $opt | sed 's/:/_/g'`";;
		esac
	done
else
	select="domain, 'admin' || siteinfo.site_id as wp_user, 'site' || siteinfo.site_id as site_handle, '/home/virtual/' || domain as path, admin_user"
fi

for domain in `psql -d appldb -c "Select 'site' || siteinfo.site_id as thissitenum, $select from (siteinfo inner join reseller on siteinfo.site_id = reseller.site_id) where siteinfo.site_id <> 0 $with;" | sed "s/|//g"`
do
        if [ "`echo $domain | head -c 1 `" == "(" ] ; then
	        break
        fi

	if [ $i -eq 0 ] ; then
		thissitenum="$domain"
		i=1
	else	
		i=`expr $i + 1`
		if [ "$head" == "" ]
		then
			if [ "`echo $domain | head -c 5 `" == "-----" ]
			then
				head=`expr $i - 1`
				i=0
			fi
			continue
		fi
	
		if [ "`echo $domain | head -c 2 `" == "{#" ] ; then
			if [ "`echo $domain | tail -c 3 `" == "#}" ] ; then
				var=`echo $domain | sed "s/{#//g;s/#}//g"`
				vara=`echo $var | cut -d: -f1`
				varb=`echo $var | cut -d: -f2`
				if [ -f /home/virtual/$thissitenum/info/current/$vara ] ; then
					domain=`fgrep $varb /home/virtual/$thissitenum/info/current/$vara | cut -d= -f2 | sed "s/ //"`
					case $varb in
						aliases) domain="`echo $domain | tr -d "'" | sed "s/\[//;s/\]//;s/,//g" `";;
					esac
				else
					echo "$vara is not a valid site option"
					exit 1
				fi
			fi
		fi

		if [ $i = $head ]
		then
			echo "$result$domain"
			i=0
			result=''
		else
			result="$result$domain,"
		fi
	fi
done
