#!/bin/bash
if [[ -z $1  || $1 == "-h" ]]; then
  echo "$0 action odoo_vid"
  echo ""
  echo "odoo_vid example: 10.0 12.0 oca16"
  echo ""
  echo "action is:"
  echo "  start"
  echo "  stop"
  echo "  restart"
  echo "  status"
  echo "  enable"
  echo "  disable"
  exit 1
fi
if [[ $1 =~ (start|stop|restart|status|enable|disable) ]]; then
  action="$1"
  svc="$2"
elif [[ $2 =~ (start|stop|restart|status|enable|disable) ]]; then
  action="$2"
  svc="$1"
else
  echo "Invalid action $1"
  echo "$0 start|stop|restart|status|enable|disable $2"
  exit 1
fi
m=$(echo $svc | grep -Eo "[0-9]+" | head -n1)
[[ ! -f /etc/init.d/$svc && $svc =~ ^oca && -f /etc/init.d/odoo${m}-oca ]] && svc="odoo${m}-oca"
[[ ! -f /etc/init.d/$svc && -f /etc/init.d/odoo$m ]] && svc="odoo$m"
[[ ! -f /etc/init.d/$svc && -f /etc/init.d/odoo${m}-server ]] && svc="odoo${m}-server"
[[ ! -f /etc/init.d/$svc ]] && echo "Service $2 not found!" && exit 126
echo "systemctl $action $svc"
sudo systemctl $action $svc
exit 0
