#!/bin/bash

echo ""
echo ""

if [ -n "$1" ]; then
echo "File name parameter has value" > /dev/null 2>&1

else
echo "This script can not be ran without a parameter of the full filename you want to run against."
exit
fi


if [ -n "$2" ]; then
echo "repo parameter has value" > /dev/null 2>&1
else
echo "This script can not be ran without a parameter of the repo you want to run against."
exit
fi

##NAME is the package name this is passed from RunList.sh
##Also can be passed by attaching the name as an argument to this script.
NAME=$1

##REPO is the repo to be worked on passed from RunList.sh as rthe second parameter
##echo "This is what was passed $2"

case $2 in

Repo/veclinux-7.1)
    REPO=$2
    BTHROWER="build-thrower/veclinux-7.1/x86"
    ;;
Repo/veclinux-7.2)
    REPO=$2
    BTHROWER="build-thrower/veclinux-7.2/x86"
    ;;
Repo/VL64-7.1)
    REPO=$2
    BTHROWER="build-thrower/veclinux-7.1/x86_64"
    ;;
Repo/VL64-7.2)
    REPO=$2
    BTHROWER="build-thrower/veclinux-7.2/x86_64"
    ;;    
*)         
    echo "Problem with the passed arguments"
    exit
esac



cd ~/$REPO


##Strips the "./" off of $NAME and assigns result to variable $NAMEPKG
NAMEPKG=`echo $NAME | sed 's|./||g'`
echo ""
echo $NAMEPKG
echo ""
###Cleans up some stuff when when script was run previously.
rm ~/$REPO/desktop.txt > /dev/null 2>&1
chmod -R 777 ~/$REPO/usr ~/$REPO/opt ~/$REPO/sbin> /dev/null 2>&1
rm -r -f ~/$REPO/usr ~/$REPO/opt ~/$REPO/sbin ~/$REPO/lib/firmware> /dev/null 2>&1


##Need a variable that carries just the package name without version and such.
justName=`echo $NAMEPKG | rev | cut -d"-" -f4-9 | rev`



##If statements below check for kernel related packages that are not to be moved.
block=${NAMEPKG:0:6}
if [ "$block" = "kernel" ]; then
echo "Don't want to move kernel stuff ... not for repo"
exit
fi

block=${NAMEPKG:0:11}

if [ "$block" = "FDesktopRec" ]; then
echo "Don't want to move FDesktopRec, segfaults."
exit
fi

if [ "$block" = "ndiswrapper" ]; then
echo "Don't want to move ndiswrapper ... not for repo"
exit
fi

if [ "$block" = "amd-catalys" ]; then
echo "Don't want to move amd-catalyst-driver ... not for repo"
exit
fi

if [ "$block" = "broadcom-st" ]; then
echo "Don't want to move broadcom-sta ... not for repo"
exit
fi

block=${NAMEPKG:0:9}
if [ "$block" = "bcm_wimax" ]; then
echo "Don't want to move bcm_wimax ... not for repo"
exit
fi

##Since a new package is going in we need to get the old package removed. 
##this bit of script below should do the job as long as the naming convention has not changed.
for file in `find ~/$REPO/extra/ -name "${justName}*"`;
do

pkgFile=`echo "$file" | rev | cut -d"/" -f1 | rev`
pkgName=`echo $pkgFile | rev | cut -d"-" -f4-9 | rev`
    if [ "$justName" = "$pkgName" ];  then
	mv $file ~/$REPO/old/
	#echo "removing $file"
    fi
done

 



