aboutsummaryrefslogtreecommitdiff
path: root/.local/bin/parucache-clean-up
blob: add81cb4f0b5e767ed6481b4541c13973dceed3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/bin/sh

script_name=$(basename "$0")

show_help() {
    cat <<EOF
usage: $script_name [OPTIONS]

clean up pacman and paru cache.

OPTIONS:
  --only-pacman    clean only pacman cache
  --only-aur       clean only paru cache
  -h, --help       display this help message and exit

EXAMPLES:
  $script_name                    # clean both pacman and paru cache
  $script_name --only-pacman      # clean only pacman cache
  $script_name --only-aur         # clean only paru cache

EOF
}

handle_interrupt() {
    printf "\n%s: operation canceled by user\n" "$script_name" >&2
    exit 130
}

trap handle_interrupt INT

error_exit() {
    printf "%s: error: %s\n" "$script_name" "$1" >&2
    exit 1
}

check_privilege() {
    if [ "$(id -u)" -eq 0 ]; then
        return 0
    fi
    if ! command -v sudo >/dev/null 2>&1; then
        error_exit "sudo is required but not found"
    fi
}

# run command with elevated privileges only if needed
run_privileged() {
    if [ "$(id -u)" -eq 0 ]; then
        "$@"
    else
        sudo "$@"
    fi
}

# clean pacman cache
clean_pacman_cache() {
    printf "cleaning pacman cache...\n"
    run_privileged paccache --remove --keep 0 || error_exit "failed to clean pacman cache"
}

# clean paru cache
clean_aur_cache() {
    cache_dir="$HOME/.cache/paru/clone"
    
    printf "cleaning paru cache...\n"
    if [ -d "$cache_dir" ]; then
        rm -rf "$cache_dir" || error_exit "failed to clean paru cache"
    else
        printf "warning: paru cache directory not found: %s\n" "$cache_dir"
    fi

    # not regenerating paru database anymore since the aur has bot-detection now
    ## printf "regenerating paru database...\n"
    ## paru --gendb || error_exit "failed to regenerate paru database"

    echo ''
    paru -Ps | grep --color=never '::'
    echo ''
}

# default values
clean_pacman=1
clean_aur=1

# parse args
while [ $# -gt 0 ]; do
    case "$1" in
        -h|--help)
            show_help
            exit 0
            ;;
        --only-pacman)
            clean_aur=0
            ;;
        --only-aur)
            clean_pacman=0
            ;;
        *)
            printf "%s: error: unknown option: %s\n" "$script_name" "$1" >&2
            printf "try '%s --help' for more information\n" "$script_name" >&2
            exit 1
            ;;
    esac
    shift
done

# checks
if [ "$clean_pacman" -eq 0 ] && [ "$clean_aur" -eq 0 ]; then
    error_exit "cannot use --only-pacman and --only-aur together"
fi

if [ "$clean_pacman" -eq 1 ]; then
    command -v paccache >/dev/null 2>&1 || error_exit "paccache is required but not found (install pacman-contrib)"
    check_privilege
fi

if [ "$clean_aur" -eq 1 ]; then
    command -v paru >/dev/null 2>&1 || error_exit "paru is required but not found"
fi

# clean relevant caches 
[ "$clean_pacman" -eq 1 ] && clean_pacman_cache
[ "$clean_aur" -eq 1 ] && clean_aur_cache

printf "cleanup complete!\n"