# !/bin/bash
# A script to fix Cydia repository errors
# Changelog at http://secretss.com/cyfix
# v1.4c - 18th April 2010
#
# Thanks go to MadHouse and Helpmy360isEMO.

if [ "$(id -u)" != "0" ]; then
	echo -e "\nThis script must be run as root.\nTo login, type 'su', then key in your root\npassword at the prompt.\nOr type 'sudo cyfix' to run the script\n(only if you've edited your sudoers file).\n"
	exit
fi

if [ ! -e /usr/bin/wget ] && [ ! -e /usr/bin/plutil ]; then
	echo Installing necessary tools...
	if [ ! -e /usr/bin/apt-get ]; then
	     echo -e "Please install APT 0.7 Strict from Cydia.\nUnable to install necessary tools,\nexiting Cyfix.\n"
	     exit
	fi
	apt-get -qq install wget
	if [ -f /usr/bin/wget ]; then
	     echo 1 out of 2 items installed.
	else
	     echo -e "Unable to install wget.\nAre you connected to the Internet?"
	     exit
	fi
	apt-get -qq install com.ericasadun.utiliies
	if [ -f /usr/bin/plutil ]; then
	     echo 2 out of 2 items installed.
	else
	     echo -e "Unable to install Erica Utilities.\nPlease check your Internet connectivity and try again."
	     exit
	fi 
	echo -e "Done\n"
fi
if [ ! -e /usr/bin/wget ] || [ ! -e /usr/bin/plutil ]; then
	echo Installing necessary tools...
	if [ ! -e /usr/bin/apt-get ]; then
	     echo -e "Please install APT 0.7 Strict from Cydia.\nUnable to install necessary tools, exiting Cyfix.\n"
	     exit
	fi
	if [ ! -e /usr/bin/wget ]; then
	     apt-get -qq install wget
	     if [ ! -e /usr/bin/wget ]; then
		  echo -e "Unable to install wget.\nAre you connected to the Internet?"
		  exit
	     fi
	fi
	if [ ! -e /usr/bin/plutil ]; then
	     apt-get -qq install com.ericasadun.utilities
	     if [ ! -e /usr/bin/plutil ]; then
		  echo -e "Unable to install Erica Utilities.\nAre you connected to the Internet?"
		  exit
	     fi
	fi
	echo -e "Done.\n"
fi

case "$1" in "")
############## processing repo url ##################
echo -e "\nPlease input the problematic repo URL:"
echo -n http://
read repo

echo $repo > cyfix.log
if [ $(grep -c exitnow cyfix.log) == 0 ]; then
	rm cyfix.log
	continue
else
	echo -e "\nExiting now.\n"
	rm cyfix.log
	exit
fi

echo -e -n "\nProcessing repository url... "
if [[ -z "$repo" ]]; then
	echo -e "\nNo input detected. Exiting process.\n"
	exit
fi
if [ $(echo -n $repo | tail -c -1) == "/" ]; then
	repo=$(echo -n $repo | head -c -1)
fi
repo=$(echo $repo | tr -d ' ')