##Below the new package is extracted and the .desktop file is grepped for a category to put it in.
echo "Extracting $NAMEPKG to get at the desktop file."
find ~/Upload/vectorcontrib/$BTHROWER -name "$NAMEPKG" -exec tar xJf {} usr/ sbin/ opt/ lib/  \;  
find ./usr/share/applications/ -name "*desktop" -exec cat {} >> ./desktop.txt  \;
find ./usr/local/share/applications/ -name "*desktop" -exec cat {} >> ./desktop.txt \;
find ./opt/ -name "*desktop" -exec cat {} >> ./desktop.txt \; 

	Xdesktop=`ls -l ./usr/share/xsessions/*desktop | wc -l`
	if [ "$Xdesktop" -gt 0 ] ; then
    echo ""
    echo ""
    echo ""
    echo "Found desktop in Xsessions moving $NAMEPKG to desktop area"
    echo ""
    echo ""
    echo ""
    cp ~/Upload/vectorcontrib/$BTHROWER/$NAMEPKG ~/$REPO/extra/desktop
    exit
    fi
MOVED=""
	if `grep -q Categories.\*AudioVideo ./desktop.txt`; then
    cp ~/Upload/vectorcontrib/$BTHROWER/$NAMEPKG ~/$REPO/extra/xap/AudioVideo
    echo ""
    echo ""
    echo ""
    echo "Moved $NAMEPKG to AudioVideo"
    MOVED="AudioVideo"
    echo ""
    echo ""
    echo ""
    fi
    
if [ -z "$MOVED" ]; then
    if `grep -q Categories.*Development ./desktop.txt`; then
    cp ~/Upload/vectorcontrib/$BTHROWER/$NAMEPKG ~/$REPO/extra/xap/Development
    echo ""
    echo ""
    echo ""
    echo "Moved $NAMEPKG to Development"
    echo ""
    echo ""
    echo ""
    MOVED="Development"
    fi
fi

if [ -z "$MOVED" ]; then  
    if `grep -q Categories.*Office ./desktop.txt`; then
    cp ~/Upload/vectorcontrib/$BTHROWER/$NAMEPKG ~/$REPO/extra/xap/Office
    echo ""
    echo ""
    echo ""
    echo "Moved $NAMEPKG to Office"
    echo ""
    echo ""
    echo ""
    MOVED="Education"
    fi
fi
    
if [ -z "$MOVED" ]; then
    if `grep -q Categories.*Game ./desktop.txt`; then
    cp ~/Upload/vectorcontrib/$BTHROWER/$NAMEPKG ~/$REPO/extra/xap/Game
    echo ""
    echo ""
    echo ""
    echo "Moved $NAMEPKG to Game"
    echo ""
    echo ""
    echo ""
    MOVED="Game"
    fi
fi

if [ -z "$MOVED" ]; then   
    if `grep -q Categories.*Graphics ./desktop.txt`; then
    cp ~/Upload/vectorcontrib/$BTHROWER/$NAMEPKG ~/$REPO/extra/xap/Graphics
    echo ""
    echo ""
    echo ""
    echo "Moved $NAMEPKG to Graphics"
    echo ""
    echo ""
    echo ""
    MOVED="Graphics"
    fi
fi

if [ -z "$MOVED" ]; then    
    if `grep -q Categories.*Network ./desktop.txt`; then
    cp ~/Upload/vectorcontrib/$BTHROWER/$NAMEPKG ~/$REPO/extra/xap/Network
    echo ""
    echo ""
    echo ""
    echo "Moved $NAMEPKG to Network"
    echo ""
    echo ""
    echo ""
    MOVED="Network"
    fi
fi

if [ -z "$MOVED" ]; then   
    if `grep -q Categories.*Education ./desktop.txt`; then
    cp ~/Upload/vectorcontrib/$BTHROWER/$NAMEPKG ~/$REPO/extra/xap/Education
    echo ""
    echo ""
    echo ""
    echo "Moved $NAMEPKG to Education"
    echo ""
    echo ""
    echo ""
    MOVED="Office"
    fi
fi
 
