#!/bin/busybox ash
# (c) Robert Shingledecker 2012
# Contributions by Jason Williams
. /etc/init.d/tc-functions

sudo chown root:staff /tmp > /dev/null 2>&1
sudo chmod 1777 /tmp > /dev/null 2>&1
TCUSER=`cat /etc/sysconfig/tcuser`
TCEDIR=`readlink /etc/sysconfig/tcedir`
DEBINXDIR=""$TCEDIR"/import/debinx"
SCEBOOTLST=""$TCEDIR"/sceboot.lst"
checknotroot
BUILD=`getBuild`
HERE=`pwd`

if [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
	echo "${YELLOW}sce-import - Search, convert, install DEB and pre-built packages as local SCEs.${NORMAL}"
        echo "             Use simple name (nano not nano_*_i386.deb). May use option combos."
        echo "             Also see /etc/sysconfig/sceconfig, locale.nopurge and sce.purge."
	echo "${YELLOW}"sce-import"${NORMAL}             Prompt, enter starting characters of package sought."
	echo "${YELLOW}"sce-import PKG"${NORMAL}         Search packages that start with desired package name."
	echo "${YELLOW}"sce-import -b PKG"${NORMAL}      Add resulting SCE to sceboot.lst."
	echo "${YELLOW}"sce-import -c PKG"${NORMAL}      Search packages that contain desired package name."
	echo "${YELLOW}"sce-import -d PKG"${NORMAL}      Choose existing SCE(s) to provide dependencies for"
        echo "                       new SCE, may make new SCE significantly smaller."
        echo "${YELLOW}"sce-import -k PKG"${NORMAL}      Keep /usr/share/doc and /man in SCE, see man-db."
	echo "${YELLOW}"sce-import -l LISTFILE"${NORMAL} SCE mega-extension created from text file listing one"
        echo "                       PKG per line, eg. sce-import -l /tmp/my_apps contains"
        echo "                       emelfm & nano, which can now share common dependencies."
	echo "${YELLOW}"sce-import -n PKG"${NORMAL}      Non-interactive exact name import, no combos like -nd."
	echo "${YELLOW}"sce-import -o PKG"${NORMAL}      Add imported SCE to ondemand via /tce/ondemand script."
	echo "${YELLOW}"sce-import -p PKG"${NORMAL}      Preserve old DEBINX, no new fetch, better performance."
	echo "${YELLOW}"sce-import -r PKG"${NORMAL}      Use RAM, swap partition/file to unpack source DEBs."
	echo "${YELLOW}"sce-import -s PKG"${NORMAL}      Estimate package, HD and RAM space, warn as needed."
	echo "${YELLOW}"sce-import -u PKG"${NORMAL}      (DEFAULT) update mode, sync new DEBINX files."
	echo "${YELLOW}"sce-import -v PKG"${NORMAL}      View list of packages the imported SCE contains."
	echo "${YELLOW}"sce-import -z PKG"${NORMAL}      Ignore locale.nopurge, sce.purge, sceconfig files."
exit 0
fi

mksce() {
	if [ "$FLAGS" ]
	then
		sudo -E deb2sce "$FLAGS" "$1"
		if [ "$?" != "0" ]; then
			exit 1
		fi
		cleanup
		exit 0
	else
		sudo -E deb2sce "$1"
		if [ "$?" != "0" ]; then
			exit 1
		fi
		cleanup
		exit 0
	fi

}

exit_tcnet() {
	echo " "
	echo "${YELLOW}Warning:${NORMAL} Issue connecting to `cat /opt/tcemirror`, exiting.."
	exit 1
}

read IMPORTMIRROR < /opt/tcemirror
PREBUILTMIRROR="${IMPORTMIRROR%/}/dCore/"$BUILD"/import"
IMPORTMIRROR="${IMPORTMIRROR%/}/dCore/import"


unset FLAGS PACKAGELIST NOPGREP
while getopts drsbolknupcvzSN OPTION
do
	case ${OPTION} in
		c) CHECKALL=TRUE ;;
		u) UPDATEDEBINXMODE=TRUE ;;
		p) PRESERVEDEBINXMODE=TRUE ;;
		n) NONINTERACTIVE=TRUE ;;
		k) KEEPDOC=TRUE ;;
		d) DEPS=TRUE ;;
		r) RAM=TRUE ;;
		s) SIZE=TRUE ;;
		b) FLAGS="$FLAGS"b ; ONBOOT=TRUE ;;
		o) FLAGS="$FLAGS"o ; ONDEMAND=TRUE ;;
		l) PACKAGELIST=TRUE ;;
		v) VIEWPKGS=TRUE ;;
		z) NOCONFIG=TRUE ;;
		N) NOLOCK=TRUE ;;
		*) echo "Run  sce-import --help  for usage information."
                   exit 1 ;;
	esac
done

if [ ! "$NOLOCK" == "TRUE" ]; then
	if [ -f /tmp/.scelock ]; then
		LOCK=`cat /tmp/.scelock`
		if /bb/ps -o pid | sed -e 's/^[ \t]*//'| grep "^$LOCK$" | /bb/grep -v grep > /dev/null 2>&1; then
			echo "${YELLOW}Warning:${NORMAL} Another SCE utility is presently in use, exiting.."
			exit 1
		fi
	fi
	
	echo "$$" > /tmp/.scelock
