#!/usr/bin/bash
#####################################
# mk-spec script for librnd, this creates a new spec using upstream data.
#
# For a new full release:
# Update the original spec file with the new version and release.
# Get the new tarball: mgarepo sync -d --dry-run
# Then run this script in SOURCES: ./mk-spec
#
# For a pre-release snapshot:
# Use mk-tar script then update the original spec file with
# the new version, release and new svn revision as output from mk-tar.
# Now run this script from SOURCES: ./mk-spec
#####################################

currdir=$(pwd)
dirname=$(pwd|rev|cut -d/ -f1|rev)
[[ $dirname != SOURCES ]] && { echo "Not in SOURCES"; exit 1; }

# Create new spec file in SPECS
cp -f spec.tpl ../SPECS/librnd.spec.new

# Get version from old spec
ver=$(cat ../SPECS/librnd.spec | grep -m 1 "Version:" |tr '\t' ' '|tr -s ' '| cut -d' ' -f2)
version=$ver
# Get rel from old spec
rel=$(cat ../SPECS/librnd.spec | grep -m 1 "%define rel"|tr '\t' ' '|tr -s ' ' |cut -d ' ' -f3)

# Check if define svn is commented in old spec
svn=$(cat ../SPECS/librnd.spec |grep -v '^#' | grep -m1 "define svn"|cut -c1)
if [[ $svn == "%" ]]; then # Use svn snapshot tarball
    svnrev=$(cat ../SPECS/librnd.spec |grep -v '^#' | grep -m1 "^%define svn"|tr '\t' ' '|tr -s ' '|rev|cut -d' ' -f1|rev)
    sed -i "s/\@svn-revision\@/%define svn             $svnrev/" "$currdir"/../SPECS/librnd.spec.new
    tarver="$svnrev"
else
    sed -i "s/\@svn-revision\@/#define svn/" "$currdir"/../SPECS/librnd.spec.new
    tarver="$version"
fi

# Get major
major=$(echo "$version" | cut -d. -f1)

# Extract tarball in SOURCES
tar -xf librnd-"$tarver".tar.gz

# Set version from old spec into new spec
sed -i "s/\@version\@/Version:                $version/" "$currdir"/../SPECS/librnd.spec.new

# Set rel from old spec into new spec
sed -i "s/\@release\@/%define rel             $rel/" "$currdir"/../SPECS/librnd.spec.new

# Generate upstream packaging lists
cd "librnd-$tarver/doc/developer/packaging" || exit 1
chmod +x librnd_packages.sh
./packages.sh || { echo "$(pwd)/packages.sh failed"; exit 1; }

cd "$currdir" || exit 1

# Save auto dir path
auto="librnd-$tarver/doc/developer/packaging/auto/"

# Configure args
args=$(cat "$auto"Configure.args|tr '\n' ':'|tr ':' ' ')
sed -i "s:@MAIN_CONFIGURE_OPTIONS@:$args:" ../SPECS/librnd.spec.new

cd "$auto" || exit 1

# Requires
for p in *.deps; do
    pkg="$p"
    tr ' ' '\n ' < "$p"  > "$pkg".tmp
    pk=$(echo "$p"|cut -d- -f2-|cut -d. -f1)
    while read line; do
        line=$(echo "$line"|sed "s/librnd$major/%{_lib}rnd%{major}/g")
        echo -e "Requires:               $line = %{version}-%{release}" >> "$currdir"/tmp.txt
    done < "$pkg".tmp
    tmptxt="$(cat "$currdir"/tmp.txt)"
    sed -i "s/@${pk}-req@/${tmptxt//$'\n'/\\n}/" "$currdir"/../SPECS/librnd.spec.new
    rm -f "$currdir"/tmp.txt
done

# files
for p in *.files; do
pkg=$(echo "$p"|cut -d. -f1|cut -d- -f2-)
    [[ $pkg == "doc"  ]] && continue
    files=$(cat $p|sed 's:\\n: :g')
    files=$(echo $files|sed -e 's/$PREFIX/%{_usr}/g')
    files=$(echo $files|sed -e 's:%{_usr}/share:%{_datadir}:g')
    files=$(echo $files|sed -e 's:%{_datadir}/man:%{_mandir}:g')
    files=$(echo $files|sed -e 's:%{_usr}/include:%{_includedir}:g')
    files=$(echo $files|sed -e 's:%{_usr}/bin:%{_bindir}:g')
    files=$(echo $files|sed -e 's:%{_usr}/lib/librnd/plugins:%{libplugindir}:g')
    files=$(echo $files|sed -e 's:%{_usr}/lib:%{_libdir}:g')
    files=$(echo $files|sed -e 's/$P/%{plugindir}/g')
    files=$(echo $files|sed -e 's/$C/%config(noreplace)\@%{_datadir}\/%{name}/g')
    files=$(echo $files|sed -e 's/$LP/%{libplugindir}/g')
    files=$(echo $files|sed -e 's/$LC/%config(noreplace)\@%{_datadir}\/librnd/g')
    files=$(echo $files|sed -e 's/librnd/%{name}/g')
    files=$(echo $files|sed -e 's/ /\\n/g')
    files=$(echo $files|sed -e 's/@/ /g')
sed -i "s:@${pkg}-files@:${files//$'\n'/\\n}:" "$currdir"/../SPECS/librnd.spec.new
done

# Summary
for p in *.short; do
    pkg=$(echo "$p"|cut -d. -f1|cut -d- -f2-)
    summary=$(cat "$p"|sed 's_\.$__'|sed 's_librnd __'|sed 's_ for librnd__'|sed 's_._\u&_')
    sed -i "s_@${pkg}-summary@_${summary//$'\n'/\\n}_" "$currdir"/../SPECS/librnd.spec.new
done

# Sub-package descriptions
for p in *.long; do
    pkg="librnd-$(echo "$p"|cut -d. -f1|cut -d- -f2-)"
    # Limit line length
    desc=$(cat $p|sed 's/\t//g'|fmt -w79)
    sed -i "s_@${pkg}-desc@_${desc//$'\n'/\\n}_" "$currdir"/../SPECS/librnd.spec.new
done

# Add * to man files for compressed versions
sed -i 's/%{_man.*/&*/g' "$currdir"/../SPECS/librnd.spec.new

# Move these files to /usr/lib on 64 bit
sed -i 's:%{_libdir}/%{name}/\*.scm:%{_usr}/lib/%{name}/\*.scm:' "$currdir"/../SPECS/librnd.spec.new

# Save original spec as backup
cp -f "$currdir"/../SPECS/librnd.spec "$currdir"/../SPECS/librnd.spec~ || \
{ echo "Problem backing up old spec"; exit 1; }

# Write new spec.
cp -f "$currdir"/../SPECS/librnd.spec.new "$currdir"/../SPECS/librnd.spec || \
{ echo "Problem writing new spec"; exit 1; }

# Remove librnd.spec.new
rm -f "$currdir"/../SPECS/librnd.spec.new

echo "../SPECS/librnd.spec has been updated :)"
