#!/bin/sh

TCEDIR=/etc/sysconfig/tcedir
TCUSER=`cat /etc/sysconfig/tcuser`
YEAR="2014"
MONTH="06"
DAY="01"
BUILD="i686"
WORK=/tmp/dCore-arch/work 
APP="$1"
REPO="core extra community"
read IMPORTMIRROR < /opt/tcemirror  
IMPORTMIRROR="${IMPORTMIRROR%/}/dCore/arch"
PREBUILTMIRROR="${IMPORTMIRROR%/}/"$BUILD"/"
	 

recurse() {
 recursive_scan() {
  grep "^$1$" /tmp/deplist > /dev/null 2>&1 && continue	
  echo ${1} >> /tmp/deplist
  deps=""
  if [ -s ${1}.dep ]; then
	
    deps=`cat ${1}.dep`
    for d in $deps; do
      recursive_scan $d
    done
  fi
}

#sed -i "s/^[          ]//g" *.dep
if [ -f ${1}.dep ]; then
 
   for F in `cat ${1}.dep`; do
    if [ -f "$F".dep ]; then		
     for K in `cat "$F".dep`; do 
	
	 recursive_scan "$K"
	 
	 for E in `cat "$1".dep`; do 

	  if cat /tmp/deplist | grep -v "$K" | grep "^$E$" >/dev/null; then
	    echo "$E"
	   
	   sed -i "/"^$E$"/d" "$1".dep
	  fi
	  done
	#cat /tmp/deplist > /tmp/"$K".files
	rm /tmp/deplist
	
      done
    fi	

   done
else
 continue
fi

}

[ -f /tmp/archscripts.tar.gz ] && sudo rm /tmp/archscripts.tar.gz

sudo busybox wget "$IMPORTMIRROR"/archscripts.tar.gz -O /tmp/archscripts.tar.gz

tar xf /tmp/archscripts.tar.gz -C /tmp/

for I in `echo "$REPO"`; do 
	[ -d /tmp/dCore-arch-"$I" ] || sudo mkdir -p /tmp/dCore-arch-"$I"
	sudo chown "$TCUSER":staff /tmp/dCore-arch-"$I"
	cd /tmp/dCore-arch-"$I"
	if [ -f "$I".db.tar.gz.md5.txt ] && md5sum -c "$I".db.tar.gz.md5.txt > /dev/null 2>&1; then
		sudo tar xf "$I".db.tar.gz
	else
		sudo rm "$I".db.tar.gz* > /dev/null 2>&1
		busybox wget http://seblu.net/a/arm/"$YEAR"/"$MONTH"/"$DAY"/"$I"/os/"$BUILD"/"$I".db.tar.gz
		md5sum "$I".db.tar.gz > "$I".db.tar.gz.md5.txt
		sudo tar xf "$I".db.tar.gz
	fi
done

if ! find /tmp/dCore-arch*/"$APP"-[0-9]* > /dev/null 2>&1 && ! grep "^$1:" /tmp/PKGADDDEP > /dev/null 2>&1 && ! grep "^$1:" /tmp/PKGPREBUILT > /dev/null 2>&1 ; then
	echo ""$APP" does not exist."
	exit 1
fi


sudo mkdir -p "$WORK"
cd "$WORK"
sudo chown "$TCUSER":staff "$WORK"

> /tmp/.archdeps

getarchDeps(){
ls /tmp/dCore-arch*/"$1"-[0-9]* > /dev/null 2>&1 || continue
APPDIR=`find /tmp/dCore-arch*/"$1"-[0-9]* | head -n1`
grep -A20 "%DEPENDS%" "$APPDIR"/depends | grep -v "%DEPENDS%" | \
sed '/^$/q' | cut -f1 -d: | cut -f1 -d">" | cut -f1 -d"=" | cut -f1 -d"<"
}

