1284990Scy#! /bin/sh
2284990Scy# Wrapper for Microsoft lib.exe
3284990Scy
4284990Scyme=ar-lib
5284990Scyscriptversion=2012-03-01.08; # UTC
6284990Scy
7284990Scy# Copyright (C) 2010-2014 Free Software Foundation, Inc.
8284990Scy# Written by Peter Rosin <peda@lysator.liu.se>.
9284990Scy#
10284990Scy# This program is free software; you can redistribute it and/or modify
11284990Scy# it under the terms of the GNU General Public License as published by
12284990Scy# the Free Software Foundation; either version 2, or (at your option)
13284990Scy# any later version.
14284990Scy#
15284990Scy# This program is distributed in the hope that it will be useful,
16284990Scy# but WITHOUT ANY WARRANTY; without even the implied warranty of
17284990Scy# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18284990Scy# GNU General Public License for more details.
19284990Scy#
20284990Scy# You should have received a copy of the GNU General Public License
21284990Scy# along with this program.  If not, see <http://www.gnu.org/licenses/>.
22284990Scy
23284990Scy# As a special exception to the GNU General Public License, if you
24284990Scy# distribute this file as part of a program that contains a
25284990Scy# configuration script generated by Autoconf, you may include it under
26284990Scy# the same distribution terms that you use for the rest of that program.
27284990Scy
28284990Scy# This file is maintained in Automake, please report
29284990Scy# bugs to <bug-automake@gnu.org> or send patches to
30284990Scy# <automake-patches@gnu.org>.
31284990Scy
32284990Scy
33284990Scy# func_error message
34284990Scyfunc_error ()
35284990Scy{
36284990Scy  echo "$me: $1" 1>&2
37284990Scy  exit 1
38284990Scy}
39284990Scy
40284990Scyfile_conv=
41284990Scy
42284990Scy# func_file_conv build_file
43284990Scy# Convert a $build file to $host form and store it in $file
44284990Scy# Currently only supports Windows hosts.
45284990Scyfunc_file_conv ()
46284990Scy{
47284990Scy  file=$1
48284990Scy  case $file in
49284990Scy    / | /[!/]*) # absolute file, and not a UNC file
50284990Scy      if test -z "$file_conv"; then
51284990Scy	# lazily determine how to convert abs files
52284990Scy	case `uname -s` in
53284990Scy	  MINGW*)
54284990Scy	    file_conv=mingw
55284990Scy	    ;;
56284990Scy	  CYGWIN*)
57284990Scy	    file_conv=cygwin
58284990Scy	    ;;
59284990Scy	  *)
60284990Scy	    file_conv=wine
61284990Scy	    ;;
62284990Scy	esac
63284990Scy      fi
64284990Scy      case $file_conv in
65284990Scy	mingw)
66284990Scy	  file=`cmd //C echo "$file " | sed -e 's/"\(.*\) " *$/\1/'`
67284990Scy	  ;;
68284990Scy	cygwin)
69284990Scy	  file=`cygpath -m "$file" || echo "$file"`
70284990Scy	  ;;
71284990Scy	wine)
72284990Scy	  file=`winepath -w "$file" || echo "$file"`
73284990Scy	  ;;
74284990Scy      esac
75284990Scy      ;;
76284990Scy  esac
77284990Scy}
78284990Scy
79284990Scy# func_at_file at_file operation archive
80284990Scy# Iterate over all members in AT_FILE performing OPERATION on ARCHIVE
81284990Scy# for each of them.
82284990Scy# When interpreting the content of the @FILE, do NOT use func_file_conv,
83284990Scy# since the user would need to supply preconverted file names to
84284990Scy# binutils ar, at least for MinGW.
85284990Scyfunc_at_file ()
86284990Scy{
87284990Scy  operation=$2
88284990Scy  archive=$3
89284990Scy  at_file_contents=`cat "$1"`
90284990Scy  eval set x "$at_file_contents"
91284990Scy  shift
92284990Scy
93284990Scy  for member
94284990Scy  do
95284990Scy    $AR -NOLOGO $operation:"$member" "$archive" || exit $?
96284990Scy  done
97284990Scy}
98284990Scy
99284990Scycase $1 in
100284990Scy  '')
101284990Scy     func_error "no command.  Try '$0 --help' for more information."
102284990Scy     ;;
103284990Scy  -h | --h*)
104284990Scy    cat <<EOF
105284990ScyUsage: $me [--help] [--version] PROGRAM ACTION ARCHIVE [MEMBER...]
106284990Scy
107284990ScyMembers may be specified in a file named with @FILE.
108284990ScyEOF
109284990Scy    exit $?
110284990Scy    ;;
111284990Scy  -v | --v*)
112284990Scy    echo "$me, version $scriptversion"
113284990Scy    exit $?
114284990Scy    ;;
115284990Scyesac
116284990Scy
117284990Scyif test $# -lt 3; then
118284990Scy  func_error "you must specify a program, an action and an archive"
119284990Scyfi
120284990Scy
121284990ScyAR=$1
122284990Scyshift
123284990Scywhile :
124284990Scydo
125284990Scy  if test $# -lt 2; then
126284990Scy    func_error "you must specify a program, an action and an archive"
127284990Scy  fi
128284990Scy  case $1 in
129284990Scy    -lib | -LIB \
130284990Scy    | -ltcg | -LTCG \
131284990Scy    | -machine* | -MACHINE* \
132284990Scy    | -subsystem* | -SUBSYSTEM* \
133284990Scy    | -verbose | -VERBOSE \
134284990Scy    | -wx* | -WX* )
135284990Scy      AR="$AR $1"
136284990Scy      shift
137284990Scy      ;;
138284990Scy    *)
139284990Scy      action=$1
140284990Scy      shift
141284990Scy      break
142284990Scy      ;;
143284990Scy  esac
144284990Scydone
145284990Scyorig_archive=$1
146284990Scyshift
147284990Scyfunc_file_conv "$orig_archive"
148284990Scyarchive=$file
149284990Scy
150284990Scy# strip leading dash in $action
151284990Scyaction=${action#-}
152284990Scy
153284990Scydelete=
154284990Scyextract=
155284990Scylist=
156284990Scyquick=
157284990Scyreplace=
158284990Scyindex=
159284990Scycreate=
160284990Scy
161284990Scywhile test -n "$action"
162284990Scydo
163284990Scy  case $action in
164284990Scy    d*) delete=yes  ;;
165284990Scy    x*) extract=yes ;;
166284990Scy    t*) list=yes    ;;
167284990Scy    q*) quick=yes   ;;
168284990Scy    r*) replace=yes ;;
169284990Scy    s*) index=yes   ;;
170284990Scy    S*)             ;; # the index is always updated implicitly
171284990Scy    c*) create=yes  ;;
172284990Scy    u*)             ;; # TODO: don't ignore the update modifier
173284990Scy    v*)             ;; # TODO: don't ignore the verbose modifier
174284990Scy    *)
175284990Scy      func_error "unknown action specified"
176284990Scy      ;;
177284990Scy  esac
178284990Scy  action=${action#?}
179284990Scydone
180284990Scy
181284990Scycase $delete$extract$list$quick$replace,$index in
182284990Scy  yes,* | ,yes)
183284990Scy    ;;
184284990Scy  yesyes*)
185284990Scy    func_error "more than one action specified"
186284990Scy    ;;
187284990Scy  *)
188284990Scy    func_error "no action specified"
189284990Scy    ;;
190284990Scyesac
191284990Scy
192284990Scyif test -n "$delete"; then
193284990Scy  if test ! -f "$orig_archive"; then
194284990Scy    func_error "archive not found"
195284990Scy  fi
196284990Scy  for member
197284990Scy  do
198284990Scy    case $1 in
199284990Scy      @*)
200284990Scy        func_at_file "${1#@}" -REMOVE "$archive"
201284990Scy        ;;
202284990Scy      *)
203284990Scy        func_file_conv "$1"
204284990Scy        $AR -NOLOGO -REMOVE:"$file" "$archive" || exit $?
205284990Scy        ;;
206284990Scy    esac
207284990Scy  done
208284990Scy
209284990Scyelif test -n "$extract"; then
210284990Scy  if test ! -f "$orig_archive"; then
211284990Scy    func_error "archive not found"
212284990Scy  fi
213284990Scy  if test $# -gt 0; then
214284990Scy    for member
215284990Scy    do
216284990Scy      case $1 in
217284990Scy        @*)
218284990Scy          func_at_file "${1#@}" -EXTRACT "$archive"
219284990Scy          ;;
220284990Scy        *)
221284990Scy          func_file_conv "$1"
222284990Scy          $AR -NOLOGO -EXTRACT:"$file" "$archive" || exit $?
223284990Scy          ;;
224284990Scy      esac
225284990Scy    done
226284990Scy  else
227284990Scy    $AR -NOLOGO -LIST "$archive" | sed -e 's/\\/\\\\/g' | while read member
228284990Scy    do
229284990Scy      $AR -NOLOGO -EXTRACT:"$member" "$archive" || exit $?
230284990Scy    done
231284990Scy  fi
232284990Scy
233284990Scyelif test -n "$quick$replace"; then
234284990Scy  if test ! -f "$orig_archive"; then
235284990Scy    if test -z "$create"; then
236284990Scy      echo "$me: creating $orig_archive"
237284990Scy    fi
238284990Scy    orig_archive=
239284990Scy  else
240284990Scy    orig_archive=$archive
241284990Scy  fi
242284990Scy
243284990Scy  for member
244284990Scy  do
245284990Scy    case $1 in
246284990Scy    @*)
247284990Scy      func_file_conv "${1#@}"
248284990Scy      set x "$@" "@$file"
249284990Scy      ;;
250284990Scy    *)
251284990Scy      func_file_conv "$1"
252284990Scy      set x "$@" "$file"
253284990Scy      ;;
254284990Scy    esac
255284990Scy    shift
256284990Scy    shift
257284990Scy  done
258284990Scy
259284990Scy  if test -n "$orig_archive"; then
260284990Scy    $AR -NOLOGO -OUT:"$archive" "$orig_archive" "$@" || exit $?
261284990Scy  else
262284990Scy    $AR -NOLOGO -OUT:"$archive" "$@" || exit $?
263284990Scy  fi
264284990Scy
265284990Scyelif test -n "$list"; then
266284990Scy  if test ! -f "$orig_archive"; then
267284990Scy    func_error "archive not found"
268284990Scy  fi
269284990Scy  $AR -NOLOGO -LIST "$archive" || exit $?
270284990Scyfi
271