#!/usr/bin/env sh
set -e

case "$1" in
  configure|abort-upgrade|abort-remove|abort-deconfigure)
    tmpl_dir="/usr/share/saikyo-os-release"

    install_one() {
      src="$1"
      dst="$2"
      if [ -r "$src" ]; then
        if [ -e "$dst" ] && ! cmp -s "$src" "$dst"; then
          cp -f "$dst" "${dst}.saikyo-bak" 2>/dev/null || true
        fi
        mkdir -p "$(dirname "$dst")" 2>/dev/null || true
        install -m 0644 "$src" "$dst"
      fi
    }

    install_one "${tmpl_dir}/os-release/os-release" "/etc/os-release"
    install_one "${tmpl_dir}/os-release/os-release" "/usr/lib/os-release"
    install_one "${tmpl_dir}/lsb-release/lsb-release" "/etc/lsb-release"
    install_one "${tmpl_dir}/issue/issue" "/etc/issue"
    ;;
esac

exit 0
