#! /bin/bash

# Johannes Meixner <jsmeix@suse.de>, 2004, 2005, 2006, 2007, 2008, 2010, 2014, 2018
#
# Copyright (c) 2010 Novell, Inc.
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, contact Novell, Inc.
#
# To contact Novell about this file by physical or electronic mail,
# you may find current contact information at www.novell.com

#set -x

export PATH="/sbin:/usr/sbin:/usr/bin:/bin"
export LC_ALL="POSIX"
export LANG="POSIX"
umask 022

NET_BACKEND_CONFIG_FILE="/etc/sane.d/net.conf"
SANED_CONFIG_FILE="/etc/sane.d/saned.conf"

MY_NAME=${0##*/}
[ $# -ne 2 ] && { echo -en "\nUsage:\n$MY_NAME 'Comma seperated list of hosts for $NET_BACKEND_CONFIG_FILE' 'Comma seperated list of hosts or subnets for $SANED_CONFIG_FILE'\n" 1>&2 ; exit 1 ; }

NET_BACKEND_CONFIG="$1"
SANED_CONFIG="$2"
ACTIVATE_BACKEND=${0%/*}/activate_scanner_backend
DEACTIVATE_BACKEND=${0%/*}/deactivate_scanner_backend
XINETD_CONFIG_FILE="/etc/xinetd.d/sane-port"
XINETD_SYSTEMD_UNIT="xinetd.service"
XINETD_SYSTEMD_UNIT_FILE="/usr/lib/systemd/system/$XINETD_SYSTEMD_UNIT"
SANED_SYSTEMD_UNIT="saned.socket"
SANED_SYSTEMD_UNIT_FILE="/usr/lib/systemd/system/$SANED_SYSTEMD_UNIT"

# Function to remove old entries and append new entries to a SANE config file:
ReWrite()
{ # Remove all non-comment lines:
  sed -i -e '/^[^#]/d' $1
  # Remove all empty lines:
  sed -i -e '/^[[:space:]]*$/d' $1
  # Append the new config lines if "$2" is not the empty string:
  if [ -n "$2" ]
  then echo "$2" | tr -d '[:space:]' | tr -s ',' '\n' >>$1 || { echo "Failed to set '$2' in $1" 1>&2 ; return 1 ; }
  fi
}

# Write the NET_BACKEND_CONFIG into the NET_BACKEND_CONFIG_FILE:
ReWrite "$NET_BACKEND_CONFIG_FILE" "$NET_BACKEND_CONFIG" || exit 2

# Write the SANED_CONFIG into the SANED_CONFIG_FILE:
ReWrite "$SANED_CONFIG_FILE" "$SANED_CONFIG" || exit 3

# Activate the net backend if "$NET_BACKEND_CONFIG" is not the empty string
# otherwise deactivate the net backend:
if [ -n "$NET_BACKEND_CONFIG" ]
then $ACTIVATE_BACKEND net || { echo "Failed to activate the net backend" 1>&2 ; exit 4 ; }
else $DEACTIVATE_BACKEND net || { echo "Failed to deactivate the net backend" 1>&2 ; exit 5 ; }
fi

# Enable the saned if "$SANED_CONFIG" is not the empty string otherwise disable the saned
# and do the appropriate stuff regarding the xinetd:
if [ -n "$SANED_CONFIG" ]
then # First try the newest way (i.e. reload or start and enable SANED_SYSTEMD_UNIT)
     # otherwise fall back to the older ways (i.e via xinetd):
     if test -e $SANED_SYSTEMD_UNIT_FILE
     then if systemctl --quiet is-active $SANED_SYSTEMD_UNIT && systemctl --quiet is-enabled $SANED_SYSTEMD_UNIT
          then systemctl --quiet reload $SANED_SYSTEMD_UNIT || { echo "Failed to reload $SANED_SYSTEMD_UNIT" 1>&2 ; exit 7 ; }
          else systemctl --quiet start $SANED_SYSTEMD_UNIT || { echo "Failed to start $SANED_SYSTEMD_UNIT" 1>&2 ; exit 8 ; }
               systemctl --quiet enable $SANED_SYSTEMD_UNIT || { echo "Failed to enable $SANED_SYSTEMD_UNIT" 1>&2 ; exit 9 ; }
          fi
          exit 0
     fi
     echo "Cannot reload or start and enable $SANED_SYSTEMD_UNIT (no $SANED_SYSTEMD_UNIT_FILE)." 1>&2
     echo "Falling back to reload or start and enable $XINETD_SYSTEMD_UNIT." 1>&2
     sed -i -e 's/^.*disable.*$/\tdisable     = no/' $XINETD_CONFIG_FILE || { echo "Failed to enable saned in $XINETD_CONFIG_FILE" 1>&2 ; exit 6 ; }
     if test -e $XINETD_SYSTEMD_UNIT_FILE
     then if systemctl --quiet is-active $XINETD_SYSTEMD_UNIT && systemctl --quiet is-enabled $XINETD_SYSTEMD_UNIT
          then systemctl --quiet reload $XINETD_SYSTEMD_UNIT || { echo "Failed to reload $XINETD_SYSTEMD_UNIT" 1>&2 ; exit 7 ; }
          else systemctl --quiet start $XINETD_SYSTEMD_UNIT || { echo "Failed to start $XINETD_SYSTEMD_UNIT" 1>&2 ; exit 8 ; }
               systemctl --quiet enable $XINETD_SYSTEMD_UNIT || { echo "Failed to enable $XINETD_SYSTEMD_UNIT" 1>&2 ; exit 9 ; }
          fi
          exit 0
     fi
     echo "Cannot reload or start and enable $XINETD_SYSTEMD_UNIT (no $XINETD_SYSTEMD_UNIT_FILE)." 1>&2
     exit 11
else # First try the newest way (i.e. stop and disable SANED_SYSTEMD_UNIT)
     # otherwise fall back to the older ways (i.e via xinetd):
     if test -e $SANED_SYSTEMD_UNIT_FILE
     then # Do not error out if stop and/or disable SANED_SYSTEMD_UNIT does not work, perhaps it is already stopped and/or disabled:
          systemctl --quiet stop $SANED_SYSTEMD_UNIT || echo "Could not stop $SANED_SYSTEMD_UNIT" 1>&2
          systemctl --quiet disable $SANED_SYSTEMD_UNIT || echo "Could not disable $SANED_SYSTEMD_UNIT" 1>&2
          exit 0
     fi
     echo "Cannot stop and disable $SANED_SYSTEMD_UNIT (no $SANED_SYSTEMD_UNIT_FILE)." 1>&2
     echo "Falling back to disable saned for xinetd and reload $XINETD_SYSTEMD_UNIT." 1>&2
     sed -i -e 's/^.*disable.*$/\tdisable     = yes/' $XINETD_CONFIG_FILE || { echo "Failed to disable saned in $XINETD_CONFIG_FILE" 1>&2 ; exit 10 ; }
     if test -e $XINETD_SYSTEMD_UNIT_FILE
     then if systemctl --quiet is-active $XINETD_SYSTEMD_UNIT
          then systemctl --quiet reload $XINETD_SYSTEMD_UNIT || { echo "Failed to reload $XINETD_SYSTEMD_UNIT" 1>&2 ; exit 7 ; }
          fi
          exit 0
     fi
     echo "Cannot reload $XINETD_SYSTEMD_UNIT (no $XINETD_SYSTEMD_UNIT_FILE)." 1>&2
     exit 11
fi

exit 0

