#!/bin/bash
#
# vhost-stack
# https://git.stack-source.com/msb/vhost-stack
# MIT License Copyright (c) 2022 Matthew Saunders Brown

# load include file
source $(dirname $0)/vhost.sh

help()
{
  thisfilename=$(basename -- "$0")
  echo "Get VirtualHost info."
  echo ""
  echo "usage: $thisfilename [-d <domain>] [-o <mode>] [-c] [-h]"
  echo ""
  echo "  -h            Print this help."
  echo "  -d <domain>   Domain name to get info for. By defual all domains are returned"
  echo "  -o <mode>     Type of query. 'virtualhosts' (default), 'redirects', 'aliases'."
  echo "  -c            CVS - Output in cvs format, instead of tabbed table."
}

vhost:getoptions "$@"

# create newline var
NL=$'\n'

# check for option (mode or type of query)
if [[ -z $option ]]; then
  option='virtualhosts'
fi

if [[ $option = 'virtualhosts' ]]; then

  if [[ -n $domain ]]; then
    if [[ -d /srv/www/$domain ]]; then
      virtualhostArray=($domain)
    else
      echo "ERROR: $domain not found"
      exit 1
    fi
  else
    vhost::set-virtualhostArray
  fi

  output="Virtualhost Username Config Status"

  for v in "${virtualhostArray[@]}"
  do
    username=$(stat -c '%U' /srv/www/$v)
    if [[ -f /etc/apache2/sites-available/$v.conf ]]; then
      macro="Custom"
      if head -n 1 /etc/apache2/sites-available/$v.conf |grep --quiet ^Use; then
        macro=$(head -n 1 /etc/apache2/sites-available/$v.conf |grep ^Use|cut -d ' ' -f 2)
      fi
    else
      macro="None"
    fi
    if [[ -f /etc/apache2/sites-enabled/$v.conf ]]; then
      status="Enabled"
    else
      status="Disabled"
    fi
    output="$output${NL}$v $username $macro $status"
  done
  if [[ $cvs ]]; then
    echo "$output" | tr " " ","
  else
    echo "$output" | column -t
  fi
elif [[ $option = 'redirects' ]]; then

  # working dir for redirect configs
  cd /etc/apache2/sites-available

  if [[ -n $domain ]]; then
    if [[ -f $domain.conf ]]; then
      if head -n 1 $domain.conf |grep --quiet '^Use Redirect'; then
        vhostConfigs=("$domain.conf")
      else
        echo "ERROR: $domain is not configured as a Redirect."
        exit 1
      fi
    else
      echo "ERROR: $domain is not configured on this server."
      exit 1
    fi
  else
    vhostConfigs=(`ls -1`)
  fi

  output="Virtualhost Config Redirect Status"

  for c in "${vhostConfigs[@]}"
  do
    if head -n 1 /etc/apache2/sites-available/$c |grep --quiet '^Use Redirect'; then
      domain=$(head -n 1 /etc/apache2/sites-available/$c |grep '^Use Redirect'|cut -d ' ' -f 3)
      macro=$(head -n 1 /etc/apache2/sites-available/$c |grep '^Use Redirect'|cut -d ' ' -f 2)
      redirect=$(head -n 1 /etc/apache2/sites-available/$c |grep '^Use Redirect'|cut -d ' ' -f 4)
      if [[ -f /etc/apache2/sites-enabled/$c ]]; then
        status="Enabled"
      else
        status="Disabled"
      fi
      output="$output${NL}$domain $macro $redirect $status"
    fi
  done

  if [[ $cvs ]]; then
    echo "$output" | tr " " ","
  else
    echo "$output" | column -t
  fi

elif [[ $option = 'aliases' ]]; then

  # working dir for aliases configs
  cd /etc/apache2/sites-available

  if [[ -n $domain ]]; then
    if [[ -f $domain.conf ]]; then
      if head -n 1 $domain.conf |grep --quiet '^Use VHostAlias'; then
        vhostConfigs=("$domain.conf")
      else
        echo "ERROR: $domain is not configured as a Alias"
        exit 1
      fi
    else
      echo "ERROR: $domain is not configured on this server."
      exit 1
    fi
  else
    vhostConfigs=(`ls -1`)
  fi

  output="Virtualhost Username Macro Alias Status"

  for c in "${vhostConfigs[@]}"
  do
    if head -n 1 /etc/apache2/sites-available/$c |grep --quiet '^Use VHostAlias'; then
      macro=$(head -n 1 /etc/apache2/sites-available/$c |grep '^Use VHostAlias'|cut -d ' ' -f 2)
      alias=$(head -n 1 /etc/apache2/sites-available/$c |grep '^Use VHostAlias'|cut -d ' ' -f 3)
      username=$(head -n 1 /etc/apache2/sites-available/$c |grep '^Use VHostAlias'|cut -d ' ' -f 4)
      virtualhost=$(head -n 1 /etc/apache2/sites-available/$c |grep '^Use VHostAlias'|cut -d ' ' -f 5)
      if [[ -f /etc/apache2/sites-enabled/$c ]]; then
        status="Enabled"
      else
        status="Disabled"
      fi
      output="$output${NL}$alias $username $macro $virtualhost $status"
    fi
  done

  if [[ $cvs ]]; then
    echo "$output" | tr " " ","
  else
    echo "$output" | column -t
  fi

else
  echo "ERROR: Invalid mode '$option'"
  exit 1
fi