summaryrefslogtreecommitdiff
path: root/support/miktex_update/miktex_update
blob: dfd03d7aecd4ba4903c830efedc9ff9f99ebf468 (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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#!/bin/bash
#
# miktex_update
#
# synchronize the local MiKTeX repository (optional),
# update installed packages and install new packages
# (tested with MiKTeX 2.8) v1.4 (30.10.2009)
# (C) Josef Kleber 2008-2009  License: LPPL
# See http://miktexupdate.josef-kleber.de/index.htm or
#     http://miktexupdate.josef-kleber.de/en_index.htm
# for more information
#
VERSION="v1.4 (30.10.2009)"
REPO=C:/MiKTeXRepRsync
REPOSUBDIR=/packages 
# don't change REPOSUBDIR!!
HTTPURL="http://www.ctan.org/systems/win32/miktex/tm/packages"
HTTPSEARCHURL="http://www.ctan.org/cgi-bin/filenameSearch.py?filename=miktex-zzdb&Search=Search"
MIKTEXFILENAME1="miktex"
MIKTEXFILENAME2="zzdb"
MIKTEXFILENAMEEND=""
MIKTEXVERSION=""
TEMPDIR=""
TEMPENV=""
RSYNCURL1=rsync://tug.ctan.org/CTAN/
# RSYNCURL1=rsync://rsync.tex.ac.uk/CTAN/
# RSYNCURL1=rsync://dante.ctan.org/CTAN/
# see http://www.ctan.org/tex-archive/CTAN.sites for other "rsync" servers
RSYNCURL2=systems/win32/miktex/tm/packages 
RSYNCURL="${RSYNCURL1}${RSYNCURL2}"
RSYNCOPT1="-av --delete"
RSYNCOPT2="--progress --partial"
ODIR=$(pwd)
ODIR2=""
ODIR3=""
UPDATEMODE="default"
ARGREPO="default"
VERBOSE=""
UPDLIST="update.txt"
INSTLIST="install.txt"
ERRORDIR="$HOME"
ERROR="miktex_update_error.txt"
ERRORFILE="${ERRORDIR}/${ERROR}"
ERRORFILESIZE=""
UPDATE="true"
INSTALL="true"
NOLIST="false"
INTERACTIVE="default"
SEARCHONLY="false"
ADMIN=""
typeset -i STEP=0
#
function printHelp()
{
  echo -e \
          "`basename $0` [options] [--local | --online [--repository <URL/DIR Repository>]]\n"\
          " updates your MiKTeX installation and installs new packages\n\n"\
          " Options:\n\n"\
          " -h, --help  will print this help statement\n\n"\
          " -V, --version  will print the version number of `basename $0`\n\n"\
          " -a, --admin  run mpm in administrative mode (MiKTeX v2.8+)\n\n"\
          " -b, --broadband  will optimize the text output in LOCAL mode for broadband\n"\
          "                  download. Support for partial package download will be\n"\
          "                  switched off!\n\n"\
          " -v, --verbose  will lead to a more verbose text output in the process of\n"\
          "                updating and installing new packages\n\n"\
          " -n, --nolist  will not print the list of updatable and/or installable\n"\
          "               packages\n\n"\
          " -I, --interactive  the script will ask, if you want to update and/or install\n"\
          "                    packages (-n.--nolist will be switched off)\n\n"\
          " -s, --searchonly  will only list packages, but not update or install them\n\n"\
          " -u, --update  will only update already installed packages\n\n"\
          " -i, --install  will only install new respectively uninstalled packages\n\n"\
          " -l, --local  will set the update mode to LOCAL and syncronize the local\n"\
          "              repository (default or specified with option -r) with CTAN\n\n"\
          " -o, --online  will set the update mode to ONLINE. You can specify an online\n"\
          "               repository with option -r. Without doing so, a repository\n"\
          "               from the MiKTeX server network will be picked by random\n\n"\
          " -r, --repository  specifies the local or online repository for updating\n"\
          "                   and installing new packages\n\n"\
          " -R, --rsync  specifies the rsync server, if you don't want to use the\n"\
          "              default server (rsync://tug.ctan.org/CTAN/)\n\n"\
          " See http://miktexupdate.josef-kleber.de/index.htm or\n"\
          "     http://miktexupdate.josef-kleber.de/en_index.htm\n"\
          " for more information"
  exit 0;
}
function printVersion()
{
  echo "This is version $VERSION of `basename $0`"
  exit 0;
}
#
if [ $# -eq 0 ]
then
  echo -e "`basename $0`: Could not find command line arguments.\nUse the '--help' option for explanation."
  exit 1
fi
#
LONGOPTS="help,admin,broadband,verbose,version,nolist,local,online,update,install,interactive,searchonly,repository:,rsync:"
SHORTOPTS="habVvnlouiIsr:R:"
ARGS=$(getopt -l "$LONGOPTS" "$SHORTOPTS" "$@" 2>$ERRORFILE)
if [ $? -ne 0 ]
then
  echo -e "`basename $0`: Could not process command line arguments.\nUse the '--help' option for explanation."
  echo -e "ERROR:\n`cat $ERRORFILE`"
  exit 1
fi
#
eval set -- "$ARGS"
#
while [ $# -gt 0 ]; do
  ARG=$1
  case "$ARG" in
      --help|-h)         shift; printHelp;;
      --broadband|-b)    shift; RSYNCOPT2="";;
      --admin|-a)        shift; ADMIN="--admin";;
      --verbose|-v)      shift; VERBOSE="--verbose";;
      --version|-V)      shift; printVersion;;
      --nolist|-n)       shift; NOLIST="true";;
      --local|-l)        shift; UPDATEMODE="LOCAL";;
      --online|-o)       shift; UPDATEMODE="ONLINE";;
      --update|-u)       shift; INSTALL="false";;
      --install|-i)      shift; UPDATE="false";;
      --interactive|-I)  shift; INTERACTIVE="interactive";;
      --searchonly|-s)   shift; SEARCHONLY="true";;
      --repository|-r)   shift; ARGREPO="$1"; shift;;
      --rsync|-R)        shift; RSYNCURL="$1${RSYNCURL2}"; shift;;
      --)                shift; break;;
      *)                 break;;
  esac