if [ -z "$MOVED" ]; then  
    if `grep -q Categories.*Science ./desktop.txt`; then
    cp ~/Upload/vectorcontrib/$BTHROWER/$NAMEPKG ~/$REPO/extra/xap/Science
    echo ""
    echo ""
    echo ""
    echo "Moved $NAMEPKG to Science"
    echo ""
    echo ""
    echo ""
    MOVED="Science"
    fi
fi

if [ -z "$MOVED" ]; then    
    if `grep -q Categories.*Settings ./desktop.txt`; then
    cp ~/Upload/vectorcontrib/$BTHROWER/$NAMEPKG ~/$REPO/extra/xap/Settings
    echo ""
    echo ""
    echo ""
    echo "Moved $NAMEPKG to Settings"
    echo ""
    echo ""
    echo ""
    MOVED="Settings"
    fi
fi

if [ -z "$MOVED" ]; then    
    if `grep -q Categories.*System ./desktop.txt`; then
    cp ~/Upload/vectorcontrib/$BTHROWER/$NAMEPKG ~/$REPO/extra/xap/System
    echo ""
    echo ""
    echo ""
    echo "Moved $NAMEPKG to System"
    echo ""
    echo ""
    echo ""
    MOVED="System"
    fi
fi

if [ -z "$MOVED" ]; then    
    if `grep -q Categories.*Utility ./desktop.txt`; then
    cp ~/Upload/vectorcontrib/$BTHROWER/$NAMEPKG ~/$REPO/extra/xap/Utility
    echo ""
    echo ""
    echo ""
    echo "Moved $NAMEPKG to Utility"
    echo ""
    echo ""
    echo ""
    MOVED="Utility"
    fi
fi


    if [ -z "$MOVED" ]; then
    echo " "
    else
    rm ./desktop.txt
    exit
    fi
    

