check_mp3() {
   if [ $? -eq 0 ]; then
     if [ -x /usr/bin/kdialog ]; then
       kdialog --msgbox "MP3 support now installed, you will need to restart Amarok."
     elif [ -x /usr/bin/zenity ]; then
       zenity --info --text "MP3 support now installed, you will need to restart Amarok."
     else
       Xdialog --msgbox "MP3 support now installed, you will need to restart Amarok." 5 60
     fi
     exit 0
   else
     if [ -x /usr/bin/kdialog ]; then
       kdialog --msgbox "There was an error installing MP3 support. You need to install the package \"libxine-extracodecs\" manually."
     elif [ -x /usr/bin/zenity ]; then
       zenity --info --text "There was an error installing MP3 support. You need to install the package \"libxine-extracodecs\" manually."
     else
       Xdialog --msgbox "There was an error installing MP3 support.\nYou need to install the package \"libxine-extracodecs\" manually." 10 60
     fi
     exit 1
   fi
}

AVAILABLE=`apt-cache show libxine-extracodecs | grep Package`
INSTALLED=`dpkg --get-selections libxine-extracodecs | grep install`
RELEASE=`lsb_release -sc`

#is available, not installed
if [ -n "$AVAILABLE" ] && [ -z "$INSTALLED" ]; then
  if [ -x /usr/bin/kdesu ] && [ -x /usr/bin/adept_batch ]; then
    kdesu -d "bash -c \"kdialog --msgbox 'Fetching package list, please wait...' & apt-get update && killall kdialog; adept_batch install libxine-extracodecs\""
  elif [ -x /usr/bin/gksudo ] && [ -x /usr/sbin/synaptic ]; then
    gksudo "bash -c 'apt-get update && echo libxine-extracodecs i | synaptic --set-selections --non-interactive'"
  else
    xterm -e 'sudo apt-get update && sudo apt-get install libxine-extracodecs'
  fi
  check_mp3
#not available, not installed
elif [ -z "$AVAILABLE" ] && [ -z "$INSTALLED" ]; then
  if [ -x /usr/bin/kdialog ]; then
    kdialog --yesno "Do you want to enable restricted software from multiverse?"
  elif [ -x /usr/bin/zenity ]; then
    zenity --question --text "Do you want to enable restricted software from multiverse?"
  else
    Xdialog --yesno "Do you want to enable restricted software from multiverse?" 7 60
  fi

  if [ $? -eq 0 ]; then
    if [ -x /usr/bin/kdesu ] && [ -x /usr/bin/adept_batch ]; then
      kdesu  "bash -c \"kdialog --msgbox 'Fetching package list, please wait...' & echo deb http://archive.ubuntu.com/ubuntu/ $RELEASE universe multiverse >> /etc/apt/sources.list && apt-get update && killall kdialog; adept_batch install libxine-extracodecs\""
    elif [ -x /usr/bin/gksudo ] && [ -x /usr/sbin/synaptic ]; then
      gksudo "bash -c 'echo deb http://archive.ubuntu.com/ubuntu/ $RELEASE universe multiverse >> /etc/apt/sources.list && apt-get update && echo libxine-extracodecs i | synaptic --set-selections --non-interactive'"
    else
      echo -e '#! /bin/sh\necho "deb http://archive.ubuntu.com/ubuntu/ $RELEASE universe multiverse" >> /etc/apt/sources.list' > /tmp/sources_list.sh
      chmod +x /tmp/sources_list.sh
      xterm -e 'sudo /tmp/sources_list.sh && sudo apt-get update && sudo apt-get install libxine-extracodecs'
      rm -f /tmp/sources_list.sh
    fi
  else
    if [ -x /usr/bin/kdialog ]; then
      kdialog --msgbox "There was an error installing MP3 support."
    elif [ -x /usr/bin/zenity ]; then
      zenity --error --text "There was an error installing MP3 support."
    else
      Xdialog --msgbox "There was an error installing MP3 support." 5 50
    fi
    exit 1
  fi
  check_mp3
fi

exit 0
