/useful/trunk-1

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/useful/trunk-1
23 by Gustav Hartvigsson
[do-offline-updates.sh] Rewrite to be more inline with the rest of the tools.
1
#!/usr/bin/env bash
2
3
####
4
# FILE NAME do-offline-updates.sh
5
#
27 by Gustav Hartvigsson
Added copyright information to all files.
6
# Authors:
7
#    Gustav Hartvigsson 2024
8
#    Distributed under the Cool Licence 1.1
9
#
23 by Gustav Hartvigsson
[do-offline-updates.sh] Rewrite to be more inline with the rest of the tools.
10
####
11
12
__UPDATES_AVAILABLE=false
13
__DO_REBOOT=true
14
__DO_DOWNLOAD=true
15
__REBOOT_COMMAND="systemctl reboot"
16
17
__SANITY=true
18
19
__SCRIPT_ROOT=$(dirname $(readlink -f $0))
20
source $__SCRIPT_ROOT/useful.inc.sh
21
22
function __usage () {
23
  echo "    do-offline-updates.sh"
24
  echo "Perform offline updates, if available."
25
  echo ""
26
  echo "--help           -h"
27
  echo "    Show this help message."
28
  echo ""
29
  echo "--no-reboot"
30
  echo "    Do not perform an reboot."
31
  echo ""
32
  echo "--no-download    --check-updates"
33
  echo "    Do not download updates."
34
  echo "    Will show if updates are available."
35
  echo "    Implies --no-reboot."
36
  echo ""
37
}
38
39
function __sanity_check () {
40
  # Check that we have the tools needed.
41
  __find_tool systemctl
42
  __find_tool pkcon
43
44
  if [[ $__SANITY == false ]]; then
45
    echo ""
46
    echo "Please install the missing tools."
47
    echo ""
48
    exit 1
49
  fi
50
} 
51
52
function __check_for_updates () {
29 by Gustav Hartvigsson
* fixed this... Finally.
53
  __silent pkcon get-updates
23 by Gustav Hartvigsson
[do-offline-updates.sh] Rewrite to be more inline with the rest of the tools.
54
  if [[ $? == 0 ]]; then
55
    echo "Updates are available!"
56
    echo ""
57
    __UPDATES_AVAILABLE=true
58
  else
29 by Gustav Hartvigsson
* fixed this... Finally.
59
    echo "No updates available."
30 by Gustav Hartvigsson
* Fixed useful.inc.sh
60
    __UPDATES_AVAILABLE=false
23 by Gustav Hartvigsson
[do-offline-updates.sh] Rewrite to be more inline with the rest of the tools.
61
  fi
62
}
63
64
function __download_updates () {
28 by Gustav Hartvigsson
Misspelled: pkcon.
65
  pkcon update --download-only
23 by Gustav Hartvigsson
[do-offline-updates.sh] Rewrite to be more inline with the rest of the tools.
66
  if [[ $? == 0 ]];then
67
    __UPDATES_AVAILABLE=true
68
  else
30 by Gustav Hartvigsson
* Fixed useful.inc.sh
69
    ## huh?
23 by Gustav Hartvigsson
[do-offline-updates.sh] Rewrite to be more inline with the rest of the tools.
70
    __UPDATES_AVAILABLE=false
71
  fi
72
73
}
74
75
function __reboot () {
30 by Gustav Hartvigsson
* Fixed useful.inc.sh
76
  echo "reboot"
25 by Gustav Hartvigsson
[do-offline-updates.sh] Woops.
77
  eval $__REBOOT_COMMAND
23 by Gustav Hartvigsson
[do-offline-updates.sh] Rewrite to be more inline with the rest of the tools.
78
}
79
80
function __parse_args () {
30 by Gustav Hartvigsson
* Fixed useful.inc.sh
81
23 by Gustav Hartvigsson
[do-offline-updates.sh] Rewrite to be more inline with the rest of the tools.
82
  while [[ $# -gt 0 ]]
83
  do
84
    case $1 in
85
      -h|--help)
86
         __usage
87
         exit
88
         shift
89
      ;;
90
      --no-reboot)
91
        __DO_REBOOT=false
92
        shift
93
        ;;
94
      --no-download|--check-updates)
95
        __DO_REBOOT=false
96
        __DO_DOWNLOAD=false
97
        shift
98
        ;;
99
      *)
100
        echo "Unkown argument \"${1}\"."
101
        exit 1
102
        shift
103
      ;;
104
      --)
105
        shift
106
        break
107
      ;;
108
    esac
109
  done
110
}
111
112
function __main () {
29 by Gustav Hartvigsson
* fixed this... Finally.
113
  __sanity_check
23 by Gustav Hartvigsson
[do-offline-updates.sh] Rewrite to be more inline with the rest of the tools.
114
  __parse_args $@
29 by Gustav Hartvigsson
* fixed this... Finally.
115
  __check_for_updates
23 by Gustav Hartvigsson
[do-offline-updates.sh] Rewrite to be more inline with the rest of the tools.
116
30 by Gustav Hartvigsson
* Fixed useful.inc.sh
117
  if [[ ( "$__DO_DOWNLOAD" == true ) && ( "$__UPDATES_AVAILABLE" == true ) ]]; then
31 by Gustav Hartvigsson
* That's awkard...
118
    __download_updates
23 by Gustav Hartvigsson
[do-offline-updates.sh] Rewrite to be more inline with the rest of the tools.
119
  fi
120
30 by Gustav Hartvigsson
* Fixed useful.inc.sh
121
  if [[ ( "$__DO_REBOOT" == true ) && ( "$__UPDATES_AVAILABLE" == true ) ]]; then
31 by Gustav Hartvigsson
* That's awkard...
122
    __reboot
23 by Gustav Hartvigsson
[do-offline-updates.sh] Rewrite to be more inline with the rest of the tools.
123
  fi
124
}
125
126
127
__main $@