added usbreset.lsp
[rrq/newlisp-ftw.git] / un-merge-usr.lsp
1 #!/usr/bin/newlisp
2 #
3 # This script undo the "merged usr" directories; i.e., replace any
4 # toplevel links /bin, /sbin, /lib, /lib32, /libx32, or /lib64 with
5 # proper directories of their content according to installation paths.
6 #
7
8
9 (signal 2 (fn (x) (exit 1)))
10
11 (define (replace-file A B)
12   (delete-file A)
13   (rename-file B A))
14
15 (define (installs-as PATH)
16   (= (! (format "/usr/bin/dpkg -S \"%s\" > /dev/null 2>&1" PATH))))
17
18 (define (delete-file-exist PATH)
19   (and (file? PATH) (delete-file PATH)))
20
21 (setf UNSURE "/var/log/UNSURE" TELL true)
22 (define (unsure PATH)
23   (when TELL
24     (println (format "Adding paths to %s with unsure pathnames" UNSURE))
25     (setf TELL nil)
26     )
27   (append-file UNSURE (string PATH "\n")))
28
29 (define (delete-empty-dirs D)
30   (! (format "/usr/bin/find %s /usr%s -depth -type d -delete 2>/dev/null" D D))
31   )
32
33 #### Un-merging process starts here
34 (setf DIRS '("/bin" "/sbin" "/lib" "/lib32" "/libx32" "/lib64")
35       CP (if (file? "/bin/cp") "/bin/cp" "/usr/bin/cp") )
36
37 (println (date))
38
39 # Make target directories as copies of their /usr linking
40 (dolist (D DIRS)
41   (when (and (= 41471 (file-info D 1)) (directory? D))
42     (println "copying " D)
43     (let ((Dx (format "%s-tobe" D)))
44       (and (make-dir Dx)
45            (= (! (format "%s -a /usr%s/. %s/." CP D Dx)))
46            (replace-file D Dx)))))
47
48 # Rebuild library cache
49 (= (! "/sbin/ldconfig"))
50
51 # Remove counterpart of known installed file(s), then remove any empty
52 # directories
53 (dolist (D DIRS)
54   (when (directory? (string "/usr" D))
55     (let ((N 0) (FILES (exec (format "/usr/bin/find /usr%s -type f" D))))
56       (println "checking " (length FILES) " files for " D)
57       (dolist (F FILES)
58         (when (= (% (inc N) 100))
59           (print (format "checked %d of %d files for %s\r"
60                          N (length FILES) D))
61           (setf TELL true))
62         (if (installs-as F) (delete-file-exist (4 F))
63           (installs-as (4 F)) (delete-file F)
64           (unsure F)))
65       (delete-empty-dirs D)
66       )
67     (println)
68     ))
69
70 # Rebuild library cache
71 (= (! "/sbin/ldconfig"))
72
73 # Ensure the root directories still/again exist 
74 (dolist (D DIRS)
75   (! (format "/bin/mkdir -p %s /usr%s" D D)))
76
77 (print "Push enter to reboot: ")
78 (read-line)
79 (! "/usr/sbin/reboot")
80 (exit 0)