summaryrefslogtreecommitdiff
path: root/setup_native/scripts/update.sh
blob: 1b3785b3894c916a19c1f6eb193b5517cd7f2b2f (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
#!/bin/sh

GNOMEXCMDS="/usr/bin/gksu"
GNOMECMDS="/usr/bin/gnome-terminal /opt/gnome/bin/gnome-terminal"
KDEXCMDS="/usr/bin/kdesu /opt/kde/bin/kdesu /opt/kde?/bin/kdesu"
KDECMDS="/usr/bin/konsole /opt/kde/bin/konsole /opt/kde?/bin/konsole"
MISCCMDS="/usr/bin/x-terminal-emulator /usr/X11/bin/xterm /usr/openwin/bin/xterm"

TITLE='${PRODUCTNAME} update'

#
# Usage
#

Usage () {
  if [ "`basename $0`" = "update" ]; then
    echo "Usage: `basename $0` [<path to ${PRODUCTNAME} installation>]"
  fi
  echo
  exit 2
}

make_tempfile() {

  # Always prefer mktemp when available
  if [ -x "$MKTEMP" ]; then
    "$MKTEMP" "/tmp/$1.XXXXXX"
  else
    TMPCMD="/tmp/$1.$$"
    touch $TMPCMD
    echo $TMPCMD
  fi
  
}

# 
# make_command_file - generate a command file to pass to xterm & co
#
run_in_terminal () {

  TMPCMD=`make_tempfile 'OpenOffice.org-Online-Update'`
  
  cat >> $TMPCMD 

  cat >> $TMPCMD << \EOF
if [ $? -eq 0 -a ! -z "$DISPLAY" ]; then
  echo
  echo "Press <Enter> to close this window"
  read DUMMY
fi
EOF

  cat >> $TMPCMD << EOF
rm -f $TMPCMD
EOF

  chmod a+x $TMPCMD
  exec "$@" -e $TMPCMD
}

#
# run the command line passed as positional parameters either via 
# gksu/kdesu or via su command in a terminal emulator.
#
elevate() {

  TITLEOPT=""
  GEOMOPT=""
  case `basename $XTERM` in
    "xterm")
      TITLEOPT="-title"
      GEOMOPT="-geometry "
      ;;
    "gnome-terminal" | "konsole" | "x-terminal-emulator")
      TITLEOPT="--title"
      GEOMOPT="--geometry="
      ;;
  esac

  case `basename $SU` in
    "kdesu" )
      # --caption
      SUOPT="-t"
      ;;
    "gksu" )
      # --title
      SUOPT=""
      ;;
    "su" )
      SUOPT="- root -c"
      ;;
  esac

  ( echo "echo The application \"$@\" will update your ${PRODUCTNAME} installation."
    echo "echo"
    echo "$SU" "$SUOPT" "$@"
  ) | run_in_terminal "$XTERM" "$TITLEOPT" "$TITLE" ${GEOMOPT}+300+300
}


update_pkg() {
  ADMINFILE=`make_tempfile 'LibreOffice-Online-Update-admin'`
  
cat >> $ADMINFILE << EOF
action=nocheck
conflict=nocheck
setuid=nocheck
idepend=nocheck
rdepend=nocheck
mail=
EOF
  
  PKGLIST=""
  for i in `cd $1; ls -d *`; do
    pkginfo -q $i && PKGLIST="$PKGLIST $i"
  done
  
  pkgrm -n -a $ADMINFILE $PKGLIST
  pkgadd -n -a $ADMINFILE -d $1 $PKGLIST
  
  rm -f $ADMINFILE
}

#
# main
#

CMD="`basename $0`"
BASEDIR="`dirname $0`"
ABSBASEDIR="`cd $BASEDIR; pwd`"

if [ -z "$DISPLAY" ]; then
  SU="su"
  XTERM=""
else
  # define search order depending on the desktop in use ..
  if [ "`uname -s`" = "SunOS" ]; then
    XPROP=/usr/openwin/bin/xprop
    GREP=/usr/xpg4/bin/grep
    MKTEMP=/usr/bin/mktemp
  else
    if [ -x /usr/X11/bin/xprop ]; then
      XPROP=/usr/X11/bin/xprop
    else
      XPROP=/usr/bin/xprop
    fi
    GREP=grep
    MKTEMP=/bin/mktemp
  fi

  # use desktop specific tools where possible, but prefer X11 su over terminal emulators
  if $XPROP -root | $GREP -q '^KWIN_RUNNING'; then
    SULIST="$KDEXCMDS $GNOMEXCMDS"
    XTERMLIST="$KDECMDS $MISCCMDS"
  else
    SULIST="$GNOMEXCMDS $KDEXCMDS"
    XTERMLIST="$GNOMECMDS $MISCCMDS"
  fi

  # search for the first available terminal emulator
  for i in $XTERMLIST; do
    if [ -x $i ]; then
      XTERM=$i
      break
    fi
  done

  # we prefer gksu or kdesu if available
  SU="su"
  for i in $SULIST; do
    if [ -x $i ]; then
      SU=$i
      break
    fi
  done
fi

if [ "$CMD" = "update" ]; then
  if [ $# -gt 0 ]; then
    [ -d $1 -a $# -eq 1 ] || Usage
    elevate "$ABSBASEDIR/$CMD"
  else
    if [ -d "$BASEDIR/RPMS" ]; then
      # do not use --hash as the control sequence to edit the current line does not work
      # in our setup (at least not with kdesu)
      rpm -v --freshen `find "$BASEDIR"/RPMS -name '*.rpm'`
    elif [ -d "$BASEDIR/DEBS" ]; then
      dpkg --install --selected-only --recursive "$BASEDIR"/DEBS
    elif [ -d "$BASEDIR/packages" ]; then
      update_pkg "$BASEDIR/packages"
    fi
  fi
else
  Usage
fi