summaryrefslogtreecommitdiffstats
path: root/nix-script-ls-profiles.sh
blob: 61f0d7d5090f807378435ec52f0dafb6ce734214 (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
#!/usr/bin/env bash

source $(dirname ${BASH_SOURCE[0]})/nix-utils.sh

profiledir=/nix/var/nix/profiles

usage() {
    cat <<EOS >&2
    $(help_synopsis "${BASH_SOURCE[0]}" "[--system | -s] [--user | -u] [-n] [-h]")

    --system | -s           | Show system profiles
    --user | -u             | Show user profiles
    --system-profiles | -p  | Show other system profiles
    -n                      | Show only profile numbers
    -h                      | Show this help and exit

    Show the generations which are currently available for either the
    current user (-u | --user) or the current system (-s | --system)
    profile.

    Per default, this shows the link name, if you pass -n it only shows
    the numbers.

    Example usage:

        # To show the profile generations for the system generation, but
        # only the numbers.
        # With verbosity on.
        nix-script -v ls-profiles -s -n

$(help_end "${BASH_SOURCE[0]}")
EOS
}

SYSTEM=0
USER=0
SYSPROF=0
NUMBERS=0

for arg
do

    case $arg in
        "--system" )
            SYSTEM=1
            ;;

        "-s" )
            SYSTEM=1
            ;;

        "-u" )
            USER=1
            ;;

        "--user" )
            USER=1
            USER_NAMES=$USER
            ;;

        "--system-profiles" )
            SYSPROF=1
            ;;

        "-p" )
            SYSPROF=1
            ;;

        "-n" )
            NUMBERS=1
            ;;

        "-h" )
            usage
            exit 0
            ;;

        "*")
            ;;
    esac
done

(( $SYSTEM == 0 && $USER == 0 && $SYSPROF == 0 )) && usage && exit 1

numberfilter() {
    pref="$1"
    if (( $NUMBERS == 0 ))
    then
        cat
    else
        if (( ($SYSTEM != 0 && $USER != 0) ||
              ($SYSTEM != 0 && $SYSPROF != 0) ||
              ($USER != 0 && $SYSPROF != 0) ))
        then
            cut -d - -f 2 | sort -n | sed -r "s:^(.*):$pref\1:"
        else
            cut -d - -f 2 | sort -n
        fi
    fi
}

list() {
    ls "$profiledir/$1" | grep -E "$2" | numberfilter "$3"
}

(( $SYSTEM == 1 )) && list "" "^system-.*-link" "system/"

if (( $USER == 1 ))
then
    for u in $USER_NAMES;
    do
        list "per-user/$u" "^profile-.*-link" "user/$u/"
    done
fi

if (( $SYSPROF == 1 ))
then
    for entry in $(ls "$profiledir/system-profiles" | grep -v ".*-.*-link")
    do
        list "system-profiles/" "^$entry-.*-link" "system/$entry/"
    done
fi