Firm=`find ./lib/firmware/ -name "*" 2>/dev/null | wc -l`
    echo $Firm
    sleep 1
    if [ "$Firm" -gt 0 ] ; then
    echo ""
    echo ""
    echo "Found a Firmware file in /usr/man moving $NAMEPKG to Firmware"
    echo ""
    echo ""
    echo ""
    cp ~/Upload/vectorcontrib/$BTHROWER/$NAMEPKG ~/$REPO/extra/Firmware
    exit
   fi

     SBinarys=`ls -l ./sbin/* 2>/dev/null | wc -l`
    if [ "$SBinarys" -gt 0 ] ; then
    echo ""
    echo ""
    echo ""
    echo "Found executable in sbin moving $NAMEPKG to Sbin"
    echo ""
    echo ""
    echo ""
    cp ~/Upload/vectorcontrib/$BTHROWER/$NAMEPKG ~/$REPO/extra/Sbin
   
    exit
    fi    
    
    SBinarys=`ls -l ./usr/sbin/* 2>/dev/null | wc -l`
    if [ "$SBinarys" -gt 0 ] ; then
    echo ""
    echo ""
    echo ""
    echo "Found executable in /usr/sbin moving $NAMEPKG to usrSbin"
    echo ""
    echo ""
    echo ""
    cp ~/Upload/vectorcontrib/$BTHROWER/$NAMEPKG ~/$REPO/extra/usrSbin
   
    exit
    fi    
    
 

   

    SBinarys=`ls -l ./usr/local/sbin/* 2>/dev/null | wc -l`
    if [ "$SBinarys" -gt 0 ] ; then
    echo ""
    echo ""
    echo ""
    echo "Found executable in /usr/local/sbin moving $NAMEPKG to usrSbin area"
    echo ""
    echo ""
    echo ""
    cp ~/Upload/vectorcontrib/$BTHROWER/$NAMEPKG ~/$REPO/extra/usrsbin
    
    exit
    fi
    
 
 
    Ubin=`ls -l ./usr/bin/* 2>/dev/null | wc -l`
    if [ "$Ubin" -gt 0 ] ; then
    echo ""
    echo ""
    echo ""
    echo "Found a man file in /usr/man moving $NAMEPKG to Commandline area"
    echo ""
    echo ""
    echo ""
    cp ~/Upload/vectorcontrib/$BTHROWER/$NAMEPKG ~/$REPO/extra/Commandline
    
    exit
    fi
     

    Ubin=`ls -l ./usr/local/bin/* 2>/dev/null | wc -l`
    if [ "$Ubin" -gt 0 ] ; then
    echo ""
    echo ""
    echo ""
    echo "Found a man file in /usr/man moving $NAMEPKG to Commandline area"
    echo ""
    echo ""
    echo ""
    cp ~/Upload/vectorcontrib/$BTHROWER/$NAMEPKG ~/$REPO/extra/Commandline
    
    exit
    fi
     

    
   
    
	Libs=`ls -l ./usr/lib/* 2>/dev/null | wc -l`
	
    if [ "$Libs" -gt 0 ] ; then
    echo ""
    echo ""
    echo ""
    echo "Found file in /usr/lib moving $NAMEPKG to Library"
     echo ""
    echo ""
    echo ""
    cp ~/Upload/vectorcontrib/$BTHROWER/$NAMEPKG ~/$REPO/extra/Library
    
    exit
    fi
    
    	Libs=`ls -l ./usr/lib64/* 2>/dev/null | wc -l`
	
    if [ "$Libs" -gt 0 ] ; then
    echo ""
    echo ""
    echo ""
    echo "Found files in /usr/lib64  moving $NAMEPKG to Library"
     echo ""
    echo ""
    echo ""
    cp ~/Upload/vectorcontrib/$BTHROWER/$NAMEPKG ~/$REPO/extra/Library
    
    exit
    fi
    
         Libexec=`ls -l ./usr/libexec/* 2>/dev/null | wc -l`
    if [ "$Libexec" -gt 0 ] ; then
    echo ""
    echo ""
    echo ""
    echo "Found executable moving $NAMEPKG to Library"
     echo ""
    echo ""
    echo ""
    cp ~/Upload/vectorcontrib/$BTHROWER/$NAMEPKG ~/$REPO/extra/Library
   
    exit
    fi
    
        Include=`ls -l ./usr/include/* 2>/dev/null | wc -l`
    if [ "$Include" -gt 0 ] ; then
    echo ""
    echo ""
    echo ""
    echo "Found executable moving $NAMEPKG to Library"
     echo ""
    echo ""
    echo ""
    cp ~/Upload/vectorcontrib/$BTHROWER/$NAMEPKG ~/$REPO/extra/Library
   
    exit
    fi
    
    if [ `echo $NAMEPKG | grep meta` ]; then
    cp ~/Upload/vectorcontrib/$BTHROWER/$NAMEPKG ~/$REPO/extra/Meta
    exit
    fi
    
    Fonts=`find ./usr/share/fonts/ -name "*" 2>/dev/null | wc -l`
    echo $Fonts
    sleep 1
    if [ "$Fonts" -gt 0 ] ; then
    echo ""
    echo ""
    echo ""
    echo "Moving $NAMEPKG to Fonts"
     echo ""
    echo ""
    echo ""
    cp ~/Upload/vectorcontrib/$BTHROWER/$NAMEPKG ~/$REPO/extra/Fonts
   
    exit
    fi	  


   
    Ubin=`find ./usr/man/ -name "*gz" 2>/dev/null | wc -l`
    echo $Ubin
    sleep 1
    if [ "$Ubin" -gt 0 ] ; then
    echo ""
    echo ""
    echo "Found a man file in /usr/man moving $NAMEPKG to Commandline area"
    echo ""
    echo ""
    echo ""
    cp ~/Upload/vectorcontrib/$BTHROWER/$NAMEPKG ~/$REPO/extra/Commandline
    exit
   fi
   
   
    Themes=`find ./usr/share/icons/ -name "*" 2>/dev/null | wc -l`
    echo $Themes
    sleep 1
    if [ "$Themes" -gt 0 ] ; then
    echo ""
    echo ""
    echo ""
    echo "Moving $NAMEPKG to Themes"
     echo ""
    echo ""
    echo ""
    cp ~/Upload/vectorcontrib/$BTHROWER/$NAMEPKG ~/$REPO/extra/Themes
    exit
   fi
   
   Themes=`find ./usr/share/themes/ -name "*" 2>/dev/null | wc -l`
    echo $themes
    sleep 1
   if [ "$Themes" -gt 0 ] ; then
    echo ""
    echo ""
    echo ""
    echo "Moving $NAMEPKG to Themes"
     echo ""
    echo ""
    echo ""
    cp ~/Upload/vectorcontrib/$BTHROWER/$NAMEPKG ~/$REPO/extra/Themes
    exit
   fi
   
    
  
   
   
    if [ `echo $NAMEPKG | grep aaa_base` ]; then
    cp ~/Upload/vectorcontrib/$BTHROWER/$NAMEPKG ~/$REPO/extra/Base
    exit
    fi
    
    if [ `echo $NAMEPKG | grep etc-` ]; then
    cp ~/Upload/vectorcontrib/$BTHROWER/$NAMEPKG ~/$REPO/extra/Base
    exit
    fi
    
    if [ `echo $NAMEPKG | grep vlinit-` ]; then
    cp ~/Upload/vectorcontrib/$BTHROWER/$NAMEPKG ~/$REPO/extra/Base
    exit
    fi
    
    if [ `echo $NAMEPKG | grep util-macros-` ]; then
    cp ~/Upload/vectorcontrib/$BTHROWER/$NAMEPKG ~/$REPO/extra/Base
    exit
    fi
    
    
    if [ `echo $NAMEPKG | grep vinstall-` ]; then
    cp ~/Upload/vectorcontrib/$BTHROWER/$NAMEPKG ~/$REPO/extra/Base
    exit
    fi
    
    if [ `echo $NAMEPKG | grep vlart-` ]; then
    cp ~/Upload/vectorcontrib/$BTHROWER/$NAMEPKG ~/$REPO/extra/Base
    exit
    fi
    
     if [ `echo $NAMEPKG | grep qt3-` ]; then
    cp ~/Upload/vectorcontrib/$BTHROWER/$NAMEPKG ~/$REPO/extra/Library
    exit
    fi
    
     if [ `echo $NAMEPKG | grep riak-` ]; then
    cp ~/Upload/vectorcontrib/$BTHROWER/$NAMEPKG ~/$REPO/extra/Commandline
    exit
    fi
    
     if [ `echo $NAMEPKG | grep ffmpegthumbnairler-caja` ]; then
    cp ~/Upload/vectorcontrib/$BTHROWER/$NAMEPKG ~/$REPO/extra/Base
    exit
    fi
    
      Share=`find ./usr/share/ -name "*" 2>/dev/null | wc -l`
    echo $Share
    sleep 1
    if [ "$Share" -gt 0 ] ; then
    echo ""
    echo ""
    echo "Found a man file in /usr/man moving $NAMEPKG to Data"
    echo ""
    echo ""
    echo ""
    cp ~/Upload/vectorcontrib/$BTHROWER/$NAMEPKG ~/$REPO/extra/Data
    exit
   fi

  


     locale=`ls -l ./usr/share/locale/* 2>/dev/null | wc -l`
    if [ "$locale" -gt 0 ] ; then
    echo ""
    echo ""
    echo ""
    echo "Moving $NAMEPKG to Base"
     echo ""
    echo ""
    echo ""
    cp ~/Upload/vectorcontrib/$BTHROWER/$NAMEPKG ~/$REPO/extra/Base
    else
    cp ~/Upload/vectorcontrib/$BTHROWER/$NAMEPKG ~/$REPO/extra/Bad    
    fi
    
    exit
    fi	  


  
   
  
    exit
    
    
    
