summaryrefslogtreecommitdiffstats
path: root/scripts/source-archive
blob: 5a9358f7a463c8d4e0267f0eabea876e089a409e (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
#!/bin/sh

CURRENT_DIR=$(pwd)
PKGNAME=$(basename $CURRENT_DIR)

FORMAT="tgz"
VERSION="1.0"
SPECDIR="$HOME/.specs"
OUTPUT=$SPECDIR/$PKGNAME
UPDATE_SOURCES=1

if [ -d $OUTPUT ]
then
  SPECFILE=$(find -L $SPECDIR/$PKGNAME -name '*.spec' | head -1)
  if [ "x$SPECFILE" != "x" ]
  then
    DEDUCED_VERSION=$(sed -n 's,^\s*Version:\s*\(.*\)\s*$,\1,p' < $SPECFILE)

    if [ "x$DEDUCED_VERSION" != "x" ]
    then
      VERSION=$DEDUCED_VERSION
    fi
  fi
fi

function die
{
  echo $@ >&2
  exit 1
}

function check_set
{
  VALUE=$1
  shift

  while [ $# -ne 0 ]
  do
    if [ "x$1" = "x$VALUE" ]
    then
      return 0
    fi
    shift
  done

  return 1
}

function compress_tar
{
  tee
}

function compress_tgz
{
  gzip
}

function compress_tbz2
{
  bzip2
}

function compress_txz
{
  xz
}

function extension
{
  case $1 in
    tar)
      echo tar
      ;;

    tgz)
      echo tar.gz
      ;;

    tbz2)
      echo tar.bz2
      ;;

    txz)
      echo tar.xz
      ;;

    *)
      die "Unkown format $1"
      ;;
  esac
}

while [ $# -gt 0 ]
do
  OPTION=$1
  shift

  case $OPTION in
    -f|--format)
      FORMAT=$1
      shift
      ;;

    -v|--version)
      VERSION=$1
      shift
      ;;

    -o|--output)
      OUTPUT=$1
      shift
      ;;

    --no-update)
      UPDATE_SOURCES=0
      ;;

    -h|--help)
      echo "Create archive from the current git repository

usage: $0 [ options ]

options:
  -f --format       Format of the archive (see supported formats below) (default: $FORMAT)
  -v --version      Set version of script (default: $VERSION)
  -o --output       Output directory or file (default: $OUTPUT)
     --no-update    Do not update sources file

  -h --help         Show this help message

Supported formats:
  tar
  tgz
  tbz2
  txz"
      exit 1
      ;;

    -*)
      echo "Unkown option $OPTION" >&2
      exit 1
      ;;
  esac
done

[ ! -d $OUTPUT -o ! -d $(dirname $OUTPUT) ] && die "$OUTPUT is not a file or directory"

check_set $FORMAT tar tgz tbz2 txz || die "$FORMAT is not supported"

if [ -d $OUTPUT ]
then
  SAVE_TO=$OUTPUT/$PKGNAME-$VERSION.$(extension $FORMAT)
else
  SAVE_TO=$OUTPUT
fi

git archive --format=tar --prefix=$PKGNAME-$VERSION/ HEAD | compress_$FORMAT > $SAVE_TO
echo "Archive saved to $SAVE_TO"

if [ $UPDATE_SOURCES -ne 0 -a -d $OUTPUT -a -f $OUTPUT/sources ]
then
  ARCH=$(basename $SAVE_TO)
  cd $OUTPUT
  md5sum $ARCH > sources
  cd - >/dev/null

  echo "MD5 checksum saved to 'sources' file"
fi