#!/bin/bash

#Debian based apt-compare utily
#Author: Arunoda Susiripala
#Licence: http://www.gnu.org/licenses/gpl-3.0.txt
#Copyright: Arunoda Susiripala
#E-mail: me@arunoda.com


PKG_NAME=$1
NEW_PKGS="";
NEW_PKGS_CNT=0;
APT_CACHE='/var/cache/apt/archives'
PKG_OUT=$PKG_NAME #new package location

function check_it {
	# checking whether $1 exists in PKG_LIST
	ans_go=`grep -E ^$1$ PKG_LIST | wc -w`
	if [  $ans_go -eq 0 ]; then
		#check for package available actually used in this OS 
		# (to remove optional packages)
		res=`dpkg -l | tail -n +6 | cut -d " " -f3 | grep ^$1$`
		if [ $res ]; then 
			NEW_PKGS[$NEW_PKGS_CNT]=`echo $1`
			NEW_PKGS_CNT=`expr $NEW_PKGS_CNT + 1`
			
			#getting dependencies for $1 (only one level)
			local TMP_PKGS=`apt-cache depends -i $1 | tail -n +2 | sed -s 's/.*: //'` 
			local CNT=`echo $TMP_PKGS | wc -w`
		
			#recursivly call for dependace packages to check unmet their dependancies
			for lc in  $(seq 1 1 $CNT)
			do
				check_it `echo $TMP_PKGS | cut -d" " -f$lc`
			done
		fi		
	fi

}

function process_pkg {
	filename=`ls $APT_CACHE | grep $1'_'`
	if [ $filename  ]; then
		cp $APT_CACHE/$filename $PKG_OUT 
	else
		echo "   + " $1 | tee $PKG_OUT/NEED_DOWNLOAD
	fi
}

function main_process {
	# creating directory for store packages
	# if there is a old one delete that folder..
	if [ -d $PKG_OUT ]; then
		rm --interactive=never -r $PKG_OUT
	fi
	mkdir $PKG_OUT
	
	
	# start point for getting packages
	check_it $PKG_NAME

	echo
	echo " * Total $NEW_PKGS_CNT package(s) fetched from the apt cache"
	echo " * Following packages to be retrieved manually "
	echo ""
	
	for lc in $(seq 0 1 `expr $NEW_PKGS_CNT - 1`)
	do
		process_pkg ${NEW_PKGS[$lc]}
	done
	
}

function install {
	chmod 777 $1/*.deb
	sudo dpkg -i $1/*.deb
}


if [ ! $1 ]; then
	echo "apt-compare - compare packages between 2 debian based hosts" \
	 " and fetch packages from the cache and display packages you've to find manually" | fmt ;echo
	echo "USAGE:"
	echo " apt-compare -m [create package list(PKG_LIST) from the current OS to be compare with another OS]"
	echo " apt-compare <package-name> [compare and fetch packages]"
	echo "  assume that PKG_LIST(generated by above) is in the working directory"
elif [ $1 == "-m" ]; then
	##creating the package list in the target OS
	dpkg -l | tail -n +6 | cut -d " " -f3> PKG_LIST
elif [ $1 == "-i" ]; then
	if [ -d $2 ]; then
		install $2
	else
		echo "Package not found!";
	fi
else
	##first check whether the software is installed in the system
	res=`dpkg -l | tail -n +6 | grep ii | cut -d " " -f3 | grep ^$PKG_NAME$`
	if [ $res ]; then 
		main_process
	else
		echo "$PKG_NAME is not instaled in the current system";
		echo "do you wanna install it now..? [y|n]"
		read ans
		if [ $ans == 'y' ]; then
			sudo apt-get install $PKG_NAME 
			
			##first check whether the software is installed in the system
			res=`dpkg -l | tail -n +6 | grep ii | cut -d " " -f3 | grep ^$PKG_NAME$`
			if [ $res ]; then
				main_process
			else
				echo "package doesnot exists in the software channel" 
			fi
		else
			echo "aborted!"
		fi
	fi
	
fi




	
	

