#!/bin/bash
#
# Copyright (C) 2020 Robert Scheck <robert@fedoraproject.org>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
#

dsts=(/usr/bin/halog /usr/bin/iprange /usr/sbin/haproxy /etc/haproxy/haproxy.cfg /etc/sysconfig/haproxy /usr/share/haproxy /usr/lib/systemd/system/haproxy.service /var/lib/haproxy/stats)
srcs=(halog18 iprange18 haproxy18 ../haproxy18/haproxy.cfg haproxy18 haproxy18 haproxy18.service ../haproxy18/stats)

if [ "$2" = "-y" -o "$2" = "--assumeyes" ]; then
  assumeyes=1
fi

case "$1" in
  enable|set)
    if [ ${assumeyes-0} -eq 0 ]; then
      echo "Important: The creation of convenience symbolic links for the haproxy18"
      echo "RPM package conflicts with the regular haproxy RPM package"
      echo ""
      read -p "Would you like to continue [y/N]: " answer
      echo ""
      if [ "${answer,,}" != "y" ]; then
        echo "Creation of convenience symbolic aborted by user"
        exit 1
      fi
    fi
    offset=0
    for dst in ${dsts[@]}; do
      if [ -e "$dst" ]; then
        if [ -L "$dst" ]; then
          lnk="$(readlink "$dst")"
          if [ "$lnk" != "${srcs[$offset]}" ]; then
            echo "$dst: Different symbolic link already exists"
            conflict=1
          fi
        else
          echo "$dst: File already exists"
          conflict=1
        fi
      fi
      offset=$(($offset + 1))
    done
    if [ ${conflict-0} -eq 1 ]; then
      echo ""
      echo "Warning: Skipped creation of convenience symbolic links due to previous mismatch(es)"
      exit 1
    else
      mkdir -p /etc/haproxy/
      offset=0
      for dst in ${dsts[@]}; do
        if [ ! -L "$dst" ]; then
          ln -s "${srcs[$offset]}" "$dst"
        fi
        offset=$(($offset + 1))
      done
      if [ ${assumeyes-0} -eq 0 ]; then
        echo "Convenience symbolic links for haproxy18 have been created successfully"
      fi
    fi
    ;;
  disable|unset)
    for dst in ${dsts[@]}; do
      if [ -e "$dst" -a ! -L "$dst" ]; then
        echo "$dst: File is not a symbolic link"
        conflict=1
      fi
    done
    if [ ${conflict-0} -eq 1 ]; then
      echo ""
      echo "Warning: Skipped removal of convenience symbolic links due to previous mismatch(es)"
      exit 1
    else
      for dst in ${dsts[@]}; do
        rm -f "$dst"
      done
      if [ -d /etc/haproxy/ ]; then
        rmdir --ignore-fail-on-non-empty /etc/haproxy/
      fi
    fi
    ;;
  *)
    echo "Usage: $0 <enable|disable> [-y|--assumeyes]"
    exit 1
    ;;
esac

exit 0
