Major overhaul -- most scripts rewritten or updated.
[pkgusr] / usr / lib / pkgusr / uninstall_package
1 #!/bin/sh
2 # Original...
3 # Copyright (c) 2004 Matthias S. Benkmann <article AT winterdrache DOT de>
4 # You may do everything with this code except misrepresent its origin.
5 # PROVIDED `AS IS' WITH ABSOLUTELY NO WARRANTY OF ANY KIND!
6
7 # Copyright (C) 2007 - 2014 Steve Youngs <steve@sxemacs.org>
8 # Originally, all this script did was to echo a command to stdout.  It didn't
9 # actually do any deleting.  To remove the package you had to kill/yank that
10 # command and then remove the "echo"s in it to get the job done.
11
12 # Rewritten to eliminate the need for kill/yank and echo removal.  The default
13 # is still a dry-run, but now the "wet" run is achieved by adding a 2nd argument
14 # to the command line: `now'.  The `dry_run' is piped through less(1) for easier
15 # inspection.  And errors are redirected to /tmp/<pkgname>.err during the real
16 # uninstall.
17
18 if [ $(id -u) -eq 0 ]; then
19         echo 1>&2 "It is too hazardous to delete packages with root"
20         echo 1>&2 "Aborting"
21         exit 1
22 fi
23
24 usage()
25 {
26         cat<<EOF
27 USAGE: uninstall_package [now]
28
29 Unless you specify "now" as the 1st arg, nothing will actually
30 be deleted.
31
32 EOF
33 }
34
35 pkg=$(whoami)
36
37 dry_run()
38 {
39     forall_direntries_from ${pkg} -not -type d -exec echo rm -vf {} 2>/dev/null \;
40     suid=$(forall_direntries_from ${pkg} -not -user ${pkg})
41     if [ -n "${suid}" ]; then
42         echo '###'
43         echo '# There were setuid binaries found.  You _could_ use root'
44         echo '# to delete this package, but you run a very real risk of'
45         echo '# completely FUCKING UP your system.  Instead, run this:'
46         echo '#'
47         echo '#  forall_direntries_from ${pkg} -not -user ${pkg}'
48         echo '#'
49         echo '# and delete those files manually and individually'
50         echo '###'
51     fi
52     echo
53     echo Use: \"uninstall_package now\" to really remove this package.
54     echo Any errors will be redirected to /tmp/${pkg}.err
55 }
56
57 run()
58 {
59         # Delete anything that isn't a directory
60         forall_direntries_from ${pkg} -ignore_readdir_race \
61                 -not -type d -exec rm -vf {} 2>>/tmp/${pkg}.err \;
62         # Remove any empty directories, but ONLY empty directories
63         forall_direntries_from ${pkg} -ignore_readdir_race \
64                 -type d -empty -exec rmdir -v {} 2>>/tmp/${pkg}.err \;
65
66         leftovers=$(forall_direntries_from ${pkg})
67         if [ -s /tmp/${pkg}.err -a -n "${leftovers}" ]; then
68                 echo Errors were reported.  Please inspect /tmp/${pkg}.err
69         else
70         # Bring ~/.project inline with reality
71         sed -i -e 's/\(Last_Updated: \).*$/\1Not Installed/' \
72                 -e 's/\(Version: \).*$/\1Not Installed/' \
73                 -e 's/\(Deps: \).*$/\1Not Installed/' ${HOME}/.project
74         awk '/^CONTENTS:/ { print; exit; } {print}' ${HOME}/.project > ${HOME}/.projtmp
75         echo "--------" >> ${HOME}/.projtmp
76         mv ${HOME}/.projtmp ${HOME}/.project
77         # We should be done.
78         echo Package: ${pkg} successfully removed
79         rm -f /tmp/${pkg}.err
80     fi
81 }
82
83 case $1 in
84         now) run ;;
85         -h|--help|--usage|help|usage) usage ;;
86         *) dry_run|less ;;
87 esac
88
89 exit 0