| 
									
										
										
										
											2021-03-30 01:09:01 +02:00
										 |  |  | #!/bin/bash
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | set -euo pipefail | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Try removing as many empty directories as possible with a single | 
					
						
							|  |  |  | # `rmdir` call to avoid or at least speed up the loop below. | 
					
						
							| 
									
										
										
										
											2021-09-13 20:32:20 +08:00
										 |  |  | if /bin/rmdir -- "${@}" &>/dev/null | 
					
						
							|  |  |  | then | 
					
						
							| 
									
										
										
										
											2021-03-30 01:09:01 +02:00
										 |  |  |   exit | 
					
						
							|  |  |  | fi | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-13 20:32:20 +08:00
										 |  |  | for path in "${@}" | 
					
						
							|  |  |  | do | 
					
						
							| 
									
										
										
										
											2021-03-30 01:09:01 +02:00
										 |  |  |   symlink=true | 
					
						
							|  |  |  |   [[ -L "${path}" ]] || symlink=false | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   directory=false | 
					
						
							| 
									
										
										
										
											2021-09-13 20:32:20 +08:00
										 |  |  |   if [[ -d "${path}" ]] | 
					
						
							|  |  |  |   then | 
					
						
							| 
									
										
										
										
											2021-03-30 01:09:01 +02:00
										 |  |  |     directory=true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-13 20:32:20 +08:00
										 |  |  |     if [[ -e "${path}/.DS_Store" ]] | 
					
						
							|  |  |  |     then | 
					
						
							| 
									
										
										
										
											2021-03-30 01:09:01 +02:00
										 |  |  |       /bin/rm -- "${path}/.DS_Store" | 
					
						
							|  |  |  |     fi | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Some packages leave broken symlinks around; we clean them out before | 
					
						
							|  |  |  |     # attempting to `rmdir` to prevent extra cruft from accumulating. | 
					
						
							|  |  |  |     /usr/bin/find -f "${path}" -- -mindepth 1 -maxdepth 1 -type l ! -exec /bin/test -e {} \; -delete | 
					
						
							| 
									
										
										
										
											2021-09-13 20:32:20 +08:00
										 |  |  |   elif ! ${symlink} && [[ ! -e "${path}" ]] | 
					
						
							|  |  |  |   then | 
					
						
							| 
									
										
										
										
											2021-03-30 01:09:01 +02:00
										 |  |  |     # Skip paths that don't exists and aren't a broken symlink. | 
					
						
							|  |  |  |     continue | 
					
						
							|  |  |  |   fi | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-13 20:32:20 +08:00
										 |  |  |   if ${symlink} | 
					
						
							|  |  |  |   then | 
					
						
							| 
									
										
										
										
											2021-03-30 01:09:01 +02:00
										 |  |  |     # Delete directory symlink. | 
					
						
							|  |  |  |     /bin/rm -- "${path}" | 
					
						
							| 
									
										
										
										
											2021-09-13 20:32:20 +08:00
										 |  |  |   elif ${directory} | 
					
						
							|  |  |  |   then | 
					
						
							| 
									
										
										
										
											2021-03-30 01:09:01 +02:00
										 |  |  |     # Delete directory if empty. | 
					
						
							|  |  |  |     /usr/bin/find -f "${path}" -- -maxdepth 0 -type d -empty -exec /bin/rmdir -- {} \; | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     # Try `rmdir` anyways to show a proper error. | 
					
						
							|  |  |  |     /bin/rmdir -- "${path}" | 
					
						
							|  |  |  |   fi | 
					
						
							|  |  |  | done |