summaryrefslogtreecommitdiffstats
path: root/compat32-tools/convertpkg-compat32
blob: ea8f2d320298f444c68b3da4c04bffdde7f11ad7 (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
#!/bin/sh

# $Id$

# Copyright (c) 2009, Frederick Emmott <fred@slackware.com>
# Copyright (c) 2009, Eric Hameleers, Eindhoven, NL
# 
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
# 
# THE SOFTWARE IS PROVIDED ``AS IS'' AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

# Convert a 32-bit Slackware package (s390 or x86)
# to a compatibility package for a 64bit multilib Slackware.

# Catch errors and display the offending line number:
set -e
trap 'echo "$0 FAILED at line ${LINENO}"' ERR

# Package-independent variables
ARCH=${ARCH:-$(uname -m)}
OUTPUT=${OUTPUT:-/tmp}    # where the package gets created
TMP=${TMP:-/tmp}          # location for temporary files

# $BUILD can also be overridden, though it in-turn is overridden
# if an output package name is specified on the command line.

# Blacklist of packages not to use this script on (these *have* to be compiled
# on a 64bit box):
BLACKLIST="
glibc.*
kernel.*
gcc.*
"

function show_help () {
  # Write the help text to output:
  cat <<EOF

Usage: $0 <-i input_package_file_name> [-o output_package_file_name] [-d output_directory] [-s custom_slack_desc]

$(basename $0) is used to convert 32-bit Slackware packages
to compatibility packages for 64-bit variants.

required parameters::
  -i input_package_file_name     : 32-bit package to convert
optional parameters::
  -d destination_directory       : create package in this directory
  -o output_package_file_name    : package name to create
  -s output_slack_desc           : slack-desc to use for new package

environment variables:
  ARCH   (target architecture; defaults to \$(uname -m))
  BUILD  (build number for output package; defaults to same as input package.
          output_package_file_name overrides this value)
  OUTPUT (location to create the package; defaults to /tmp)
  TMP    (location for temporary files; defaults to /tmp)

EOF
}

# Zero some initial variables:
OUTPKG=""
PKGFILE=""
PKGPATH=""
SLACKDESC=""

# Parse the commandline parameters:
while [ ! -z "$1" ]; do
  case $1 in
    -d|--destdir)
      OUTPUT="$(cd $(dirname ${2}); pwd)/$(basename ${2})"
      shift 2
      ;;
    -h|--help)
      show_help
      exit 0
      ;;
    -i|--inpkg)
      PKGFILE="$(basename ${2})"
      PKGPATH="$(cd $(dirname ${2}); pwd)/$(basename ${2})"
      shift 2
      ;;
    -o|--outpkg)
      OUTPKG="$(cd $(dirname ${2}); pwd)/$(basename ${2})"
      shift 2
      ;;
    -s|--slack-desc)
      SLACKDESC="$(cd $(dirname ${2}); pwd)/$(basename ${2})"
      shift 2
      ;;
    -*)
      echo "Unsupported parameter '$1'!"
      exit 1
      ;;
    *)
      # Do nothing
      shift
      ;;
  esac
done

# Bail out now if we did not get an input package:
if [ -z "$PKGFILE" -o ! -e "$PKGPATH" ]; then
  echo "** Please supply a valid input package! **"
  show_help
  exit 3
fi

# if a destination_directory was specified, abort now if we can not create it:
if [ -n "$OUTPUT" -a ! -d "$OUTPUT" ]; then
  echo "Creating output directory '$OUTPUT'..."
  mkdir -p $OUTPUT
  if [ ! -w "$OUTPUT" ]; then
    echo "Creating output directory '$OUTPUT' failed!"
    exit 3
  fi
fi

# Figure out initial variables
PKGNAM=$(echo $PKGFILE | rev | cut -f4- -d- | rev)
VERSION=$(echo $PKGFILE | rev | cut -f3 -d- | rev)
BUILD=${BUILD:-$(echo $PKGFILE | rev | cut -f1 -d- | rev | cut -f1 -d.)}
OUTPKG=${OUTPKG:-"${PKGNAM}-compat32-${VERSION}-${ARCH}-${BUILD}.txz"}
# With OUTPKG as commandline param, it may not just be "${PKGNAM}-compat32":
PKGNAM32=$(echo $OUTPKG | rev | cut -f4- -d- | rev)

for regex in $BLACKLIST; do
  if echo $PKGNAM | grep -Pq "$regex"; then
    echo "Package $PKGNAM is blacklisted by '$regex', aborting."
    exit 2
  fi
done

echo "Converting package $PKGNAM (version $VERSION) to $OUTPKG ($PKGNAM32)"

PKG=$TMP/package-$PKGNAM32
rm -rf $PKG
mkdir -p $PKG $TMP
cd $PKG || exit 1

# Explode the package into $PKG .
# We will not touch the existing install/doinst.sh - it should
# create symlinks and run other errands when the resulting package is installed.
/sbin/explodepkg $PKGPATH

# Remove stuff we only want from the 64-bit package:
rm -rf bin sbin usr/{include,sbin,share,info,man,libexec} etc/profile.d

# Take care of gtk+2, pango and 32bit binaries in general:
if [ "$PKGNAM" = "gtk+2" -o  "$PKGNAM" = "pango" ]; then
  find usr/bin -type f ! -name "*-32" -exec mv {} {}-32 \;
elif [ -d usr/bin ]; then
  mkdir ./32
  find usr/bin -type f -exec mv {} ./32 \;
  rm -rf usr/bin/*
  mv ./32 usr/bin/
fi

# Keep documentation we might be required to keep, or is just polite:
if [ -d usr/doc ]; then
  find usr/doc -type f ! -iname "COPYING*" -a ! -iname "AUTHORS*" -a ! -iname "LICENSE*" -a ! -iname  "GPL*" | xargs -d '\n' rm -f
  find usr/doc -type d -depth | xargs -d '\n' rmdir --ignore-fail-on-non-empty
fi

if [ ! -z $SLACKDESC ]; then
  echo "Using externally provided slack-desc ($SLACKDESC)..."
  cat $SLACKDESC > install/slack-desc
else
  # Re-work the original slack-desc

  # Fix the handy ruler:
  SPCS=""; while [ ${#SPCS} -lt ${#PKGNAM32} ]; do SPCS=" $SPCS";done
  sed -i -r "s/^ *\|-/${SPCS}\|-/" install/slack-desc

  # Every line; foo: -> foo-compat32:
  sed -i "s,$PKGNAM:,$PKGNAM32:," install/slack-desc

  # First line: foo-compat32: foo (description of foo)
  #   -> foo-compat32: foo-compat32 (description of foo)
  sed -i "s,$PKGNAM32: $PKGNAM ,$PKGNAM32: $PKGNAM32 ," install/slack-desc

  # Last line: if empty, add 32-bit message
  sed -i "\$s,^${PKGNAM32}: *$,${PKGNAM32}: This package contains 32-bit compatibility binaries.," install/slack-desc
fi

# Make the package (don't process the symlinks):
/sbin/makepkg --linkadd n --chown n $OUTPUT/$OUTPKG

echo "Package created:  $OUTPUT/$OUTPKG"