done
#
if [ $INTERACTIVE = "interactive" ]
then
  NOLIST="false"
fi
if [ $SEARCHONLY = "true" ]
then
  NOLIST="false"
  INTERACTIVE="default"
fi
#
TEMPENV=$(cygpath -w $TEMP)
TEMPDIR="${TEMPENV}\miktex_update"
echo
echo
echo " MiKTeX Update"
echo "---------------"
echo
if [ $UPDATEMODE = "LOCAL" ]
then
  if [ $ARGREPO != "default" ]
  then
    REPO=${ARGREPO}${REPOSUBDIR}
  else
    REPO=${REPO}${REPOSUBDIR}
  fi
  if [ $SEARCHONLY = "true" ]
  then
    ((STEP++))
    echo "$STEP) Download latest MiKTeX repository information"
    echo
    ODIR2=$(pwd)
    mkdir -p $TEMPDIR 2>>$ERRORFILE
    cd $TEMPDIR
    rm -f *.* 2>>$ERRORFILE
    MIKTEXVERSION=$(mpm --version | head -1 | cut -f4 -d ' ' | cut -c1-3 2>>$ERRORFILE)
    wget -q $HTTPSEARCHURL -O url.htm 2>>$ERRORFILE
    MIKTEXFILENAMEEND=$(cat url.htm | grep $MIKTEXFILENAME1 | grep $MIKTEXVERSION | head -1 | cut -f3-4 -d '.' | cut -f1 -d '<' 2>>$ERRORFILE)
    URL="${HTTPURL}/${MIKTEXFILENAME1}-${MIKTEXFILENAME2}1-${MIKTEXVERSION}.${MIKTEXFILENAMEEND}"
    wget -q $URL 2>>$ERRORFILE
    URL="${HTTPURL}/${MIKTEXFILENAME1}-${MIKTEXFILENAME2}2-${MIKTEXVERSION}.${MIKTEXFILENAMEEND}"
    wget -q $URL 2>>$ERRORFILE
    REPO=$TEMPDIR
    cd $ODIR2
  else
    ((STEP++))
    echo "$STEP) Synchronize local repository with CTAN"
    echo
    if [ ! -d $REPO ]
    then
      mkdir -p $REPO 2>>$ERRORFILE
    fi
    cd $REPO
    cd ..
    if [ -a $ERRORFILE ]
    then
      rm $ERRORFILE 2>>$ERRORFILE
    fi
    RSYNCOPT="$RSYNCOPT1 $RSYNCOPT2"
    rsync $RSYNCOPT $RSYNCURL . 2>>$ERRORFILE
    if [ $? -eq 0 ]
    then
      echo
    fi
  fi
  REPO=$(cygpath -w $REPO)