fi

NICE=`grep -m 1 "^NICE=" /etc/sysconfig/sceconfig | cut -f2 -d=`
if [ -n "$NICE" ]; then
	if [ "$NICE" -gt 19 ]; then
		NICE=19
		echo "* Using nice level 19, 19 is the highest possible nice value."
	elif [ 0 -gt "$NICE" ]; then
		NICE=0
		echo "* Using nice level "$NICE", only root can use negative nice values."
	else 
		echo "* Using nice level "$NICE"."
	fi
	/bb/renice -n "$NICE" -p $$
fi

if grep -i "^ONBOOT=TRUE" /etc/sysconfig/sceconfig > /dev/null 2>&1 && [ "$NOCONFIG" != "TRUE" ]; then
	ONBOOT=TRUE
	FLAGS="$FLAGS"b
fi

if grep -i "^ONDEMAND=TRUE" /etc/sysconfig/sceconfig > /dev/null 2>&1 && [ "$NOCONFIG" != "TRUE" ]; then
	ONDEMAND=TRUE
	FLAGS="$FLAGS"o
fi

shift `expr $OPTIND - 1`

[ "$FLAGS" ] && FLAGS="-$FLAGS"
TARGET="$1"

cleanup () {
[ -d /tmp/work ] && sudo rm -f /tmp/work/* > /dev/null 2>&1
[ -f /tmp/.viewpkgs ] && sudo rm /tmp/.viewpkgs
[ -f /tmp/.importinteractive ] && sudo rm /tmp/.importinteractive
[ -f /tmp/.keepdoc ] && sudo rm /tmp/.keepdoc
[ -f /tmp/.importram ] && sudo rm /tmp/.importram
[ -f /tmp/.importsize ] && sudo rm /tmp/.importsize
[ -f /tmp/.pkgprebuilt ] && sudo rm /tmp/.pkgprebuilt
[ -f /tmp/.depfile ] && sudo rm /tmp/.depfile
[ -f /tmp/.pkgextrafiles ] && sudo rm /tmp/.pkgextrafiles
[ -f /tmp/select.ans ] && sudo rm /tmp/select.ans   
[ -f /tmp/.targetfile ] && sudo rm /tmp/.targetfile
[ -f /tmp/.extrarepodep ] && sudo rm /tmp/.extrarepodep
[ -f /tmp/.importdep ] && sudo rm /tmp/.importdep
[ -f /tmp/.newdep ] && sudo rm /tmp/.newdep
[ -f /tmp/.importfree ] && sudo rm /tmp/.importfree
[ -f /tmp/.scedeps ] && sudo rm /tmp/.scedeps
[ -f /tmp/.scedebs ] && sudo rm /tmp/.scedebs
[ -f /tmp/.scelist ] && sudo rm /tmp/.scelist
#[ -f /tmp/deb2sce.tar.gz ] && sudo rm /tmp/deb2sce.tar.gz
[ -f /tmp/.prebuiltmd5sumlist ] && sudo rm /tmp/.prebuiltmd5sumlist
[ -f /tmp/.pkgextrafilemd5sumlist ] && sudo rm /tmp/.pkgextrafilemd5sumlist
[ -f /tmp/.blocked ] && sudo rm /tmp/.blocked
[ -f /tmp/.pkglisterror ] && sudo rm /tmp/.pkglisterror
[ -f /tmp/.pkgdeperror ] && sudo rm /tmp/.pkgdeperror
[ -f /tmp/.nogetdeps ] && sudo rm /tmp/.nogetdeps
[ -f /tmp/sce.recommends ] && sudo rm /tmp/sce.recommends
[ -f /tmp/sce.suggests ] && sudo rm /tmp/sce.suggests
[ -f /tmp/.recommends ] && sudo rm /tmp/.recommends
[ -f /tmp/.suggests ] && sudo rm /tmp/.suggests
[ -f /tmp/.scenoconfig ] && sudo rm /tmp/.scenoconfig
## Added:
[ -f /tmp/.pkglist ] && sudo rm /tmp/.pkglist
[ -f /tmp/import.log ] && sudo rm /tmp/import.log
[ -f /tmp/.sceimportselect ] && sudo rm /tmp/.sceimportselect
[ -f /tmp/sceimporterror ] && sudo rm /tmp/sceimporterror
}

cleanup

# Strip .sce suffix of package and preceding directory 
# and preceding directory if specified on command line.
TARGET=`basename "$TARGET"`
TARGET=${TARGET%%.sce}
TARGET=${TARGET%%.sce.lst}

# Determine if debinx is desired to be updated in this session.
if [ "$PRESERVEDEBINXMODE" = "TRUE" ] ; then
	echo "* Using the -p option."
elif grep -i "^PRESERVEDEBINXMODE=TRUE" /etc/sysconfig/sceconfig > /dev/null 2>&1 && [ "$NOCONFIG" != "TRUE" ]; then
	PRESERVEDEBINXMODE=TRUE
	echo "* Using the -p option."
else
	echo "* Using the -u option."
fi

# Noconfig mode, overlooking sceconfig, sce.purge, and locale.nopurge
if [ "$NOCONFIG" == "TRUE" ]; then
	touch /tmp/.scenoconfig
	echo "* Using the -z option."
fi

# Determine if non-interactive mode is being used.
if grep -i "^NONINTERACTIVE=TRUE" /etc/sysconfig/sceconfig > /dev/null 2>&1 && [ "$NOCONFIG" != "TRUE" ]; then
	touch /tmp/.importinteractive
	echo "* Using the -n option."
elif [ "$NONINTERACTIVE" == "TRUE" ]; then
	touch /tmp/.importinteractive
	echo "* Using the -n option."
fi

if [ ! -f "$DEBINXDIR"/PKGEXCLUDELISTCUSTOM ]; then	
	if grep -wq "^$TARGET$" "$DEBINXDIR"/PKGEXCLUDELIST > /dev/null 2>&1 && [ ! -z "$TARGET" ]; then
		echo " "
		echo "${YELLOW}Warning:${NORMAL} $TARGET is a blocked package, exiting.."
		echo "$TARGET is a blocked package" >> /tmp/sceimporterror
		exit 1
	fi
else
		if grep -wq "^$TARGET$" "$DEBINXDIR"/PKGEXCLUDELISTCUSTOM > /dev/null 2>&1 && [ ! -z "$TARGET" ]; then
		echo " "
		echo "${YELLOW}Warning:${NORMAL} $TARGET is a blocked package, exiting.."
		echo "$TARGET is a blocked package" >> /tmp/sceimporterror
		exit 1
	fi
fi

# Determine if the size option is being used.
if grep -i "^SIZE=TRUE" /etc/sysconfig/sceconfig > /dev/null 2>&1 && [ "$NOCONFIG" != "TRUE" ]; then
	touch /tmp/.importsize
	echo "* Using the -s option."
elif [ "$SIZE" == "TRUE" ]; then
	touch /tmp/.importsize
	echo "* Using the -s option."
fi

# Determine if the viewpackges option is being used.
if grep -i "^VIEWPKGS=TRUE" /etc/sysconfig/sceconfig > /dev/null 2>&1 && [ "$NOCONFIG" != "TRUE" ]; then
	touch /tmp/.viewpkgs
	echo "* Using the -v option."
elif [ "$VIEWPKGS" == "TRUE" ]; then
	touch /tmp/.viewpkgs
	echo "* Using the -v option."
fi

# Determine if the checkall option is being used.
if grep -i "^CHECKALL=TRUE" /etc/sysconfig/sceconfig > /dev/null 2>&1 && [ "$NOCONFIG" != "TRUE" ]; then
	export CHECKALL=TRUE
	echo "* Using the -c option."
elif [ "$CHECKALL" == "TRUE" ]; then
	echo "* Using the -c option."
fi

# Determine if RAM is going to be used for unpacking.
if grep -i "^RAM=TRUE" /etc/sysconfig/sceconfig > /dev/null 2>&1 && [ "$NOCONFIG" != "TRUE" ]; then
	touch /tmp/.importram
	echo "* Using the -r option."
elif [ "$RAM" == "TRUE" ]; then
	touch /tmp/.importram
	echo "* Using the -r option."
fi

# Determine if docs are to be kept (/usr/share/doc, /usr/share/man, etc).
if grep -i "^KEEPDOC=TRUE" /etc/sysconfig/sceconfig > /dev/null 2>&1 && [ "$NOCONFIG" != "TRUE" ]; then
	touch /tmp/.keepdoc
	echo "* Using the -k option."
elif [ "$KEEPDOC" == "TRUE" ]; then
	touch /tmp/.keepdoc
	echo "* Using the -k option."
fi

# Determine if the -d option or the existence of a dep file
# is present and use dep file if found.
if [ "$DEPS" == "TRUE" ]; then
	touch /tmp/.importdep
	echo "* Using the -d option."
elif ls /etc/sysconfig/tcedir/sce/"$TARGET".sce.dep > /dev/null 2>&1; then
	touch /tmp/.importdep
	echo "* Existing dependency file found."
fi

# Check if TCEDIR is on a USB flash partition, offer the RAM=TRUE option if not already.
if [ ! -f /tmp/.importram ]; then
	PART=`readlink /etc/sysconfig/tcedir | cut -f3 -d"/"`
	if ls /dev/disk/by-id/usb* > /dev/null 2>&1; then
		for I in `ls /dev/disk/by-id/usb*`; do
			if readlink "$I" | grep "/$PART$" > /dev/null 2>&1; then
				USBTCEDIR=TRUE
			fi
		done
		if [ "$USBTCEDIR" == "TRUE" ]; then
			echo " "
			echo " "
			echo " "
			echo "It appears that your TCE directory is located on $PART" 
			echo "which is a USB flash partition and the RAM '-r' option"
			echo "is not being used.  Would you like to use RAM for the" 
			echo "package file operations which can include thousands of"
			echo "writes and may prolong the life of the USB flash device?"
			echo "Specify RAM=TRUE in /etc/sysconfig/sceconfig and make a"
			echo "backup or invoke sce-import with the '-r' option to use"
			echo "RAM for Debian/Ubuntu package unpacking.  Press Enter"
			echo -n "to use ram, or (n/no) to not:  "
			read ans
			if [ "$ans" == "n" ] || [ "$ans" == "no" ]; then
				echo " "
			else
				touch /tmp/.importram
				echo " "
				echo " "
				echo "* Using the -r option."
			fi
		else
			echo " "
		fi
	fi
fi

# Check for ntfs TCEDIR without ntfs-3g loaded, and exit if so.
MS_MNTS=`mount|awk '$5~/ntfs/{printf "%s ",$1}'`
REALDIR=`readlink /etc/sysconfig/tcedir`
MNTDEV=`df "$REALDIR" | tail -n1 | cut -f1 -d" "`
case "$MS_MNTS" in 
			*"$MNTDEV"* )
				
				echo " "
				echo "${YELLOW}Warning:${NORMAL} "$REALDIR" resides on a read only NTFS filesystem,"
				echo "         ntfs-3g needed to write to NTFS, exiting.."
				echo "$TARGET: ntfs-3g needed to make use of the NTFS filesystem for the TCE dir." >> /tmp/sceimporterror
				exit 1					
			;;

esac


# Check for MS Windows filesystem of TCEDIR, and only continue of RAM 
# is being used for unpacking.
MS_MNTS=`mount|awk '$5~/fat|vfat|msdos|ntfs|ntfs-3g|fuseblk/{printf "%s ",$1}'`
REALDIR=`readlink /etc/sysconfig/tcedir`
MNTDEV=`df "$REALDIR" | tail -n1 | cut -f1 -d" "`
case "$MS_MNTS" in 
			*"$MNTDEV"* )
				if [ ! -f /tmp/.importram ]; then 
					echo " "
					echo "${YELLOW}Warning:${NORMAL} "$REALDIR" resides on a Windows filesystem."
					echo "Try again with the -r flag to unpack files in RAM, exiting.."
					echo "$TARGET: TCE directory on a Windows filesystem, use the -r RAM option." >> /tmp/sceimporterror
					exit 1
				fi	
			;;

esac

if [ "$PRESERVEDEBINXMODE" != "TRUE" ]; then
	sudo -E debGetEnv "$2"
	if [ "$?" != "0" ]; then
		echo " "
		echo "${YELLOW}Warning:${NORMAL} Error updating DEBINX files, exiting.."
		echo "$TARGET: Error updating DEBINX files." >> /tmp/sceimporterror
		exit 1
	fi
else
	cd "$DEBINXDIR"
	if [ ! -s /etc/sysconfig/tcedir/import/debinx/debinx ] || [ ! -f deb2sce.tar.gz ]; then
		echo " "
		echo "${YELLOW}Warning:${NORMAL} Run sce-import without the -p option to update DEBINX data, exiting.."
		echo "$TARGET: Need to update the DEBINX data without the -p option." >> /tmp/sceimporterror
		exit 1
	
	else
		DEBINX=`cat /etc/sysconfig/tcedir/import/debinx/debinx`
		if [ ! -f "$DEBINX" ]; then
			echo " "
			echo "${YELLOW}Warning:${NORMAL} Run sce-import without the -p option to update DEBINX data, exiting.."
			echo "$TARGET: Need to update the DEBINX data without the -p option." >> /tmp/sceimporterror
			exit 1
		fi
	fi
	if [ -s /etc/sysconfig/tcedir/import/debinx/debinx_security ]; then
		DEBINX_SECURITY=`cat /etc/sysconfig/tcedir/import/debinx/debinx_security`
		if [ ! -f "$DEBINX_SECURITY" ]; then
			echo " "
			echo "${YELLOW}Warning:${NORMAL} Run sce-import without the -p option to update DEBINX data, exiting.."
			echo "$TARGET: Need to update the DEBINX data without the -p option." >> /tmp/sceimporterror
			exit 1
		fi
	fi
	tar xvf deb2sce.tar.gz PKGEXTRAREPODEP >/dev/null 2>&1
	[ -f PKGEXCLUDELISTCUSTOM ] || tar xvf deb2sce.tar.gz PKGEXCLUDELIST >/dev/null 2>&1
	tar xvf deb2sce.tar.gz PKGADDDEP >/dev/null 2>&1
	tar xvf deb2sce.tar.gz PKGEXTRAFILES >/dev/null 2>&1
	tar xvf deb2sce.tar.gz PKGPREBUILTDEP >/dev/null 2>&1
	tar xvf deb2sce.tar.gz PKGEXTRAFILEMD5SUMLIST >/dev/null 2>&1
	tar xvf deb2sce.tar.gz PKGDATAFILEMD5SUMLIST >/dev/null 2>&1
	tar xvf deb2sce.tar.gz PREBUILTMD5SUMLIST >/dev/null 2>&1
	tar xvf deb2sce.tar.gz KEEPDOC >/dev/null 2>&1
	cd "$HERE"
fi

read DEBINX < /etc/sysconfig/tcedir/import/debinx/debinx
DEBINX="/etc/sysconfig/tcedir/import/debinx/$DEBINX"

checklist() {
  if sudo grep -q "^$1:" "$DEBINXDIR"/PKGADDDEP; then
	:
  elif sudo grep "^$1:" "$DEBINXDIR"/PKGPREBUILTDEP > /dev/null 2>&1; then
	:
  elif sudo grep "^Package: $1$" "$TCEDIR"/import/debinx/debinx.* > /dev/null 2>&1; then
	:
  elif sudo grep -q "^Package: $1$" "$DEBINX" > /dev/null 2>&1; then
	:
  else
	echo "$1" >> /tmp/.pkglisterror
  fi
}


# Determine if .lst file is being used or list specified, copy to /tmp/.targetfile
if [ "$PACKAGELIST" == "TRUE" ]; then
	if [ -f "$1" ] ; then
		TARGETFILE=`readlink -f "$1"`
		if [ -s "$TCEDIR"/sce/"$TARGET".sce.lst ] && [ `pwd` == "/etc/sysconfig/tcedir/sce" ]; then
			TARGETFILE=""$TCEDIR"/sce/"$TARGET".sce.lst"
		fi
		echo " "
		echo "* Checking integrity of "$TARGETFILE".."
		for I in `cat "$TARGETFILE"`; do
			checklist "$I"
		done
		if [ -s /tmp/.pkglisterror ]; then
			echo " "
			echo "$TARGET: Below packages do not exist in the .lst list file:" >> /tmp/sceimporterror
			cat /tmp/.pkglisterror >> /tmp/sceimporterror
			cat /tmp/.pkglisterror
			echo " "
			if [ ! -f /tmp/.importinteractive ]; then
				echo "${YELLOW}Warning:${NORMAL} The above package(s) in "$TARGETFILE" do not exist."
				echo " "
				echo -n "Press Enter to import the remaining list anyway, (q)uit to exit: "
				read ans
				if [ "$ans" == "q" ] || [ "$ans" == "quit" ]; then
					cleanup
					exit 1
				else
					:
				fi
			else
				echo "${YELLOW}Warning:${NORMAL} The above package(s) in "$TARGETFILE" do not exist,"
				echo "         proceeding to import remaining list."
				echo " "
			fi
		fi
		sudo cp "$TARGETFILE" /tmp/.targetfile
		#TARGET=`basename $1 .sce.lst`
	elif [ -s "$TCEDIR"/sce/"$TARGET".sce.lst ]; then
		echo "* Existing list file found."
		TARGETFILE=""$TCEDIR"/sce/"$TARGET".sce.lst"
		echo " "
		echo "* Checking integrity of "$TARGETFILE".."
		for I in `cat "$TARGETFILE"`; do
			checklist "$I"
		done
		if [ -s /tmp/.pkglisterror ]; then
			echo " "
			echo "$TARGET: Below packages do not exist in the .lst list file:" >> /tmp/sceimporterror
			cat /tmp/.pkglisterror >> /tmp/sceimporterror
			cat /tmp/.pkglisterror
			echo " "
			if [ ! -f /tmp/.importinteractive ]; then
				echo "${YELLOW}Warning:${NORMAL} The above package(s) in "$TARGETFILE" do not exist."
				echo " "
				echo -n "Press Enter to import the remaining list anyway, (q)uit to exit: "
				read ans
				if [ "$ans" == "q" ] || [ "$ans" == "quit" ]; then
					cleanup
					exit 1
				else
					:
				fi
			else
				echo "${YELLOW}Warning:${NORMAL} The above package(s) in "$TARGETFILE" do not exist,"
				echo "         proceeding to import remaining list."
				echo " "
			fi
		fi
		sudo cp "$TCEDIR"/sce/"$TARGET".sce.lst /tmp/.targetfile
	elif [ ! "$1" ]; then
		echo " "
		echo "${YELLOW}Warning:${NORMAL} Specify a target file when using the -l option, exiting.."
		echo "$TARGET: Specify a target file when using the -l option." >> /tmp/sceimporterror
		exit 1
	else
		echo " "
		echo "$TARGET: Neither of the below list files exist."  
		echo ""`pwd`/$1""
		echo "$TARGET: "`pwd`/$1"" >> /tmp/sceimporterror
		echo ""$TCEDIR"/sce/"$1".sce.lst"
		echo "$TARGET: "$TCEDIR"/sce/"$1".sce.lst" >> /tmp/sceimporterror
		echo " "
		echo "${YELLOW}Warning:${NORMAL} Neither of the above list files exist, exiting.."
		exit 1
	fi
fi

# When no package is specified on command line, ask for one.
if [ -z "$TARGET" ] && [ "$CHECKALL" == "TRUE" ]; then
	echo " "
	echo -n "Enter characters contained in package sought: "
	read TARGET
elif [ -z "$TARGET" ]; then
	echo " "
	echo -n "Enter starting characters of package sought: "
	read TARGET
fi

if [ ! "$TARGET" ]; then
	echo "No search characters entered, exiting.."
	exit 1
fi

# Below is where the package is found either in prebuilt, main
# Packages file, or in extra repos.  And then proceeds to deb2sce.

# Below is where -l list file option is being used.
if [ "$PACKAGELIST" ] && [ ! -f "$TCEDIR"/sce/"$1".sce.lst ]; then
	if [ -f "$TARGETFILE" ]; then
  		if [ -f /tmp/.importinteractive ]; then
  			echo "* Creating "$TARGET".sce from "$TARGETFILE"."
			echo " "
  			mksce "$TARGET" 
  		else
			echo " "
			echo "${YELLOW}Warning:${NORMAL} Create "$TARGET".sce from "$TARGETFILE"?"
			echo " "
			echo "Press Enter to use this package list, (n)o to import a standard"
			echo -n "'"$TARGET"' package if it exists, (q)uit exits: "
                        read ans
	  		if [ "$ans" == "n" ] || [ "$ans" == "no" ]; then  
				sudo rm /tmp/.targetfile
				echo " "
				mksce "$TARGET"
	  		elif [ "$ans" == "q" ] || [ "$ans" == "quit" ]; then
				cleanup
				exit 0
			else
				echo "* Creating "$TARGET".sce from "$TARGETFILE"."
				echo " "
				mksce "$TARGET"
			fi
		fi
	else
		echo " "
		echo "${YELLOW}Warning:${NORMAL} Specify a target file when using the -l option, exiting.."
		exit 1
	fi  
elif [ "$PACKAGELIST" ] && [ -f "$TCEDIR"/sce/"$1".sce.lst ]; then
	if [ -f "$TARGETFILE" ]; then
  		if [ -f /tmp/.importinteractive ]; then
  			echo "* Creating "$TARGET".sce from "$TARGETFILE"."
			echo " "
  			mksce "$TARGET" 
  		else
			echo " "
			echo "${YELLOW}Warning:${NORMAL} Create "$TARGET".sce from "$TARGETFILE"?"
			echo " "
			echo "Press Enter to use this package list, (n)o to import a standard"
			echo -n "'"$TARGET"' package if it exists, (q)uit exits: "
                        read ans
	  		if [ "$ans" == "n" ] || [ "$ans" == "no" ]; then  
				sudo rm /tmp/.targetfile
				echo " "
				mksce "$TARGET"
	  		elif [ "$ans" == "q" ] || [ "$ans" == "quit" ]; then
				cleanup
				exit 0
			else
				echo "* Creating "$TARGET".sce from "$TARGETFILE"."
				echo " "
				mksce "$TARGET"
			fi
		fi
	elif [ ! -f "$TARGETFILE" ] && [ -f "$TCEDIR"/sce/"$TARGET".sce.lst ]; then
  		if [ -f /tmp/.importinteractive ]; then
  			echo "* Creating "$TARGET".sce from "$TARGETFILE"."
			echo " "
  			mksce "$TARGET" 
  		else
			echo " "
			echo "${YELLOW}Warning:${NORMAL} Create "$TARGET".sce from "$TARGETFILE"?"
			echo " "
			echo "Press Enter to use this package list, (n)o to import a standard"
			echo -n "'"$TARGET"' package if it exists, (q)uit exits: "
                        read ans
	  		if [ "$ans" == "n" ] || [ "$ans" == "no" ]; then  
				sudo rm /tmp/.targetfile
				echo " "
				mksce "$TARGET"
	  		elif [ "$ans" == "q" ] || [ "$ans" == "quit" ]; then
				cleanup
				exit 0
			else
				echo "* Creating "$TARGET".sce from "$TARGETFILE"."
				echo " "
				mksce "$TARGET"
			fi
		fi
		
	else
		echo " "
		echo "${YELLOW}Warning:${NORMAL} Specify a target file when using the -l option, exiting.."
		exit 1
	fi  
# Below is where an existing .lst file is found in sce dir.
elif [ -f "$TCEDIR"/sce/"$TARGET".sce.lst ]; then
  	if [ -f /tmp/.importinteractive ]; then
		TARGETFILE=""$TCEDIR"/sce/"$TARGET".sce.lst"
		echo " "
		echo "* Checking integrity of "$TARGETFILE".."
		for I in `cat "$TARGETFILE"`; do
			checklist "$I"
		done
		sudo cp "$TCEDIR"/sce/"$TARGET".sce.lst /tmp/.targetfile
		if [ -s /tmp/.pkglisterror ]; then
			echo " "
			echo "$TARGET: Below packages do not exist in the .lst list file:" >> /tmp/sceimporterror
			cat /tmp/.pkglisterror >> /tmp/sceimporterror
			cat /tmp/.pkglisterror
			echo " "
			if [ ! -f /tmp/.importinteractive ]; then
				echo "${YELLOW}Warning:${NORMAL} The above package(s) in "$TARGETFILE" do not exist."
				echo " "
				echo -n "Press Enter to import the remaining list anyway, (q)uit to exit: "
				read ans
				if [ "$ans" == "q" ] || [ "$ans" == "quit" ]; then
					cleanup
					exit 1
				else
					:
				fi
			else
				echo "${YELLOW}Warning:${NORMAL} The above package(s) in "$TARGETFILE" do not exist,"
				echo "         proceeding to import remaining list."
				echo " "
			fi
		fi
  		echo "* Creating "$TARGET".sce from "$TARGETFILE"."
		echo " "
  		mksce "$TARGET"
  	else
		TARGETFILE=""$TCEDIR"/sce/"$TARGET".sce.lst"
		echo " "
		echo "${YELLOW}Warning:${NORMAL} Create "$TARGET".sce from "$TARGETFILE"?"
		echo " "
		echo "Press Enter to use this package list, (n)o to import a standard"
		echo -n "'"$TARGET"' package if it exists, (q)uit exits: "
                read ans
	  	if [ "$ans" == "n" ] || [ "$ans" == "no" ]; then  
			sudo rm /tmp/.targetfile
			echo " "
			mksce "$TARGET"
  		elif [ "$ans" == "q" ] || [ "$ans" == "quit" ]; then
			cleanup
			exit 0
		else
			echo " "
			echo "* Checking integrity of "$TARGETFILE".."
			for I in `cat "$TARGETFILE"`; do
				checklist "$I"
			done
			sudo cp "$TCEDIR"/sce/"$TARGET".sce.lst /tmp/.targetfile
			if [ -s /tmp/.pkglisterror ]; then
				echo " "
				echo "$TARGET: Below packages do not exist in the .lst list file:" >> /tmp/sceimporterror
				cat /tmp/.pkglisterror >> /tmp/sceimporterror
				cat /tmp/.pkglisterror
				echo " "
				if [ ! -f /tmp/.importinteractive ]; then
					echo "${YELLOW}Warning:${NORMAL} The above package(s) in "$TARGETFILE" do not exist."
					echo " "
					echo -n "Press Enter to import the remaining list anyway, (q)uit to exit: "
					read ans
					if [ "$ans" == "q" ] || [ "$ans" == "quit" ]; then
						cleanup
						exit 1
					else
						:
					fi
				else
					echo "${YELLOW}Warning:${NORMAL} The above package(s) in "$TARGETFILE" do not exist,"
					echo "         proceeding to import remaining list."
					echo " "
				fi
			fi
			echo "* Creating "$TARGET".sce from "$TARGETFILE"."
			echo " "
			mksce "$TARGET"
		fi
	fi  
# Below checks for package in the extra repos first, then the 
# Debian main repos, then in the prebuilt section, in that order.
elif [ "$CHECKALL" == "TRUE" ]; then
	if sudo grep -i "^Package: .*$TARGET" "$TCEDIR"/import/debinx/debinx* > /dev/null 2>&1 ||  sudo grep -i "^Package: .*$TARGET" "$DEBINX" > /dev/null 2>&1 || sudo grep -i "$TARGET" "$DEBINXDIR"/PKGADDDEP | cut -f1 -d: | grep -i "$TARGET" > /dev/null 2>&1 || sudo grep -i "$TARGET" "$DEBINXDIR"/PKGPREBUILTDEP | cut -f1 -d: | grep -i "$TARGET" > /dev/null 2>&1; then
		if [ -f /tmp/.importinteractive ]; then
			if sudo grep -i "^Package: .*$TARGET$" "$TCEDIR"/import/debinx/debinx* > /dev/null 2>&1; then
				DEB=`sudo grep -m 1 -i "^Package: $TARGET$" "$TCEDIR"/import/debinx/debinx* | head -1 | awk '{print $2}'`
			elif sudo grep -i "^Package: .*$TARGET$" "$DEBINX" > /dev/null 2>&1; then
				DEB=`sudo grep -m 1 -i "^Package: $TARGET$" "$DEBINX" | awk '{print $2}'`
			elif sudo grep -i "^$TARGET:" "$DEBINXDIR"/PKGADDDEP > /dev/null 2>&1; then
				DEB=`sudo grep -m 1 -i "^$TARGET:" "$DEBINXDIR"/PKGADDDEP | cut -f1 -d:`
			elif sudo grep -i "^$TARGET:" "$DEBINXDIR"/PKGPREBUILTDEP  > /dev/null 2>&1; then
				DEB=`sudo grep -m 1 -i "^$TARGET:" "$DEBINXDIR"/PKGPREBUILTDEP | cut -f1 -d:`
			fi
			if [ -z "$DEB" ]; then
				echo " "
				echo "${YELLOW}Warning:${NORMAL} '"$TARGET"' is not available as a package, exiting.."
				echo "$TARGET is not available as a package." >> /tmp/sceimporterror
				exit 1
			fi     
		else
			{ ls "$TCEDIR"/import/debinx/debinx* > /dev/null 2>&1 && sudo grep -i "^Package: .*$TARGET" "$TCEDIR"/import/debinx/debinx* | awk '{print $2}' ; sudo grep -i "^Package: .*$TARGET" "$DEBINX" | awk '{print $2}' ; sudo grep -i "$TARGET" "$DEBINXDIR"/PKGADDDEP | cut -f1 -d: | grep "$TARGET" ; sudo grep -i "$TARGET" "$DEBINXDIR"/PKGPREBUILTDEP | cut -f1 -d: | grep -i "$TARGET" ; } | sort | uniq  > /tmp/.sceimportselect 
			LINES=`/bb/wc -l /tmp/.sceimportselect | cut -f1 -d" "`
			if [ "$LINES" == "1" ]; then
				DEB=`cat /tmp/.sceimportselect`
				echo " "
				echo -n "Press Enter to import ${YELLOW}$DEB${NORMAL}, (q)uit to exit: "
				read ans
				if [ "$ans" == "q" ] || [ "$ans" == "quit" ]; then
					cleanup
					exit 0
				else
					:
				fi
			else
				cat /tmp/.sceimportselect | select "Select package for '"$TARGET"'." "-"
				read DEB < /tmp/select.ans                                                                           
  				[ "$DEB" == "q" ] || [ "$DEB" == "quit" ] || [ "$DEB" == "" ] && exit 1  
			fi   
		fi 
		if [ ! -f "$DEBINXDIR"/PKGEXCLUDELISTCUSTOM ]; then	
			if grep -wq "^$DEB$" "$DEBINXDIR"/PKGEXCLUDELIST > /dev/null 2>&1; then
				echo " "
				echo "${YELLOW}Warning:${NORMAL} $DEB is a blocked package, exiting.."
				echo "Warning: $DEB is a blocked package." >> /tmp/sceimporterror
				exit 1
			fi
		else
			if grep -wq "^$DEB$" "$DEBINXDIR"/PKGEXCLUDELISTCUSTOM > /dev/null 2>&1; then
			echo " "
			echo "${YELLOW}Warning:${NORMAL} $DEB is a blocked package, exiting.."
			echo "Warning: $DEB is a blocked package." >> /tmp/sceimporterror
			exit 1
			fi
		fi
  		echo "* Importing ${YELLOW}$DEB${NORMAL}."
		echo " "
  		mksce "$DEB"
	fi 
elif [ "$CHECKALL" != "TRUE" ]; then  
	if sudo grep -i "^Package: $TARGET" "$TCEDIR"/import/debinx/debinx* > /dev/null 2>&1 ||  sudo grep -i "^Package: $TARGET" "$DEBINX" > /dev/null 2>&1 || sudo grep -i "$TARGET" "$DEBINXDIR"/PKGADDDEP | cut -f1 -d: | grep -i "$TARGET" > /dev/null 2>&1 || sudo grep -i "$TARGET" "$DEBINXDIR"/PKGPREBUILTDEP | cut -f1 -d: | grep -i "$TARGET" > /dev/null 2>&1; then
		if [ -f /tmp/.importinteractive ]; then
			if sudo grep -i "^Package: $TARGET$" "$TCEDIR"/import/debinx/debinx* > /dev/null 2>&1; then
				DEB=`sudo grep -m 1 -i "^Package: $TARGET$" "$TCEDIR"/import/debinx/debinx* | head -1 | awk '{print $2}'`
			elif sudo grep -i "^Package: $TARGET$" "$DEBINX" > /dev/null 2>&1; then
				DEB=`sudo grep -m 1 -i "^Package: $TARGET$" "$DEBINX" | awk '{print $2}'`
			elif sudo grep -i "^$TARGET:" "$DEBINXDIR"/PKGADDDEP > /dev/null 2>&1; then
				DEB=`sudo grep -m 1 -i "^$TARGET:" "$DEBINXDIR"/PKGADDDEP | cut -f1 -d:`
			elif sudo grep -i "^$TARGET:" "$DEBINXDIR"/PKGPREBUILTDEP  > /dev/null 2>&1; then
				DEB=`sudo grep -m 1 -i "^$TARGET:" "$DEBINXDIR"/PKGPREBUILTDEP | cut -f1 -d:`
			fi
			if [ -z "$DEB" ]; then
				echo " "
				echo "${YELLOW}Warning:${NORMAL} '"$TARGET"' is not available as a package, exiting.."
				echo "$TARGET is not available as a package." >> /tmp/sceimporterror
				exit 1
			fi  
		 else
			{ ls "$TCEDIR"/import/debinx/debinx* > /dev/null 2>&1 && sudo grep -i "^Package: $TARGET" "$TCEDIR"/import/debinx/debinx* | awk '{print $2}' ; sudo grep -i "^Package: $TARGET" "$DEBINX" | awk '{print $2}' ; sudo grep -i "^$TARGET" "$DEBINXDIR"/PKGADDDEP | cut -f1 -d: | grep "$TARGET" ; sudo grep -i "^$TARGET" "$DEBINXDIR"/PKGPREBUILTDEP | cut -f1 -d: | grep -i "$TARGET" ; } | sort | uniq  > /tmp/.sceimportselect 
			LINES=`/bb/wc -l /tmp/.sceimportselect | cut -f1 -d" "`
			if [ "$LINES" == "1" ]; then
				DEB=`cat /tmp/.sceimportselect`
				echo " "
				echo -n "Press Enter to import ${YELLOW}$DEB${NORMAL}, (q)uit exits: "
				read ans
				if [ "$ans" == "q" ] || [ "$ans" == "quit" ]; then
					cleanup
					exit 0
				else
					:
				fi
			else
				cat /tmp/.sceimportselect | select "Select package for '"$TARGET"'." "-"
				read DEB < /tmp/select.ans                                                                           
  				[ "$DEB" == "q" ] || [ "$DEB" == "quit" ] || [ "$DEB" == "" ] && exit 1
			fi
        
		fi
		if [ ! -f "$DEBINXDIR"/PKGEXCLUDELISTCUSTOM ]; then	
			if grep -wq "^$DEB$" "$DEBINXDIR"/PKGEXCLUDELIST > /dev/null 2>&1; then
				echo " "
				echo "${YELLOW}Warning:${NORMAL} $DEB is a blocked package, exiting.."
				echo "$DEB is a blocked package." >> /tmp/sceimporterror
				exit 1
			fi
		else
			if grep -wq "^$DEB$" "$DEBINXDIR"/PKGEXCLUDELISTCUSTOM > /dev/null 2>&1; then
			echo " "
			echo "${YELLOW}Warning:${NORMAL} $DEB is a blocked package, exiting.."
			echo "$DEB is a blocked package." >> /tmp/sceimporterror
			exit 1
			fi
		fi
  		echo "* Importing ${YELLOW}"$DEB"${NORMAL}."
		echo " "
  		mksce "$DEB"
	fi
  	
fi

echo " "
echo "${YELLOW}WARNING:${NORMAL} '"$TARGET"' is not available as a package, exiting.."
echo "$TARGET is not available as a package." >> /tmp/sceimporterror
exit 1

