#!/bin/sh

#  unstash_func.sh
#
#Unstasher by @if0xxx
#lets fix some stupid behavior
unstash_share(){
	if [ -L /usr/share ]
		then 
		echo "start unstashing share"
		sleep 1s
		echo "creating temp dir"
		mkdir -p /tmp 
		mkdir -p /tmp/share
		echo "moving share to tmp"
		mv /usr/share/* /tmp/share
		echo "finished moving share now cleaning stuff"
		rm -rf /usr/share
		echo "reacreating App folder"
		mv /tmp/share /usr/
		echo "fix folder permission"
		chmod 755 /usr/share
		echo "done with unstashing share"
		stashed1="true"
	else 
		echo "share is already unstashed"
		stashed1="false"
	fi
}
unstash_apps(){
	if [ -L /Applications ]
		then 
		echo "start unstashing apps"
		sleep 1s
		echo "creating temp dir"
		mkdir -p /tmp 
		mkdir -p /tmp/Applications
		echo "moving Apps to tmp"
		mv /Applications/* /tmp/Applications
		echo "finished moving Apps now cleaning stuff"
		rm -rf /Applications
		echo "reacreating App folder"
		mv /tmp/Applications /
		echo "fix folder permission"
		chmod 775 /Applications
		echo "done with unstashing Apps"
		stashed2="true"
	else 
		echo "Apps are already unstashed"
		stashed2="false"
	fi
}
unstash_ring(){
	if [ -L /Library/Ringtones ]
		then 
		echo "start unstashing ringtones"
		sleep 1s
		echo "creating temp dir"
		mkdir -p /tmp
		mkdir -p /tmp/Ringtones
		echo "moving Ringtones to tmp"
		mv /Library/Ringtones/* /tmp/Ringtones
		echo "finished moving Ringtones now cleaning stuff"
		rm -rf /Library/Ringtones
		echo "reacreating Ringtone folder"
		mv /tmp/Ringtones /Library
		echo "fix folder permission"
		chmod 775 /Library/Ringtones
		echo "done with unstashing Ringtones"
		stashed3="true"
	else 
		echo "Ringtones are already unstashed"
		stashed3="false"
	fi
}
unstash_wall(){
	if [ -L /Library/Wallpaper ]
		then 
		echo "start unstashing Wallpaper"
		sleep 1s
		echo "creating temp dir"
		mkdir -p /tmp
		mkdir -p /tmp/Wallpaper
		echo "moving Wallpaper to tmp"
		mv /Library/Wallpaper/* /tmp/Wallpaper
		echo "finished moving Wallpaper now cleaning stuff"
		rm -rf /Library/Wallpaper
		echo "reacreating Ringtone folder"
		mv /tmp/Wallpaper /Library
		echo "fix folder permission"
		chmod 775 /Library/Wallpaper
		echo "done with unstashing Wallpaper"
		stashed4="true"
	else
		echo "Wallpapers are already unstashed"
		stashed4="false"
	fi

}
unstash_all(){
	unstash_apps
	unstash_ring
	unstash_wall
	unstash_share
}
stopcydiasstashing(){
	if ! [ -f /.cydia_no_stash ]
		then
		echo "stop cydias future stashing now"
		echo -n > /.cydia_no_stash
	else 
		echo "Cydias stashing is already disabled"
	fi
}
rebot(){
	echo "----------we are done----------"
	echo "to finish reboot (hit enter)"
	read rebot 
	uicache && reboot
}
checkforyucca(){
	if [ -f /usr/bin/YuccaPackager ]
		then 
		echo "----------ERROR----------"
		echo "Yucca stasher is installed"
		sleep 1s
		echo "please remove this package first"
		sleep 2s
		echo "do you want to autremove this? (Apt-get needed)[y/n]"
		read yucca 
		if [ "$yucca" = "y" ]
			then apt-get remove io.ijapija00.yuccapackager
			unstash_func
		elif [ "$yucca" = "Y" ]
			then apt-get remove io.ijapija00.yuccapackager
			unstash_func
		else 
			exit
		fi
	fi
}
checkforyucca
echo "----------unstasher----------"
echo " "
echo "unstash everything   [1]"
echo "unstash Applications [2]"
echo "unstash Ringtones    [3]"
echo "unstash Wallpapers   [4]"
if [ -f /usr/bin/unstash_collstar ]
	then echo "unstash Coolstars Stashing (partial working) [5]"
fi
read choice
if [ "$choice" = "exit" ]
	then exit
fi
if [ -f /.cydia_no_stash ]
	then 
	echo "----------WARNING----------"
	echo ".cydia_no_stash exists"
	sleep 1s
	echo "some files are maybe already unstashed"
	echo "Unstasher only touches stashed files"
	exists="yes"
	sleep 2s
	echo "continue? [y/n]"
	read con 
	if [ "$con" = "n" ]
		then exit
	elif [ "$con" = "N" ]
		then exit
	fi
fi 
if [ $choice = 1 ]
	then 
	echo "come on Eileen (do your job)"
	sleep 1s
	unstash_all
	stopcydiasstashing
	echo "toora loora toora loo" 
elif [ $choice = 2 ]
	then 
	echo "come on Eileen (do your job)"
	sleep 1s
	unstash_apps
	unstash_share
	stopcydiasstashing
	echo "toora loora toora loo"
elif [ $choice = 3 ]
	then 
	echo "come on Eileen (do your job)"
	sleep 1s
	unstash_ring
	unstash_share
	stopcydiasstashing
	echo "toora loora toora loo"
elif [ $choice = 4 ]
	then 
	echo "come on Eileen (do your job)"
	sleep 1s
	unstash_wall
	unstash_share
	stopcydiasstashing
	echo "toora loora toora loo"
elif [ -f /usr/bin/unstash_collstar ]
	then 
	if [ $choice = 5 ]
		then 
		echo "come on Eileen (do your job)"
		sleep 1s
		unstash_coolstar
		echo "toora loora toora loo"
	fi
else
	echo "wrong charakter try again"
fi
if [ "$stashed1" = "true" ]
	then rebot
elif [ "$stashed2" = "true" ]
	then rebot
elif [ "$stashed3" = "true" ]
	then rebot
elif [ "$stashed4" = "true" ]
 	then rebot
else 
	echo "----------we are done----------"
fi