/useful/trunk-1

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/useful/trunk-1
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
#!/usr/bin/env bash


####
# FILE NAME do-offline-updates.sh
#
####

__UPDATES_AVAILABLE=false
__DO_REBOOT=true
__DO_DOWNLOAD=true
__REBOOT_COMMAND="systemctl reboot"

__SANITY=true

__SCRIPT_ROOT=$(dirname $(readlink -f $0))
source $__SCRIPT_ROOT/useful.inc.sh

function __usage () {
  echo "    do-offline-updates.sh"
  echo "Perform offline updates, if available."
  echo ""
  echo "--help           -h"
  echo "    Show this help message."
  echo ""
  echo "--no-reboot"
  echo "    Do not perform an reboot."
  echo ""
  echo "--no-download    --check-updates"
  echo "    Do not download updates."
  echo "    Will show if updates are available."
  echo "    Implies --no-reboot."
  echo ""
}

function __sanity_check () {
  # Check that we have the tools needed.
  __find_tool systemctl
  __find_tool pkcon

  if [[ $__SANITY == false ]]; then
    echo ""
    echo "Please install the missing tools."
    echo ""
    exit 1
  fi
} 

function __check_for_updates () {
  __silentpkcon get-updates
  if [[ $? == 0 ]]; then
    echo "Updates are available!"
    echo ""
    __UPDATES_AVAILABLE=true
  else
    __UPDATES_AVAILABLE=false
  fi
}

function __download_updates () {
  pkgcon update --download-only
  if [[ $? == 0 ]];then
    __UPDATES_AVAILABLE=true
  else
    ## huh?
    __UPDATES_AVAILABLE=false
  fi

}

function __reboot () {
  eval __REBOOT_COMMAND
}

function __parse_args () {

  if [[ -z "$1" ]]
  then
    echo "Try --help or -h."
    exit 1
  fi
  
  while [[ $# -gt 0 ]]
  do
    case $1 in
      -h|--help)
         __usage
         exit
         shift
      ;;
      --no-reboot)
        __DO_REBOOT=false
        shift
        ;;
      --no-download|--check-updates)
        __DO_REBOOT=false
        __DO_DOWNLOAD=false
        shift
        ;;
      *)
        echo "Unkown argument \"${1}\"."
        exit 1
        shift
      ;;
      --)
        shift
        break
      ;;
    esac
  done
}

function __main () {
  __satity_check
  __parse_args $@

  if [[ ( __DO_DOWNLOAD == true ) && ( __UPDATE_AVAILABLE == true ) ]]; then
    __do_download
  fi

  if [[ ( __DO_REBOOT == true ) && ( __UPDATE_AVAILABLE == true ) ]]; then
    __do_reboot
  fi
}


__main $@