summarylogtreecommitdiffstats
path: root/cfgprf.sh
blob: 1a110c16d37c90f7372d9c2b55fc4842eb6faa82 (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#!/bin/bash

POSITIONAL_ARGS=()

while [[ $# -gt 0 ]]; do
  case $1 in
    -h|--help)
      HELP=1
      shift # past argument
      ;;
    -i|--import)
      IMPORT=1
      shift # past argument
      ;;
    -e|--export)
      EXPORT=1
      shift # past argument
      ;;
    -p|--packages)
      PACKAGES=1
      shift # past argument
      ;;
    -c|--configs)
      CONFIGS=1
      shift # past argument
      ;;
    -A|--aur)
      AUR=1
      shift # past argument
      ;;
    -a|--all)
      CONFIGS=1
      PACKAGES=1
      shift # past argument
      ;;
    -y|--yes)
      CONFIRM=1
      shift # past argument
      ;;
    -P|--profile)
      PROFILE="$2"
      shift # past argument
      shift # past value
      ;;
    -*|--*)
      echo "error: unknown option $1"
      exit 1
      ;;
    *)
      POSITIONAL_ARGS+=("$1") # save positional arg
      shift # past argument
      ;;
  esac
done

help() {
    echo
    echo
    echo "  cfgprf"
    echo
    echo "  a tool to easily save packages and configs over multiple machines"
    echo "  really useful for reinstalling, which is exactly why i made it"
    echo
    echo "  primary commands:"
    echo "      -e | --export: export mode"
    echo "          -P | --profile: REQUIRED. path to file to write to, preferrably .cfgprf"
    echo "          -p | --packages: export packages with pacman"
    echo "          -A | --aur: export AUR packages with yay or paru"
    echo "          -c | --configs: export files in ~/.config/"
    echo "          -a | --all: shorthard for --packages and --configs together. not having either will default to this"
    echo "      -i | --import: import mode"
    echo "          -P | --profile: REQUIRED. path to file to import from, usually .cfgprf"
    echo "          -p | --packages: import packages with pacman"
    echo "          -A | --aur: import AUR packages with yay or paru"
    echo "          -c | --configs: import files to ~/.config/"
    echo "          -a | --all: shorthard for --packages, --aur and --configs together. not having either will default to this"
    echo "          -y | --yes: will skip confirmation"
    echo
    echo "  other commands:"
    echo "      -h | --help: show this menu"
    echo
    echo "  example usage:"
    echo "      cfgprf --export --packages --configs --profile ~/myconfig.cfgprf"
    echo
}

set -- "${POSITIONAL_ARGS[@]}" # restore positional parameters

if [ $HELP ]; then
    help
    exit
fi

# setting mode
MODE=NONE

if [ $IMPORT ] && [ $EXPORT ]; then
    MODE=ERROR
elif [ $IMPORT ]; then
    MODE=IMPORT
elif [ $EXPORT ]; then
    MODE=EXPORT
fi

if [ $MODE == "NONE" ]; then
    help
    exit 1
elif [ $MODE == "ERROR" ]; then
    echo "error: both import and export modes selected, that cannot be done. use -h for commands."
    exit 1
fi

# default to --all if neither --packages or --configs
if ! [ $PACKAGES ] && ! [ $CONFIGS ] && ! [ $AUR ]; then
    PACKAGES=1
    AUR=1
    CONFIGS=1
fi

if [ $MODE == "IMPORT" ]; then
# import

    # file stuff
    # checks if file is a valid tar and has the cfgprf file
    if ! [ -f $PROFILE ] || ! tar -tf $PROFILE &> /dev/null || ! tar -tf $PROFILE | grep -q "cfgprf"; then
        echo "error: invalid file"
        exit 1
    fi

    if ! [ $CONFIRM ]; then

        if [ $PACKAGES ] && [ $CONFIGS ]; then
            MESSAGE="are you sure you want to import configs and packages? the configs will override yours, and packages will be installed."
        elif [ $PACKAGES ]; then
            MESSAGE="are you sure you want to import packages? the packages will be installed using pacman."
        elif [ $CONFIGS ]; then
            MESSAGE="are you sure you want to import configs? the configs will override yours, so it is advised to back them up if you don't want to get rid of them."
        fi

        echo
        read -p "${MESSAGE} (Y/n): " CONFIRM

        if [ "${CONFIRM,,}" != "y" ]; then
            echo "aborting"
            echo
            exit
        fi

    fi

    echo
    echo "importing"

    if [ $PACKAGES ]; then

        echo "importing packages"

        PACKAGELIST= tar -xf $PROFILE packages -O

        sudo pacman -S $PACKAGELIST

    fi

    if [ $AUR ]; then

        echo "importing AUR packages"

        PACKAGELIST= tar -xf $PROFILE aur -O

        if command -v yay >/dev/null; then
            yay -S $PACKAGELIST
        elif command -v paru >/dev/null; then
            paru -S $PACKAGELIST
        else
            echo "error: yay nor paru found"
        fi

    fi

    if [ $CONFIGS ]; then

        echo "importing configs"

        TEMP=$(mktemp -d)
        cd $TEMP

        tar -xf $PROFILE &>/dev/null

        rsync -a "config/" "$HOME/.config/" >/dev/null

        rm -r $TEMP

    fi

    echo
    echo "finished importing"
    echo

elif [ $MODE == "EXPORT" ]; then
# export

    echo
    echo "exporting"

    # empty file cause don't wanna append nuh uh
    > $PROFILE
    TEMP=$(mktemp -d)

    cd $TEMP

    if [ $PACKAGES ]; then

        echo "exporting packages"

        pacman -Qe > "packages"

        tar -rf $PROFILE "packages"

    fi

    if [ $AUR ]; then

        echo "exporting AUR packages"

        if command -v yay >/dev/null; then
            yay -Qem > "aur"
            tar -rf $PROFILE "aur"
        elif command -v paru >/dev/null; then
            paru -Qem > "aur"
            tar -rf $PROFILE "aur"
        else
            echo "error: yay nor paru found"
        fi

    fi

    if [ $CONFIGS ]; then

        echo "exporting configs"

        mkdir "config"

        # so many excludes cause some programs like vscode and discord abuse ~/.config/
        EXCLUDE=(
            "--exclude=*.log"
            "--exclude=*.tmp"
            "--exclude=*cache*"
            "--exclude=*Cache*"
            "--exclude=sessionData"
        )

        rsync "${EXCLUDE[@]}" -a "$HOME/.config/" "config/"

        tar -rf $PROFILE "config"

    fi

    touch "cfgprf"
    tar -rf $PROFILE "cfgprf"

    rm -r $TEMP

    echo
    echo "finished exporting"
    echo

fi