note: close programs before uninstalling
Usage: "uninstall [searchword]" (if script is saved as "uninstall")
Also good for stripping docs, etc (like from deb installs)
Edit: added option to remove from Puppy Package Manager
1/1/11: added prompt for removing open directories
Code: Select all
#!/bin/bash -a
## jpeps Ver 2.0 Jan/2011
## Uninstalls files as listed in /root/.package dir
## Options:
## -s show usr-installed-packages
## package default: uninstall with prompts
## package -f force: uninstall without prompts
## package -c check to see if in use
## package -s view package files
## package [-f] -o overrides in-use function
FLAG="0" ## checks if main program is run (vs. a check)
## Function to remove open directories
flagOpen=""
RemOpenDir(){
for item in "$(ls ${OLDPATH})"; do
if [ "$item" ]; then
flagOpen="1"
else
rm -r ${OLDPATH}
echo "Removed: ${OLDPATH}" >>/tmp/uninstall.log
OLD="$(basename ${OLDPATH})"
NEWPATH="$(echo ${OLDPATH} | sed -e "s@\/${OLD}@@")"
OLDPATH="$NEWPATH"
fi
done
}
export -f RemOpenDir
### Check if app is in use before deleting
INUSE(){
[ -f /tmp/files ] && rm /tmp/files
[ -f /tmp/test ] && rm /tmp/test
CEXEC="$(find | grep -i "$1" | grep -v "builtin")"
echo "Checking: $CEXEC"
if [ "$CEXEC" ]; then
CFILES="$(cat "$CEXEC" | grep -e "\/bin\/" -e "\/games\/" -e "\/sbin\/" | sed '1d' )"
for I in "$CFILES"; do
echo -e "$I\n" >>/tmp/files
done
for I in `cat /tmp/files`; do
ps | grep `basename "$I"` | grep -v "grep" | grep -v "$0" >>/tmp/test
done
fi
[ -f /tmp/test ] && USE="$(cat /tmp/test)"
## cleanup
[ -f /tmp/test ] && rm /tmp/test
[ -f /tmp/files ] && rm /tmp/files
if [ "$USE" ]; then
echo -e $CYAN"\nIn Use:
${USE}"
exit
fi
[ "$FLAG" = "0" ] && exit
}
export -f INUSE
#########################
if [ -z "$1" ]; then
echo -e " Usage: 'uninstall [mypackage]' with prompts\n \
'uninstall -s' view user-installed packages\n \
'uninstall [package] -f' no prompts\n \
'uninstall [package] -c' check for usage
'uninstall [package] -s' view package files
'uninstall [package] [-f] -o' overrides in-use function"
exit
fi
cd /root/.packages
RM="rm -i" ## default: remove with prompt
## Show user-installed-packages option
[ "$1" = "-s" ] && ls | grep "\.files" | sed -e 's/\.files//g' && exit
[ "$2" = "-f" ] && RM="rm" ### force option (remove without prompt).
[ "$2" = "-c" ] && INUSE $1
PACKAGE="$(find | grep -i "$1" | grep -v "builtin_files")"
PET="$(echo "$PACKAGE" | sed -e 's/\.files/\.pet/' | cut -d/ -f2)"
NAME="$(echo ${PET} | sed -e 's/\.pet//')"
## Option to show package
[ "$2" = "-s" ] && cat "$PACKAGE" && exit
[ "$PACKAGE" ] && FILES="$(cat "$PACKAGE")"
if [ "$FILES" ]; then
FLAG="1"
[ "$3" = "-o" -o "$2" = "-o" ] || INUSE $1
for ITEM in "$FILES"; do
$RM $ITEM
done
## Option to remove open directories
echo -e "\nRemove Open Directories? (y/n)"
read ans
if [ "$ans" == "y" ]; then
[ -f /tmp/uninstall.log ] && rm /tmp/uninstall.log
while read line; do
if [ -d "$line" ]; then
OLDPATH="$line"
elif [ -f "$line" ]; then
VAR="$(basename ${line})"
OLDPATH="$( echo ${line} | sed -e "s@${VAR}@@")"
fi
RemOpenDir
done < "$PACKAGE"
fi
## Remove Listings
echo -e "\nRemoving listing from .packages"
$RM "$PACKAGE"
if [ "$RM" = "rm -i" ]; then
echo -e "\nRemove listing from Puppy Package Manager? (y/n)"
read ans
case "$ans" in
y ) sed -i "/${NAME}/d" ./user-installed-packages ;;
n ) echo "listing NOT removed" ;;
* ) echo "answer should be 'y' or 'n'" ;;
esac
else
sed -i "/${NAME}/d" ./user-installed-packages
fi
echo "Done!"
fi