#!/bin/bash
dist="$1"

if [ "$UID" != "0" ]; then
echo "You have to be root to use this."
exit -1
fi

if [ "x$dist" == "x" ]; then
echo "Usage: $0 [--mandrake || --redhat || --auto]"
exit 0
fi

if [ ! -f /var/lib/rpm-get/ ]; then
mkdir -p /var/lib/rpm-get/
fi

if [ "$dist" == "--auto" ]; then
   CACHE="/var/cache/rpm-get"
   ftp="default"
   if [ -f /etc/mandrake-release ] && [ -L /etc/redhat-release ]; then
      opt="--auto"
      unset dist
      dist="--mandrake"
   fi
   if [ -f /etc/redhat-release ] && [ ! -L /etc/redhat-release ]; then
      opt="--auto"
      unset dist
      dist="--redhat"
   fi
#   if [ ! -f /etc/redhat-release ]; then
#      opt="--auto"
#      unset dist
#      dist="--suse"
#   fi
fi

if [ "$opt" != "--auto" ]; then
   echo -n "Where would you like to cache the rpm files ? [/var/cache/rpm-get]: "
   read CACHE
   if [ "x$CACHE" == "x" ]; then
      CACHE="/var/cache/rpm-get"
   fi
fi

if [ ! -d $CACHE ]; then
mkdir -p $CACHE
chown root.root $CACHE
chmod 0700 $CACHE
else
echo "Warning:" 
echo "there where already an $CACHE directory, make sure only"
echo "root can write to it: "
echo "chown root.root $CACHE"
echo "chmod 0700 $CACHE"
fi

if [ "$opt" != "--auto" ]; then
   echo "Which ftp site/path would you like to use ?"
   echo "If nothing here suits you, or the mirror you chose is out of date"
   echo "you can change the /etc/rpm-get.conf by hand later"
   echo -n '[Press enter to see a list or type "default" to use a Swedish site]: '
   read ftp
fi

if [ "$ftp" == "default" ]; then
   if [ "$dist" == "--mandrake" ]; then
      cat /etc/rpm-get.conf | sed 's/DISTRIBUTION/MANDRAKE/g' > /tmp/rpm-get.tmp
      cat /tmp/rpm-get.tmp | sed 's|ftp-path-to-packages|ftp://ftp.sunet.se/pub/Linux/distributions/mandrake/current/i586/Mandrake|g' > /etc/rpm-get.conf
      cat /etc/rpm-get.conf | sed 's|ftp-path-to-updates|ftp://ftp.sunet.se/pub/Linux/distributions/mandrake/updates/8.0/RPMS|g' > /tmp/rpm-get.tmp
      cat /tmp/rpm-get.tmp | sed 's|ftp-path-to-sources|ftp://ftp.sunet.se/pub/Linux/distributions/mandrake/current/SRPMS|g' > /etc/rpm-get.conf
   fi
   if [ "$dist" == "--redhat" ]; then
      cat /etc/rpm-get.conf | sed 's/DISTRIBUTION/REDHAT/g' > /tmp/rpm-get.tmp
      cat /tmp/rpm-get.tmp | sed 's|ftp-path-to-packages|ftp://ftp.sunet.se/pub/Linux/distributions/redhat/redhat/redhat-7.1-en/os/i386/RedHat|g' > /etc/rpm-get.conf
      cat /etc/rpm-get.conf | sed 's|ftp-path-to-updates|N/A|g' > /tmp/rpm-get.tmp
      cat /tmp/rpm-get.tmp | sed 's|ftp-path-to-sources|ftp://ftp.sunet.se/pub/Linux/distributions/redhat/redhat/redhat-7.1-en/os/i386/SRPMS|g' > /etc/rpm-get.conf
   fi
fi

if [ "x$ftp" == "x" ]; then
   if [ "$dist" == "--mandrake" ]; then
