#!/bin/bash
# Copyright (c) 1997-2009, NeoNova BV, The Netherlands. All rights Reserved.
#
# This software is the copyrighted work of NeoNova BV.
# Use of the software is subject to the general business terms and conditions of NeoNova BV
# and its related transaction documents like license sheets or other written notices.
#
# Any reproduction or redistribution of the software not in accordance with the license terms
# is expressly prohibited by law, and may result in severe civil and criminally penalties.
# Violators will be prosecuted to the maximum extent possible.

usage()
{
  echo $1
  echo Usage: $0 company-id
  echo
  echo "deleted companies:"
  echo "select id, name from company where status = 'deleted'" | psql -qt -F' ' -U astium
}

if [ -z "$1" ]
then
  usage "missing company id"
  exit 1
fi

STATUS=`echo "select status from company where id=$1"|psql -qt -Uastium`
if [ "$STATUS" != " deleted" ]
then
  NAME=`echo "select name from company where id=$1"|psql -qt -Uastium`
  usage "$1 ($NAME) is not a deleted company"
  exit 1
fi

echo "update company set status = 'active' where id = 5" | psql -qt -Uastium
echo "update department set status = 'active' where id = 5" | psql -qt -Uastium
echo "update phone set status = 'active' where company = 5" | psql -qt -Uastium
echo "update trunk set status = 'active' where company = 5" | psql -qt -Uastium
echo "update inbound set status = 'active' where company = 5" | psql -qt -Uastium
echo "update schedule set status = 'active' where company = 5" | psql -qt -Uastium
echo "update ivr set status = 'active' where company = 5" | psql -qt -Uastium
echo "update soundfile set status = 'active' where company = 5" | psql -qt -Uastium
echo "update moh set status = 'active' where company = 5" | psql -qt -Uastium
echo "update callrouting set status = 'active' where company = 5" | psql -qt -Uastium
echo "select requeue(1)" | psql -qt -Uastium -o /dev/null

NAME=`echo "select name from company where id=$1"|psql -qt -Uastium`
echo "$1 ($NAME) is now RESTORED!"
echo 
echo "(please note this is not perfect, you still need to re-add people to queues for example)"