elif [ $UPDATEMODE = "ONLINE" ]
then
  if [ $ARGREPO = "default" ]
  then
    REPO=$(mpm --pick-repository-url | sed -e "s/\r//")
  else
    REPO=$ARGREPO
  fi
  ((STEP++))
  echo "$STEP) Set online repository: $REPO"
  echo
else
  echo -e "`basename $0`: You did not specify an UPDATE MODE.\nUse the '--help' option for explanation."
  exit 1
fi
mpm --set-repository=$REPO $ADMIN 2>>$ERRORFILE
((STEP++))
echo "$STEP) Update DB"
echo
mpm --update-db $ADMIN 2>>$ERRORFILE;
if [ $UPDATE = "true" ]
then
  mpm --find-updates $ADMIN >$UPDLIST 2>>$ERRORFILE
  if [ $NOLIST = "false" ]
  then
    ((STEP++))
    echo "$STEP) List of updatable packages:"
    echo
    if [ "`cat $UPDLIST | head -1 | cut -c1-5`" = "There" ]
    then
      echo "   There are currently no updates available."
      echo
    else
      cat $UPDLIST
      echo
      if [ $INTERACTIVE = "interactive" ]
      then
        echo -e "Do you want to update these packages (y/n): \c"
        read ANSWER
        if [ $ANSWER = "y" -o $ANSWER = "Y" ]
        then
          UPDATE="true"
        else
          UPDATE="false"
        fi
        echo
      fi
    fi
  fi
  if [ $UPDATE = "true" -a $SEARCHONLY = "false" ]
  then
    ((STEP++))
    echo "$STEP) Update installed packages"
    echo
    if [ "`cat $UPDLIST | head -1 | cut -c1-5`" = "There" ]
    then
      echo "   There are currently no updates available."
      echo
    else    
      mpm --update $ADMIN $VERBOSE 2>>$ERRORFILE
    echo
    fi
  fi
fi
if [ $INSTALL = "true" ]
then
  mpm --list $ADMIN | grep -e '- ' | cut -c22- >$INSTLIST 2>>$ERRORFILE
  if [ $NOLIST = "false" ]
  then
    ((STEP++))
    echo "$STEP) List of installable packages:"
    echo
    if [ -s $INSTLIST ]
    then
      cat $INSTLIST
    echo
    if [ $INTERACTIVE = "interactive" ]
    then
      echo -e "Do you want to install these packages (y/n): \c"
      read ANSWER
      if [ $ANSWER = "y" -o $ANSWER = "Y" ]
      then
        INSTALL="true"
      else
        INSTALL="false"
      fi
      echo
    fi
    else
      echo "   There are currently no new packages available."
      echo
    fi
  fi
  if [ $INSTALL = "true" -a $SEARCHONLY = "false" ]
  then
    ((STEP++))
    echo "$STEP) Install new packages"
    echo
    if [ -s $INSTLIST ]
    then
      mpm --install-some=$INSTLIST $ADMIN $VERBOSE 2>>$ERRORFILE
    echo
    else
      echo "   There are currently no new packages available."
      echo
    fi
  fi
fi
if [ $UPDATE = "true" ]
then
  rm $UPDLIST
fi
if [ $INSTALL = "true" ]
then
  rm $INSTLIST
fi
if [ $UPDATEMODE = "LOCAL" -a $SEARCHONLY = "true" ]
then
  cd $TEMPDIR
  rm -f miktex* 2>>$ERRORFILE
  rm -f url.htm 2>>$ERRORFILE
  cd $ODIR2
  rmdir -p --ignore-fail-on-non-empty $TEMPDIR 2>>$ERRORFILE
fi 
ODIR3=$(pwd)
cd $ERRORDIR
ERRORFILESIZE=$(ls -al | grep $ERROR | tr -s ' ' | cut -f5 -d ' ' 2>>$ERRORFILE)
cd $ODIR3
echo
if [ $ERRORFILESIZE = "0" ]
then
  rm $ERRORFILE 2>>$ERRORFILE
  cd $ODIR
else
  echo "ERROR list:"
  echo
  cat $ERRORFILE
  cd $ODIR
  exit 1
fi
echo
exit 0