#!/bin/sh
osc mkpac rust-$1
(
if [[ "$CRATE_URL" =~ ^https?:// ]]; then
CRATE_URL_BASENAME="$(basename "$CRATE_URL")"
wget -c "$CRATE_URL" -O "$CRATE_URL_BASENAME"
else
CRATE_URL_BASENAME="$1"
fi
cd rust-$1
sprg='s~%{crates_source}~https://crates.io/api/v1/crates/%crate/%version/download#/%crate-%version.crate~; s~%{crates_source %{crate} %{upstream_version}}~https://crates.io/api/v1/crates/%crate/%upstream_version/download#/%crate-%upstream_version.crate~;'
if [[ "$LITE" ]]; then
  sprg="$sprg"'; /%cargo_generate_buildrequires/d; /%cargo_test/d; /BuildRequires:.*crate(.*/d; /%cargo_build/d; s~%cargo_install~dir=%{buildroot}/%{crate_instdir}; mkdir -pv "$dir"; cp -R ./* $dir/; echo \"{\\\"files\\\":{},\\\"package\\\":\\\"\\\"}\" > "$dir/.cargo-checksum.json" ~;'
fi
SPEC=${SPEC:-fedora}

if [[ "$UPDATE" ]]; then

mkdir -p $HOME/.cargo
if [[ ! -d "$HOME/.cargo/crates.io-index/.git" ]]; then
rm -Rf "$HOME/.cargo/crates.io-index"
git clone https://github.com/rust-lang/crates.io-index "$HOME/.cargo/crates.io-index"
fi
mkdir -p .cargo
cat <<'EOFEOFEOF' > .cargo/nano
#!/usr/bin/bash
cd "$(dirname $(realpath $1))"
mkdir -p .cargo
cat <<EOFEOF > .cargo/config.toml
[net]
git-fetch-with-cli=true

[source.crates-io]
replace-with='mirror'

[source.mirror]
registry='file:///$HOME/.cargo/crates.io-index'
EOFEOF

cargo upgrade --incompatible -vvvvvvv
EOFEOFEOF

chmod 755 .cargo/nano
PATH="./.cargo/:$PATH" rust2rpm --patch --no-existence-check -t $SPEC --ignore-missing-license-files $CRATE_URL_BASENAME $2; sed -i "$sprg" rust-$1.spec
else
rust2rpm --no-existence-check -t $SPEC --ignore-missing-license-files $CRATE_URL_BASENAME $2; sed -i "$sprg" rust-$1.spec
fi

cat << EOF > _service
<services>
 <service name="download_files"/>
</services>
EOF
echo "setBadness('summary-too-long', 0)" > "rust-$1.rpmlintrc"

if [[ "$REQS" ]]; then

#rpmdev-spectool -A -g "rust-$1.spec"
text="$(rpmdev-spectool -A "rust-$1.spec")"

i=$(echo "$text" | grep Source0 | sed 's~Source0:[[:space:]]*~~; s~.*/~~')

cp "$HOME/.cache/rust2rpm/$i" "./$I" ||:
tar -xzf "./$i"
rpmdev-spectool -A -g "rust-$1.spec"
cp "./$1" "$HOME/.cache/rust2rpm/$i" ||:
rm "./$i"
cd "${i:0:-6}"

for i in $(echo "$text" | grep Patch | sed 's~Patch[^:]*:[[:space:]]*~~; s~.*/~~'); do
  patch --directory="$PWD" --unified --strip=1 --posix --force --set-utc --verbose < "../$i"
done

git init
git add --all
git commit -m update

if [[ "$REQS" ]]; then
REQS=""
for i in $(cargo tree  --depth=1 --format={lib} --prefix=none -e=no-dev --no-dedupe | sed 's~_~-~;' | uniq | tail -n +2); do
  REQS="$REQS crate($i)"
done
fi

git add Cargo.toml
git commit -m update
file="fix_$(date +'%Y%j%N').diff"
git diff HEAD^1 > "$file"

name="rust-$1"

if [[ -n "$REQS" ]]; then
sed -i "1s/^/BuildRequires: $REQS\n/" "../$name.spec"
fi

if [[ -s "$file" ]]; then
mv "$file" ../
cd ../

num=$(( $(echo $(echo "$text" | grep Patch | tail -n1 | sed 's~Patch\([^:]\)*:.*~\1~')) + 1 ))

sed -i "1s/^/Patch${num}: $file\n/" "$name.spec"
else
cd ../
fi

fi

osc add *
osc update
osc ci -m update
osc service rr
)