getDeps() {
DEPLIST=" $1 $DEPLIST "
ls /tmp/dCore-arch*/"$1"-[0-9]* > /dev/null 2>&1 || continue
APPDIR=`find /tmp/dCore-arch*/"$1"-[0-9]* | head -n1`
if [ ! -z "`grep -A20 "%DEPENDS%" "$APPDIR"/depends | grep -v "%DEPENDS%" | sed '/^$/q'`" ]; then
	for E in `grep -A20 "%DEPENDS%" "$APPDIR"/depends | grep -v "%DEPENDS%" | sed '/^$/q'`; do 
		D=\'"$E"\'
		G=`echo "$D" | cut -f1 -d: | cut -f1 -d">" | cut -f1 -d"<" | cut -f1 -d"="`
		F="${G#\'}"
		F="${F%\'}"
		H=" ${F%\'} "
		if echo "$DEPLIST" | grep "$H" > /dev/null 2>&1; then
			continue
		else 
			getDeps "$F"
		fi
	done
else
	continue
fi
}

getDepsold() {
#echo "$DEPLIST" | grep " "$1" " > /dev/null 2>&1 && continue
grep "^$1$" /tmp/.archdeps > /dev/null 2>&1 && continue
echo "$1" >> /tmp/.archdeps
echo "$1"
DEPLIST=" $1 $DEPLIST "
if grep "^$1:" /tmp/PKGPREBUILT > /dev/null 2>&1; then
#	DEPLIST="`grep "^$1:" /tmp/PKGPREBUILT | cut -f2 -d:` $DEPLIST"	
	for I in `grep "^$1:" /tmp/PKGPREBUILT | cut -f2 -d:`; do 
		
		getDeps "$I"
	done

elif grep "^$1:" /tmp/PKGADDDEP > /dev/null 2>&1; then
#	DEPLIST="`grep "^$1:" /tmp/PKGADDDEP | cut -f2 -d:` $DEPLIST"
	for I in `grep "^$1:" /tmp/PKGADDDEP | cut -f2 -d:`; do 
		
		getDeps "$I"
	done

else

	DEPLIST=`getarchDeps "$1" 2>/dev/null`
fi
for D in $DEPLIST; do getDeps $D; done
}