more << EOF
NO ftp://sunsite.uio.no/
US ftp://rufus.w3.org/
US2 (Boston) ftp://rpmfind.net/
UK ftp://ftp.mirror.ac.uk/
FI ftp://ftp.clinet.fi/
EOF
echo
echo -n "[Enter ftp site, ie. US2 for the ftp site in Boston]: "
read country

   if [ "$country" == "NO" ]; then
      ftppath="ftp://sunsite.uio.no/pub/linux/Mandrake/current/i586/Mandrake"
      updatepath="ftp://sunsite.uio.no/pub/linux/Mandrake/updates/8.0/RPMS"
      sourcepath="ftp://sunsite.uio.no/pub/linux/Mandrake/current/SRPMS"
   fi
   if [ "$country" == "US" ]; then
      ftppath="ftp://rufus.w3.org/linux/Mandrake/current/i586/Mandrake"
      updatepath="ftp://rufus.w3.org/linux/Mandrake/updates/8.0/RPMS"
      sourcepath="ftp://rufus.w3.org/linux/Mandrake/current/SRPMS"
   fi
   if [ "$country" == "US2" ]; then
      ftppath="ftp://rpmfind.net/linux/Mandrake/current/Mandrake"
      updatepath="ftp://rpmfind.net/linux/Mandrake/updates/8.0/RPMS"
      sourcepath="ftp://rpmfind.net/linux/Mandrake/current/SRPMS"
   fi
   if [ "$country" == "UK" ]; then
      ftppath="ftp://ftp.mirror.ac.uk/sites/sunsite.uio.no/pub/unix/Linux/Mandrake/current/i586/Mandrake"
      updatepath="ftp://ftp.mirror.ac.uk/sites/sunsite.uio.no/pub/unix/Linux/Mandrake/updates/8.0/RPMS"
      sourcepath="ftp://ftp.mirror.ac.uk/sites/sunsite.uio.no/pub/unix/Linux/Mandrake/current/SRPMS"
   fi
   if [ "$country" == "FI" ]; then
      ftppath="ftp://ftp.clinet.fi/mirrors/ftp.linux-mandrake.com/current/i586/Mandrake"
      updatepath="ftp://ftp.clinet.fi/mirrors/ftp.linux-mandrake.com/updates/8.0/RPMS"
      sourcepath="ftp://ftp.clinet.fi/mirrors/ftp.linux-mandrake.com/current/SRPMS"
   fi
 fi

 if [ "$dist" == "--redhat" ]; then
more << EOF
NO ftp://sunsite.uio.no/
US ftp://rufus.w3.org/
US2 (Boston) ftp://rpmfind.net/
FR (No update dir) ftp://ftp.univ-savoie.fr/
EOF
echo
echo -n "[Enter ftp site, ie. US2 for the ftp site in Boston]: "
read country

  if [ "$country" == "NO" ]; then
    ftppath="ftp://sunsite.uio.no/linux/RedHat/redhat/current/en/os/i386/RedHat"
    updatepath="ftp://sunsite.uio.no/linux/RedHat/updates/current/en/os/i386"
    sourcepath="ftp://sunsite.uio.no/linux/RedHat/redhat/current/en/os/i386/SRPMS"
  fi
  if [ "$country" == "US" ]; then
     ftppath="ftp://rufus.w3.org/linux/redhat/current/en/os/i386/RedHat"
     updatepath="ftp://rufus.w3.org/linux/redhat/updates/current/en/os/i386"
     sourcepath="ftp://rufus.w3.org/linux/redhat/current/en/os/i386/SRPMS"
  fi
  if [ "$country" == "US2" ]; then
     ftppath="ftp://rpmfind.net/linux/redhat/current/en/os/i386/RedHat"
     updatepath="ftp://rpmfind.net/linux/redhat/updates/current/en/os/i386"
     sourcepath="ftp://rpmfind.net/linux/redhat/current/en/os/i386/SRPMS"
  fi
  if [ "$country" == "FR" ]; then
     ftppath="ftp://ftp.univ-savoie.fr/pub/Linux/RedHat/redhat-7.1-en/os/i386/RedHat"
     updatepath="N/A"
     sourcepath="ftp://ftp.univ-savoie.fr/pub/Linux/RedHat/redhat-7.1-en/os/i386/SRPMS"
  fi
 fi
fi

if [ "$dist" == "--mandrake" ]; then
echo -n "Configuring for Mandrake                                      "
sleep 1
cat /etc/rpm-get.conf | sed 's/DISTRIBUTION/MANDRAKE/g' > /tmp/rpm-get.tmp
cat /tmp/rpm-get.tmp | sed 's|ftp-path-to-packages|'$ftppath'|g' > /etc/rpm-get.conf
cat /etc/rpm-get.conf | sed 's|ftp-path-to-updates|'$updatepath'|g' > /tmp/rpm-get.tmp
cat /tmp/rpm-get.tmp | sed 's|CACHE|'$CACHE'|g' > /etc/rpm-get.conf
rm -f /tmp/rpm-get.tmp
echo "$ok"
fi

if [ "$dist" == "--redhat" ]; then
echo -n "Configuring for RedHat                                        "
cat /etc/rpm-get.conf | sed 's/DISTRIBUTION/REDHAT/g' > /tmp/rpm-get.tmp
cat /tmp/rpm-get.tmp | sed 's|ftp-path-to-packages|'$ftppath'|g' > /etc/rpm-get.conf
cat /etc/rpm-get.conf | sed 's|ftp-path-to-updates|'$updatepath'|g' > /tmp/rpm-get.tmp
cat /tmp/rpm-get.tmp | sed 's|CACHE|'$CACHE'|g' > /etc/rpm-get.conf
rm -f /tmp/rpm-get.tmp
sleep 1
echo "$ok"
fi

echo "Make sure to run genrpmlist to or: rpm-get update" 
echo ; echo "                               [ [1;34mCONFIGURED[0m ]"
echo
exit 0