e=$(echo $repo | sed 's/\(.\)/\1 /g')
a=($e)
j=0
for i in $(seq 0 $((${#a[*]} - 1))); do
	b[$j]=${a[$i]}
	c[$i]=${a[$i]}
	if [ ${a[$i]} == "/" ]; then
		b[$j]="\\"
		c[$i]="_"
		let j=j+1
		b[$j]="/"
	fi
	if [ ${a[$i]} == "[" ]; then
		echo -e "\nErroneous input. Please try again.\n"
		exit
	fi
	let j=j+1
done

reponame=$(echo ${b[*]} | tr -d ' ')
filename=$(echo ${c[*]} | tr -d ' ')
file1=$(echo $filename _._Packages | tr -d ' ')
file2=$(echo $filename _._Release | tr -d ' ')
file3=$(echo $filename _._Packages.IndexDiff | tr -d ' ')
file4=$(echo $filename _._Release.gpg | tr -d ' ')
file5=$(echo $filename _._en | tr -d ' ')
############## processing repo url ##################

############## 1. lists ##################
cd /var/lib/apt/lists

boo=0
if [ -f $file1 ]; then
	boo=1
	 rm $file1
fi
if [ -f $file2 ]; then
	boo=1
	 rm $file2
fi
if [ -f $file3 ]; then
	boo=1
	 rm $file3
fi
############## lists ##################

############## 2. cydia.list ##################
cd /etc/apt/sources.list.d

before=$(md5sum cydia.list)
cat cydia.list | grep -v $repo > cydia1.list
mv -f cydia1.list cydia.list

if [[ $before != $(md5sum cydia.list) ]]; then
	boo=1
fi
############## cydia.list ##################

############## check ##################
if [ $boo == 0 ]; then
	cd /var/lib/apt/lists
	ls > cyfix.log

	if [ $(grep -c $filename cyfix.log) == 0 ]; then
		baa=0
	else
		baa=1
	fi
fi

if [ $boo == 0 ] && [ $baa == 1 ]; then
       echo -e "\nThis is a default Cydia repository. Are you\nsure you want to continue the removal\nprocess? (1 or 2):"
	echo -e "\t1 = Continue
\t2 = Exit"
       read ans
       if [ $ans == 1 ]; then
		cd /var/lib/apt/lists
		cydia=$(grep $filename cyfix.log)
		for word in $cydia; do
			rm $word
		done
		rm cyfix.log
		cd /var/lib/apt/lists/partial
		ls > cyfix.log
		cydia1=$(grep $filename cyfix.log)
		for word in $cydia1; do
			rm $word
		done
		rm cyfix.log
       elif [ $ans == 2 ]; then
	      echo  
	      exit
       else
	      echo -e "Please input either 1 or 2.\n"
	      exit
       fi
elif [ $boo == 0 ] && [ $baa == 0 ]; then
	echo -e "\nRepository not found. Did you type it right?\n"
	exit
fi

echo Done.
echo -n "Erasing... "
echo Done.
echo -n "Eliminating... "
echo Done.
############## check ##################

############## 3. partial ##################
echo -n "Eradicating... "
cd /var/lib/apt/lists/partial

if [ -f $file4 ]; then
	rm $file4
fi
if [ -f $file5 ]; then
	rm $file5
fi

echo Done.
############## partial ##################

############## 4. metadata.plist ##################
echo -n "Exterminating... "
cd /var/lib/cydia

plutil -convert xml1 metadata.plist > cyfix.log
rm cyfix.log
start="<key>deb:http:\/\/$reponame\/:.\/<\/key>"
stop="<\/dict>"
echo -e "$(cat metadata.plist | sed -e "/$start/,/$stop/ s/.*//")" > metadata1.plist
mv -f metadata1.plist metadata.plist

echo Done.
############## metadata.plist ##################

############## 5. sources.list.d ##################
echo -n "Extirpating... "
cd /etc/apt/sources.list.d

file6=$(grep -l $repo *.list)
if [[ -z "$file6" ]]; then
	continue
else
	if [ -f $file6 ]; then
		rm $file6
	fi
fi

echo Done.
############## sources.list.d ##################

echo -e "\nProcess complete.\nFor best results please respring your device\nbefore launching Cydia.\n"
;;

############## update script ##################
-u)
cd /usr/bin
before=$(md5sum cyfix)
echo -e "\nChecking for updates. If an update is\navailable, download will start immediately.\nPlease do not interrupt the process.\n"
wget -N -q http://dl.dropbox.com/u/1838452/cyfix
chmod 775 cyfix
if [[ $before != $(md5sum cyfix) ]]; then
	echo -e "Cyfix has been successfully updated.\n"
	exit
else
	echo -e "No updates available.\n"
fi
;;

############## help ##################
-h)
echo -e "\nTo use, type 'cyfix' and enter the repo\nurl at the prompt. Available options:
\t-h Displays this help text
\t-u Checks for Cyfix updates"
echo -e "\nTip: If you mess up your repo url input,\ntyping 'exitnow' will quit Cyfix.\n"
;;

############## credits ##################
*)
echo -e "\nv1.4c Written by Secretss[C4iD]"
echo Contact Secretss[C4iD] on Rizon IRC server
echo or @secretss777 on Twitter.
echo -e "Type 'cyfix -h' for help.\n"
;;

esac
exit