#!/bin/bash
argu="$1"
pack="$2"

if [ "$UID" != "0" ]; then
   if [ "$argu" = "download" -o "$argu" = "source" ]; then
      echo >/dev/null
     else
      echo "You have to be root to install rpm files."
      echo "Unless if you only download them"
      exit 0
   fi
fi


. /etc/rpm-get.conf

################################## rm cache ###################################

if [ "$UID" == "0" ]; then
   if [ -f $cache/ttc ]; then
      prettc=`cat $cache/ttc`
      ttc=`expr $prettc + 1`
      echo $ttc > $cache/ttc
      if [ "$ttc" -ge "10" ]; then
	 echo -n "Do you want to remove old cached rpm files ? [y/n]: "
         read rmq
         case $rmq in
              y* | Y*)
                rm -i $cache/*.rpm
                rm -i $cache/*.rpm.?
                ;;
              n* | N*)
                echo "Ok, no files removed"
                ;;
         esac
	 > $cache/ttc
      fi
   fi
fi

if [ "$argu" == "clean" ] && [ "$UID" == "0" ]; then
   echo -n "Do you want to remove old cached rpm files ? [y/n]: "
   read rmq
   case $rmq in
   y* | Y*)
     rm -i $cache/*.rpm
     rm -i $cache/*.rpm.?
     ;;
   n* | N*)
     echo "Ok, no files removed"
     ;;  
   esac
   > $cache/ttc
fi

################################# Download ####################################

if [ "$argu" == "download" ] && [ "$UID" != "0" ]; then
   unset cache
   if [ ! -d ~/tmp ]; then
      mkdir ~/tmp
   fi
   cache="~/tmp"
fi

if [ "$UID" == "0" ]; then
   if [ ! -d $cache/ ]; then
      mkdir $cache/
      chown root.root $cache/
      chmod 0700 $cache/
   fi
fi

################################### Usage #####################################

if [ "$argu" == "" ]; then
    echo "Usage: $0 [options] <package> <arguments> "
    echo "Options can be as follows: "
    echo "install"
    echo "configure"
    echo "clean"
    echo "check"
    echo "remove"
    echo "source"
    echo "download"
    echo "update"
    echo "upgrade"
    echo "dist-upgrade"
    echo "build-rpm"
    echo "getlists"
    echo "Arguments can be as follows: "
    echo "--force"
    echo "--noask"
    echo "--nodeps"
    echo "--version"
  exit 0
fi

################################ Configure ####################################

if [ "$argu" == "configure" ]; then
   echo "Enter distribution to configure for"
   echo
   echo "1) Mandrake"
   echo "2) RedHat"
   echo -n "[Enter number 1-2]: "
   read cdist
   if [ "x$cdist" == "x" ]; then
      echo "No distribution enterd"
      exit -1
   fi
   if [ "$cdist" == "1" ]; then
      /sbin/rpm-get-configure --mandrake
   fi
   if [ "$cdist" == "2" ]; then
      /sbin/rpm-get-configure --redhat
   fi
  exit 0
fi

################################ Build-RPM ####################################

function notinstalled () {
echo "There was no source RPM file to build in $cache"
exit -1
}

if [ "$argu" == "build-rpm" ]; then
   rpm -ivh $cache/$pack || notinstalled
   spec=$(rpm -qlp $cache/$pack | grep '.spec')
   cd /usr/src/RPM/SPECS/
   echo "Building RPM, this make take a minute."
   rpm -ba $spec 2>&1 | grep Wrote
   echo "Done."
   cd -
fi

################################### Misc ######################################

if [ "$argu" == "--version" ]; then
    echo "$0-1.5"
  exit 0
fi

if [ "$argu" == "moo" ]; then
   echo "          (__)                  "
   echo "          (oo)                  "
   echo "    /------\/                   "
   echo "   / |    ||                    "
   echo "  *  /\---/\                    "
   echo "     ~~   ~~                    "
   echo " ...."Have you mooed today?"... "
fi


if [ "$?" == "--force" ]; then
   if [ "$argu" = "install" ]; then
      F="--force --nodeps"
   fi
   if [ "$argu" == "upgrade" ]; then
      F="--nodeps"
   fi
fi

if [ "$?" == "--nodeps" ] || [ "$append" == "--nodeps" ]; then
   function rpmdepend {
     echo "Will not try to solve dependencys."
   }
 else
   function rpmdepend {
     rpm-dep.sh $1
   }
fi

if [ "$argu" == "getlists" ]; then
   wget $ftppath/base/rpm-list.db.bz2 --output-document=/var/lib/rpm-get/rpm-list.db.bz2
   wget $ftppath/base/RPMS-list --output-document=/var/lib/rpm-get/RPMS-list
   wget $ftppath/base/SRPMS-list --output-document=/var/lib/rpm-get/SRPMS-list
fi

if [ "$argu" == "check" ]; then
      rpmdepend $ftppath/RPMS/$rpms
fi


############################### dist-upgrade ##################################


# This might install all the devel packages aswell ... bad ...
# But I'm working on it.

if [ "$argu" == "dist-upgrade" ]; then
   echo
   echo "dist-upgrade might install devel packages that you didnt have before"
   echo "This will exclude the kernel and devel packages."
   echo "Some RedHat users have reported bugs in this function"
   echo "So it might not work 100% ...."
   echo
   echo "[Type continue, to continue or hit enter to abort]"
   read pause
   if [ "$pause" == "continue" ]; then
      tmp="$cache/rpm-dist-upgrade"
      rpm -qai | grep Name | cut -d' ' -f10 >> $tmp
      cat $tmp | uniq > $tmp.$$ 
      mv -f $tmp.$$ $tmp
      for files in `cat $tmp | grep -v kernel | grep -v devel`; do
          echo -n "$files*," >> $tmp.tmp
      done
      char1=$(cat $tmp.tmp | wc -c)
      char=$(expr $char1 - 1)
      cat $tmp.tmp | cut -c 0-$char > $tmp
      rm -f $tmp.tmp
      rpm -Uvh --force --nodeps $ftppath/RPMS/{$(cat $tmp)}
      if [ -f /var/lib/rpm-get/RPMS2-list ]; then
         rpm -Uvh --force --nodeps $ftppath/RPMS2/{$(cat $tmp)}
      fi
      cat $tmp
      rm -f $tmp
   fi
fi

################################# Install #####################################

if [ "$argu" == "install" ] || [ "$argu" == "download" ]; then
   rm -f /tmp/RPMS-update /tmp/UPDATE-list
   touch /tmp/RPMS-update
   touch /tmp/UPDATE-list

   for rpms in `grep $pack /var/lib/rpm-get/RPMS-list | awk -F' ' '{print $1}'`; do

       size=`grep -wh $rpms /var/lib/rpm-get/RPMS-list`
       if [ "$?" == "--noask" ]; then
          agreement="1"
         else
	  agreement=
          if [ "$argu" == "download" ]; then
	     echo -n "Do you want to download: $size ? [y/n]: "
	    else
	     echo -n "Do you want to install: $size ? [y/n]: "
	   fi
	   read yesnoanswer

	   case $yesnoanswer in
		y* | Y*)
		   agreement=1;;
		n* | n*)
	           echo "Skipped !"
	   esac
	 fi

	if [ "$agreement" == "1" ]; then
	   cd $cache/
	   if (echo $@ $append | grep "nodeps" >/dev/null); then
	      echo >/dev/null
	     else
	      rpmdepend $ftppath/RPMS/$rpms
	    fi

		wget $ftppath/RPMS/$rpms

	    cd -
       fi
	done


    if [ "$distro" == "MANDRAKE" ]; then
	for rpms in `grep $pack /var/lib/rpm-get/RPMS2-list | awk -F' ' '{print $1}'`; do

	size=`grep -wh $rpms /var/lib/rpm-get/RPMS2-list`
        if [ "$?" == "--noask" ];then
            agreement="1"
          else
	    agreement=
		if [ "$argu" == "download" ]; then
		   echo -n "Do you want to download: $size ? [y/n]: "
		  else
		   echo -n "Do you want to install: $size ? [y/n]: "
		fi
		read yesnoanswer

	case $yesnoanswer in
	    y* | Y*)
		agreement=1;;
	    n* | n*)
	    echo "Skipped !"
	esac
	fi

	if [ "$agreement" == "1" ]; then
	    cd $cache/
	    if echo $@ $append | grep "nodeps" >/dev/null; then
                echo >/dev/null
	       else
		rpmdepend $ftppath/RPMS2/$rpms
	    fi

	    wget $ftppath/RPMS2/$rpms
	    if [ "$argu" != "download" ]; then
       	       rpmdepend $cache/$rpms
	       rpm -ivh $F $cache/$rpms
	    fi
	    cd -
	fi

	    done
	exit 0
    fi
fi

################################### Upgrade ###################################

if [ "$argu" == "upgrade" ]; then

  rm -f /tmp/RPMS-update /tmp/UPDATE-list
  touch /tmp/RPMS-update
  touch /tmp/UPDATE-list

  lynx --source $updatepath | cut -d '>' -f 2 | cut -d '<' -f 1 | grep .rpm | grep -v HREF > /tmp/RPMS-update

function localver {
    ver1=`echo $1 | awk -F'-' '{print $1}'`
    ver2=`which $ver1 2>/dev/null`

    if [ "$ver2" == "" ]; then
    echo "000000"
  exit 1
fi

    echo -n `rpm -qf $ver2 2>/dev/null | tr -d [:alpha:] | sed 's/.[3456]86//g' | sed 's/[-.]//g'`
}
    
    if [ "x$pack" == "x" ]; then
       for files in `cat /tmp/RPMS-update`; do
       remotevar=`echo $files | tr -d [:alpha:] | sed 's/.[3456]86//g' | sed 's/[-.]//g'`
       localvar=`localver $files` 
 
       if [ "$localvar" == "000000" ]; then
	  echo >/dev/null
         else
          function versioncheck {
	   if [ "$remotevar" -gt "$localvar" ]; then
	      echo "$files Seems to need an update"
	      echo "$files" >> /tmp/UPDATE-list
	      else 
	      echo "$files Version are up to date"
	    fi
	   }
	   versioncheck 2>/dev/null
       fi
  done
    else
       for files in `cat /tmp/RPMS-update | grep $pack`; do
       remotevar=`echo $files | tr -d [:alpha:] | sed 's/.[3456]86//g' | sed 's/[-.]//g'`
       localvar=`localver $files`

       if [ "$localvar" == "000000" ]; then
          echo >/dev/null
         else
          function versioncheck {
           if [ "$remotevar" -gt "$localvar" ]; then
              echo "$files Seems to need an update"
              echo "$files" >> /tmp/UPDATE-list
             else
              echo "$files Version are up to date"
            fi
           }
           versioncheck 2>/dev/null
       fi
     done
    fi

    for rpms in `cat /tmp/UPDATE-list`; do

	if [ "$?" == "--noask" ]; then
	    agreement="1"
          else 
	    agreement=
	    echo -n "Do you want to install: $rpms ? [y/n]: "
	    read yesnoanswer
	   case $yesnoanswer in
		y* | Y*)
		    agreement=1;;
		n* | n*)
	    echo "Skipped !"
	   esac
	fi

    if [ "$agreement" == "1" ]; then
	cd $cache/
	if echo $@ $append | grep "nodeps" >/dev/null ; then
	    echo >/dev/null
          else
	    rpmdepend $updatepath/$rpms
	fi

	wget $updatepath/$rpms
	    rpmdepend $cache/$rpms
	    rpm -Uvh $F $cache/$rpms
	cd -
    fi

  done
    exit 0
fi

################################## Update #####################################

if [ "$argu" == "update" ]; then
   genrpmlist
fi

################################# Remove ######################################

if [ "$argu" == "remove" ]; then
   rpm -q $pack && rpm -e $pack ||
   echo "Package ($pack) doesnt seem to be installed."
fi

################################# Source ######################################

if [ "$argu" == "source" ]; then
   for rpms in `grep $pack /var/lib/rpm-get/SRPMS-list | awk -F' ' '{print $1}'`; do
   size=`grep -wh $rpms /var/lib/rpm-get/SRPMS-list`
   if [ "$?" == "--noask" ]; then
      agreement="1"
     else
      agreement=
      echo -n "Do you want to get: $size ? [y/n]: "
      read yesnoanswer

      case $yesnoanswer in
	   y* | Y*)
	      agreement=1;;
	   n* | n*)
	      echo "Skipped !"
      esac
   fi

   if [ "$agreement" == "1" ]; then
      if [ "$argu" == "source" ] && [ "$UID" != "0" ]; then
         unset cache
         if [ ! -d ~/tmp ]; then
            mkdir ~/tmp
         fi
         cache="~/tmp"
         fi

         cd $cache/
         wget $sourcepath/$rpms
   fi

   cd -
 done
fi
