summaryrefslogtreecommitdiffstats
path: root/nix-script
blob: 5ff5fb41aec67e99e695c099744e7d90bb17adc3 (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
#!/usr/bin/env bash

usage() {
    cat <<EOS >&2
    $(help_synopsis "${BASE_SOURCE[0]}" "[options] <command> <commandoptions>")

    --conf=<path>           Path for alternative nix-script.rc file. Default: ~/.nix-script.rc
    -l | --list-commands    List all available commands
    -v                      Be verbose
    -d                      Debugging output (enables -v)
    -h                      Show this help and exit

$(help_rcvars                                                       \
"RC_CONFIG  - Path of your system configuration (git) directory"    \
"RC_NIXPKGS - Path of your nixpkgs clone"
)

$(help_end)
EOS
}

RC=~/.nix-script.rc
LIST_COMMANDS=0
VERBOSE=0

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

SHIFT_ARGS=0

#
# Increase the SHIFT_ARGS variable
#
shift_one_more() {
    SHIFT_ARGS=$(( SHIFT_ARGS + 1 ))
}

#
# Shift N times the arguments, so:
#
#  shift_n 5 a b c d e f
#
# will print "f"
#
shift_n() {
    for n in `seq 0 $1`; do shift; done
    echo $*
}

#
# Parse the arguments for this script
#
for cmd
do
    case $cmd in
    --conf=*)
        RC=$(echo $cmd | sed 's,^--conf\=,,')
        dbg "RC = $RC"
        [[ ! -e $RC ]] && stderr "RC file '$RC' does not exist" && exit 1
        shift_one_more
        ;;

    "--list-commands" )
        LIST_COMMANDS=1
        shift_one_more
        ;;

    "-l" )
        LIST_COMMANDS=1
        shift_one_more
        ;;

    "-v" )
        export VERBOSE=1
        stdout "Verbose now"
        shift_one_more
        ;;

    "-d")
        export VERBOSE=1
        export DEBUG=1
        stdout "Debugging enabled. Implicitely turned on verbosity"
        shift_one_more
        ;;

    "-h" )
        usage
        exit 1
        ;;

    * )
        if [ ! -n $(script_for $cmd) ]
        then
            stderr "Unknown flag / command '$cmd'"
            exit 1
        else
            if [ -z "$COMMAND" ]
            then
                stdout "Found command: '$cmd'"
                COMMAND=$cmd
                shift_one_more
            fi
            break
        fi
    esac
done

if [[ ! -f "$RC" ]]
then
    dbg "No configuration file, setting up (empty) default values"

    RC_CONFIG=""
    RC_NIXPKGS=""
    RC_SWITCH_DEFAULT_TAG_FLAGS=""
    RC_SWITCH_DEFAULT_TAG_FLAGS_NIXPKGS=""
else
    dbg "Configuration file found. Sourcing: '$RC'"
    source $RC
fi

dbg "RC_CONFIG = '$RC_CONFIG'"
dbg "RC_NIXPKGS = '$RC_NIXPKGS'"

if [ $LIST_COMMANDS -eq 1 ]
then
    dbg "Listing commands"
    caller_util_list_subcommands_for "nix-script"
    exit 0
fi

[ -z "$COMMAND" ] && stderr "No command given" && exit 1

dbg "Searching for script for '$COMMAND'"
SCRIPT=$(script_for $COMMAND)
dbg "Script is: $SCRIPT"

#
# Error checking whether the script is available and executable.
#
[ ! -f $SCRIPT ]   && stderr "Not available: $COMMAND -> $SCRIPT" && exit 1
[[ ! -x $SCRIPT ]] && stderr "Not executeable: $SCRIPT"           && exit 1

dbg "Parsing args for '$COMMAND'"
SCRIPT_ARGS=$(shift_n $SHIFT_ARGS $*)

#
# execute the script with its arguments
#
stdout "Calling: '$SCRIPT $SCRIPT_ARGS'"
RC=$RC RC_CONFIG=$RC_CONFIG RC_NIXPKGS=$RC_NIXPKGS exec bash $SCRIPT $SCRIPT_ARGS