#!/bin/sh
#
# Install lib32nss-mdns to work around Ubuntu bug 1201522
#
# Copyright 2008 Google Inc. All Rights Reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
#   notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
#   copyright notice, this list of conditions and the following disclaimer
#   in the documentation and/or other materials provided with the
#   distribution.
# * Neither the name of Google Inc. nor the names of its
#   contributors may be used to endorse or promote products derived from
#   this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# Include common functions.
export SCRIPTDIR=$(readlink -f $(dirname "$0"))
. "$SCRIPTDIR/common.sh"

usage() {
  SEP="-------------------------------------------------------"
  prompt_info "
$SEP
This program installs lib32nss-mdns on 64-bit Debian-based systems.
$SEP
Usage: $(basename $0) [-n] [-p message] [-c config_file] [-v]
  -c  Config file to store preferences (i.e. 'don't ask again')
  -n  Don't actually install, just show what would happen.
  -p  Prompt to display before installing.
  -v  Be verbose, even when no actions taken or errors encountered.
  -h  Show this help message.
$SEP"
}

is_supressed() {
  PROMPTID=$1
  grep -qs "$PROMPTID 1 $(hostname -f)" "$CFGFILE"
  return $?
}

check_deb() {
  dpkg-query -W -f='${Status}\n' lib32nss-mdns 2> /dev/null | grep -qs " installed"
  if [ $? -eq 0 ] ; then
    exit 3 # already installed
  fi

  apt-cache showpkg lib32nss-mdns 2> /dev/null | grep -qs "^Package: lib32nss-mdns$"
  if [ $? -ne 0 ] ; then
    exit 4 # package not available
  fi
}

install_deb() {
  check_deb

  if [ ! $DRYRUN ]; then
    enable_gui_output
  fi

  if [ "$PROMPTMSG" ]; then
    PROMPTID=$(echo "$PROMPTMSG" | md5sum | cut -d ' ' -f 1)
    is_supressed "$PROMPTID"
    if [ $? -eq 0 ]; then
      exit 0
    fi

    # Don't offer the "don't ask again" option if we can't save the preference.
    touch "$CFGFILE" > /dev/null 2>&1
    if [ $? -eq 0 ]; then
      prompt_yesno_never "$PROMPTMSG"
    else
      prompt_yesno "$PROMPTMSG"
    fi
    RES=$?
    if [ $RES -gt 0 ]; then
      if [ $RES -eq 2 ]; then
        echo "$PROMPTID 1 $(hostname -f)" >> "$CFGFILE"
      fi
      exit 1
    fi
  fi

  if [ ! $DRYRUN ]; then
    run_as_root "apt-get install lib32nss-mdns"
  fi
}

trap cleanup HUP INT QUIT ABRT TERM EXIT
cleanup() {
  set +ex
  close_progress
}

#=========
# MAIN
#=========
while getopts ":nc:p:vh" OPTNAME
do
  case $OPTNAME in
    n )
      enable_dry_run
      ;;
    c )
      CFGFILE="$OPTARG"
      ;;
    p )
      PROMPTMSG="$OPTARG"
      ;;
    v )
      VERBOSE=1
      ;;
    h )
      usage
      exit 0
      ;;
    \: )
      prompt_err "'-$OPTARG' needs an argument."
      usage
      exit 1
      ;;
    * )
      prompt_err "invalid command-line option: $OPTARG"
      usage
      exit 1
      ;;
  esac
done
shift $(($OPTIND - 1))

guess_distro

case $DISTRO in
  Debian )
    install_deb
    ;;
  Ubuntu )
    install_deb
    ;;
  * )
    exit 2 # Not Debian
    ;;
esac