getDeps "$APP" 
echo "$DEPLIST"
for I in `echo "$DEPLIST"`; do 
	if grep "^$I:" /tmp/PKGPREBUILT > /dev/null 2>&1; then
		busybox wget "$PREBUILTMIRROR"/"$I".tcz -O "$TCEDIR"/optional/"$I".tcz 
		busybox wget "$PREBUILTMIRROR"/"$I".tcz.dep -O "$TCEDIR"/optional/"$I".tcz.dep 
		busybox wget "$PREBUILTMIRROR"/"$I".tcz.list -O "$TCEDIR"/optional/"$I".tcz.list 
		busybox wget "$PREBUILTMIRROR"/"$I".tcz.md5.txt -O "$TCEDIR"/optional/"$I".tcz.md5.txt 
		for N in `grep "^$I:" /tmp/PKGPREBUILT | cut -f2 -d:`; do 
			grep "^$N$" "$TCEDIR"/optional/"$I".tcz.dep || echo "$N".tcz >> "$TCEDIR"/optional/"$I".tcz.dep
		done
		continue
	fi
	ls /tmp/dCore-arch*/"$I"-[0-9]* > /dev/null 2>&1 || continue
	cd "$TCEDIR"/optional
	if [ -f "$I".tcz ]; then
		if md5sum -c "$I".tcz.md5.txt; then
			continue
		else
			sudo rm "$I".tcz* > /dev/null 2>&1
		fi
	fi
	cd "$WORK"
	echo "$I"
	APPDIR=`find /tmp/dCore-arch*/"$I"-[0-9]* | head -n1`
	[ -d "$APPDIR" ] || continue
	MD5SUM=`grep -A1 "%MD5SUM%" "$APPDIR"/desc | grep -v MD5SUM`
	FILENAME=`grep -A1 "%FILENAME%" "$APPDIR"/desc | grep -v FILENAME`
	FILENAME="${FILENAME#.}"
	REPO=`find /tmp/dCore-arch*/"$I"-[0-9]* | cut -f3 -d"-" | cut -f1 -d"/" | head -n1`
	echo "$REPO"
	if [ -f "$FILENAME" ]; then
		if [ "`md5sum "$FILENAME" | cut -f1 -d" "`" != "$MD5SUM" ]; then
			sudo rm "$FILENAME"
		fi
	fi
	DEPENDS=`grep -A20 "%DEPENDS%" "$APPDIR"/depends | grep -v "%DEPENDS%" | sed '/^$/q'`
	[ -f "$FILENAME" ] || sudo busybox wget -c http://seblu.net/a/arm/"$YEAR"/"$MONTH"/"$DAY"/"$REPO"/os/"$BUILD"/"$FILENAME" 
	if [ "`md5sum "$FILENAME" | cut -f1 -d" "`" == "$MD5SUM" ]; then
		mkdir -p "$I"
		sudo busybox tar xf "$FILENAME" -C "$I"
		sudo mkdir -p "$I"/usr/local/archinstall
		[ -f "$I"/.INSTALL ] && sudo cp "$I"/.INSTALL "$I"/usr/local/archinstall/"$I".postinst
		[ -f "$I"/.INFO ] && sudo cp "$I"/.INFO "$I"/usr/local/archinstall/"$I".info
		[ -f "$I"/.PKGINFO ] && sudo cp "$I"/.PKGINFO "$I"/usr/local/archinstall/"$I".pkginfo
		sudo mkdir -p "$I"/usr/local/tce.installed
		SCRIPT='sh /usr/local/archinstall/'${I}.postinst' post_install'
		sudo chown "$TCUSER":staff "$I"/usr/local/tce.installed
		echo "#!/bin/sh" > "$I"/usr/local/tce.installed/"$I"
		echo " " >> "$I"/usr/local/tce.installed/"$I"
		[ -f /tmp/"$I".arch2tcz ] && cat /tmp/"$I".arch2tcz >> "$I"/usr/local/tce.installed/"$I"
		echo " " >> "$I"/usr/local/tce.installed/"$I"
		[ -f "$I"/.INSTALL ] && echo "${SCRIPT}" >> "$I"/usr/local/tce.installed/"$I"
		sudo chmod -R 775 "$I"/usr/local/tce.installed
		sudo chown -R root:staff "$I"/usr/local/tce.installed
		if grep "^$I-data.tar.gz" /tmp/PKGEXTRAFILES > /dev/null 2>&1; then
			[ -f "$I"-data.tar.gz ] && sudo rm "$I"-data.tar.gz*
			sudo busybox wget -c "$IMPORTMIRROR"/"$I"-data.tar.gz
			sudo busybox wget -c "$IMPORTMIRROR"/"$I"-data.tar.gz.md5.txt
			if md5sum -c "$I"-data.tar.gz.md5.txt > /dev/null 2>&1; then
				sudo tar xvf "$I"-data.tar.gz -C "$I"
			else
				echo "MD5SUM of "$I"-data.tar.gz extra data file failed.  Exiting..."
				exit 1
			fi
		fi
		
		sudo mksquashfs "$I" "$TCEDIR"/optional/"$I".tcz
		for D in `grep -A20 "%DEPENDS%" "$APPDIR"/depends | grep -v "%DEPENDS%" | sed '/^$/q'`; do 
			E=\'"$D"\'
			G=`echo "$E" | cut -f1 -d: | cut -f1 -d">" | cut -f1 -d"<" | cut -f1 -d"="`
			F="${G#\'}"
			H="${F%\'}"
			ls /tmp/dCore-arch*/"$H"-[0-9]* > /dev/null 2>&1 && echo "$H".tcz >> "$TCEDIR"/optional/"$I".tcz.dep
		done
		for M in `grep "^$I:" /tmp/PKGADDDEP | cut -f2 -d:`; do 
			grep "^$M$" "$TCEDIR"/optional/"$I".tcz.dep || echo "$M".tcz >> "$TCEDIR"/optional/"$I".tcz.dep
		done
		cd "$TCEDIR"/optional
		sudo md5sum "$I".tcz > "$I".tcz.md5.txt
		recurse "$I".tcz
		cd "$WORK"
		find "$I" | sed "s:"^$I"::g" > "$TCEDIR"/optional/"$I".tcz.list
		sudo rm -r "$I"	> /dev/null 2>&1
	else
		echo "MD5SUM of "$FILENAME" did not pass.  Exiting."
		exit 1
	fi

done







	

