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

useBusybox

sudo chown "$TCUSER":staff /tmp
sudo chmod 1777 /tmp
TCUSER=`cat /etc/sysconfig/tcuser`
TCEDIR=`readlink /etc/sysconfig/tcedir`
SCEBOOTLST=""$TCEDIR"/sceboot.lst"
checknotroot
BUILD=`getBuild`
HERE=`pwd`


if [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
	echo "Usage:"
	echo "'"importsce"' with no options will prompt for characters of desired package name."
	echo "'"importsce -l"' to use a file list of packages - '"importsce -l FILENAME"'."
	echo "'"importsce -b"' to add package to sceboot.lst."
	echo "'"importsce -r"' to use RAM for unpacking source debs."
	echo "'"importsce -s"' to list sizes of packages to be fetched and installed."
	echo "'"importsce -d"' to make use of existing SCE packages as dependencies.  A select menu will appear."
	echo "'"importsce -o"' to add resulting SCE package to ondemand."
	echo "'"importsce -n"' for non interactive mode."
	echo "'"importsce -u"' for update mode, syncing a new debinx files."
	echo "'"importsce -p"' for preserve debinx mode, use existing rather than fetch new for better performance." 
exit 1
fi

mksce() {
	if [ "$FLAGS" ]
	then
		sudo deb2sce "$FLAGS" "$1"             
	else
		sudo deb2sce "$1"             
	fi

}

exit_tcnet() {
	echo "There is an issue connecting to `cat /opt/tcemirror`, exiting.."
	exit 1
}

[ -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/.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

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


unset FLAGS PACKAGELIST
while getopts drsbolknup OPTION
do
	case ${OPTION} in
		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 ;;
		*) exit 1 ;;
	esac
done



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

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

shift `expr $OPTIND - 1`

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

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

wget -O deb2sce.tar.gz -cq "$PREBUILTMIRROR"/deb2sce.tar.gz 2>/dev/null ||  exit_tcnet

tar xvf /tmp/deb2sce.tar.gz PKGEXTRAREPODEP >/dev/null 2>&1
[ -f /tmp/PKGEXCLUDELIST ] || tar xvf /tmp/deb2sce.tar.gz PKGEXCLUDELIST >/dev/null 2>&1
tar xvf /tmp/deb2sce.tar.gz PKGADDDEP >/dev/null 2>&1
tar xvf /tmp/deb2sce.tar.gz PKGEXTRAFILES >/dev/null 2>&1
tar xvf /tmp/deb2sce.tar.gz PKGPREBUILTDEP >/dev/null 2>&1
tar xvf /tmp/deb2sce.tar.gz PKGEXTRAFILEMD5SUMLIST >/dev/null 2>&1
tar xvf /tmp/deb2sce.tar.gz PKGDATAFILEMD5SUMLIST >/dev/null 2>&1
tar xvf /tmp/deb2sce.tar.gz PREBUILTMD5SUMLIST >/dev/null 2>&1
cd "$HERE"

if grep -wq "^$TARGET$" /tmp/PKGEXCLUDELIST > /dev/null 2>&1; then
	echo " "
	echo "$TARGET is a blocked package.  Exiting.."
	echo " "
	exit 0
fi

# Determine if .lst file is being used or list specified, copy to /tmp/.targetfile
if [ "$PACKAGELIST" == "TRUE" ]; then
  TARGETFILE=`readlink -f "$1"`
  sudo cp $TARGETFILE /tmp/.targetfile
  TARGET=`basename $1`
elif [ -s "$TCEDIR"/sce/"$1".sce.lst ]; then
  TARGETFILE="$TCEDIR"/sce/"$1".sce.lst
  sudo cp "$TCEDIR"/sce/"$1".sce.lst /tmp/.targetfile
  PACKAGELIST=TRUE
fi



# Determine if the -d option or the existence of a dep file
# is present and use dep file if found.
if grep -i "^DEPS=TRUE" /etc/sysconfig/sceconfig > /dev/null 2>&1; then
	touch /tmp/.importdep
	echo "Using the -d option."
elif [ "$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 dep file found and will be used."
fi

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

# Determine if RAM is going to be used for unpacking.
if grep -i "^RAM=TRUE" /etc/sysconfig/sceconfig > /dev/null 2>&1; 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; then
	touch /tmp/.keepdoc
	echo "Using the -k option."
elif [ "$KEEPDOC" == "TRUE" ]; then
	touch /tmp/.keepdoc
	echo "Using the -k option."
fi

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

if grep -i "^PRESERVEDEBINXMODE=TRUE" /etc/sysconfig/sceconfig > /dev/null 2>&1; then
	PRESERVEDEBINXMODE=TRUE
	echo "Using the -p option."
elif [ "$PRESERVEDEBINXMODE" == "TRUE" ]; then
	PRESERVEDEBINXMODE=TRUE
	echo "Using the -p option."
else
	UPDATEDEBINXMODE=TRUE
	PRESERVEDEBINXMODE=FALSE
fi

if grep -i "^UPDATEDEBINXMODE=TRUE" /etc/sysconfig/sceconfig > /dev/null 2>&1; then
	UPDATEDEBINXMODE=TRUE
	PRESERVEDEBINXMODE=FALSE
	echo "Using the -u option."
elif [ "$UPDATEDEBINXMODE" == "TRUE" ]; then
	PRESERVEDEBINXMODE=FALSE
	echo "Using the -u option."
fi



# 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/{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 "WARNING: "$REALDIR" resides on a Windows filesystem."
					echo "Please try again with the -r flag to unpack files in RAM."
					exit 1
				fi	
			;;

esac

echo " "

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

[ -n "$TARGET" ] || exit 1

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

if [ "$UPDATEDEBINXMODE" == "TRUE" ]; then
	sudo debGetEnv "$2"
else
	[ -s /tmp/debinx ] || sudo debGetEnv "$2"
fi

read DEBINX < /tmp/debinx
DEBINX="/etc/sysconfig/tcedir/$DEBINX"
# Below is where -l list file option is being used.
if [ "$PACKAGELIST" ] && [ -f "$TARGETFILE" ]; then
  echo " "
  if [ -f /tmp/.importinteractive ]; then
  	echo -n "Creating "$TARGET".sce from package list "$TARGETFILE""
  	mksce "$TARGET" 
  else
	echo -n "Do you want to create "$TARGET".sce from package list "$TARGETFILE"? (y/N): " 
  	read ans                   
  	[ "$ans" == "y" ] || exit 0                          
  	mksce "$TARGET" 
  fi  
# Below is where an existing .lst file is found in sce dir.
elif [ "$PACKAGELIST" ] && [ -f "$TCEDIR"/sce/"$1".sce.lst ]; then
  echo " "
  if [ -f /tmp/.importinteractive ]; then
  	echo -n "Creating "$TARGET".sce from package list "$TARGETFILE""
  	mksce "$TARGET" 
  else
	echo -n "Do you want to create "$TARGET".sce from package list "$TARGETFILE"?"
  	read ans                   
  	[ "$ans" == "y" ] || exit 0                          
  	mksce "$TARGET" 
  fi  
# Below checks for package in the extra repos first, then the 
# Debian main repos, then in the prebuilt section, in that order.
elif sudo grep -i "^Package: $TARGET" "$TCEDIR"/debinx* > /dev/null 2>&1 ||  sudo grep -i "^Package: $TARGET" "$DEBINX" > /dev/null 2>&1 || sudo grep -i "$TARGET" /tmp/PKGADDDEP | cut -f1 -d: | grep -i "$TARGET" > /dev/null 2>&1 || sudo grep -i "$TARGET" /tmp/PKGPREBUILTDEP | cut -f1 -d: | grep -i "$TARGET" > /dev/null 2>&1; then
  if [ -f /tmp/.importinteractive ]; then
	if sudo grep -i "^Package: $TARGET$" "$TCEDIR"/debinx* > /dev/null 2>&1; then
		DEB=`sudo grep -i "^Package: $TARGET$" "$TCEDIR"/debinx* | awk '{print $2}'`
	elif sudo grep -i "^Package: $TARGET$" "$DEBINX" > /dev/null 2>&1; then
		DEB=`sudo grep -i "^Package: $TARGET$" "$DEBINX" | awk '{print $2}'`
	elif sudo grep -i "^$TARGET:" /tmp/PKGADDDEP > /dev/null 2>&1; then
		DEB=`sudo grep -i "^$TARGET:" /tmp/PKGADDDEP | cut -f1 -d:`
	elif sudo grep -i "^$TARGET:" /tmp/PKGPREBUILTDEP  > /dev/null 2>&1; then
		DEB=`sudo grep -i "^$TARGET:" /tmp/PKGPREBUILTDEP | cut -f1 -d:`
	fi
	if [ -z "$DEB" ]; then
		{ sudo grep -i "^Package: $TARGET" "$TCEDIR"/debinx*  | awk '{print $2}' ; sudo grep -i "^Package: $TARGET" "$DEBINX" | awk '{print $2}' ; sudo grep -i "$TARGET" /tmp/PKGADDDEP | cut -f1 -d: | grep "$TARGET" ; sudo grep -i "$TARGET" /tmp/PKGPREBUILTDEP | cut -f1 -d: | grep -i "$TARGET" ; } | sort | uniq | select "Select Package for $1" "-"
  		read DEB < /tmp/select.ans                                                                           
  		[ "$DEB" == "q" ] && exit 1
	fi     
  else
	{ ls "$TCEDIR"/debinx* > /dev/null 2>&1 && sudo grep -i "^Package: $TARGET" "$TCEDIR"/debinx*  | awk '{print $2}' ; sudo grep -i "^Package: $TARGET" "$DEBINX" | awk '{print $2}' ; sudo grep -i "$TARGET" /tmp/PKGADDDEP | cut -f1 -d: | grep "$TARGET" ; sudo grep -i "$TARGET" /tmp/PKGPREBUILTDEP | cut -f1 -d: | grep -i "$TARGET" ; } | sort | uniq | select "Select Package for $1" "-"
  	read DEB < /tmp/select.ans                                                                           
  	[ "$DEB" == "q" ] && exit 1     
  fi 
  echo " "   
  if [ -f /tmp/.importinteractive ]; then
	echo "Importing $DEB."
	mksce "$DEB"
  else                                                               
  	echo -n "Do you want to import $DEB? (y/N): "                        
  	read ans                                                             
  	[ "$ans" == "y" ] || exit 1                                           
  	mksce "$DEB"
  fi                                                           
else
 echo ""$TARGET" is not available as a package.  Exiting..."
 exit 1
fi

