#!/bin/bash -e

if ! [ "$1" ]
then
   echo "usage: $0 <version>"
   exit 1
fi

if ! [ -d ".git" ]
then
   echo "Should be run from the LuaRocks git repo dir."
   exit 1
fi

make clean || exit 1

version=$1
shift

#-------------------------------------------------------------------------------
if ! [ "$version" = "dev" ]
then

# e.g. if $version is "2.3.0", $xyversion is "2.3"
xyversion=${version%.*}

ROCKSPEC="luarocks-$version-1.rockspec"

if [ "$1" = "branch" ]
then
   shift

   if git show $version &> /dev/null
   then
      echo "Branch $version already exists."
      exit 1
   fi

   git reset
   git checkout .
   git checkout -B $version
   ROCKSPEC="luarocks-$version-1.rockspec"
   currentrockspec=$(ls luarocks-*.rockspec)
   if [ "$currentrockspec" != "$ROCKSPEC" ] && ! [ -e "$ROCKSPEC" ]
   then
      git mv luarocks-*.rockspec "$ROCKSPEC"
   fi
   sed -i 's/"Configuring LuaRocks version .*"/"Configuring LuaRocks version '$version'..."/' configure
   sed -i 's/version = "[^"]*"/version = "'$version'-1"/' $ROCKSPEC
   sed -i 's/\(   url = "[^"]*",\)/\1\n   tag = "v'$version'"/' $ROCKSPEC
   sed -i 's/program_version = "[^"]*"/program_version = "'$version'"/' src/luarocks/core/cfg.lua
   sed -i 's/version: [0-9.]*/version: '$version'./' appveyor.yml
   sed -i 's/LUAROCKS_VER: [0-9.]*/LUAROCKS_VER: '$version'/' appveyor.yml
   sed -i 's/vars.VERSION = "[0-9.]*"/vars.VERSION = "'$xyversion'"/' install.bat
   echo "==============================================================================="
   git diff
   echo "==============================================================================="
   echo "Does the change look alright? Press 'y' to commit"
   echo "==============================================================================="
   read
   if [ "$REPLY" = "y" ]
   then
      git commit -av -m "Release $version"
   fi
fi


[ -e "$ROCKSPEC" ] || {
   echo
   echo "$ROCKSPEC is missing. Please check rockspec version is correct."
}

grep -q "LuaRocks version $version" "configure" || {
   echo
   echo "version in configure is incorrect. Please fix it."
   exit 1
}

grep -q "\"$version-1\"" "$ROCKSPEC" || {
   echo
   echo "version in rockspec is incorrect. Please fix it."
   exit 1
}

grep -q "program_version = \"$version\"" src/luarocks/core/cfg.lua || {
   echo
   echo "program_version in src/luarocks/core/cfg.lua is incorrect. Please fix it."
   exit 1
}

grep -q "version: $version\\." appveyor.yml || {
   echo
   echo "version in appveyor.yml is incorrect. Please fix it."
   exit 1
}

grep -q "LUAROCKS_VER: $version" appveyor.yml || {
   echo
   echo "LUAROCKS_VER in appveyor.yml is incorrect. Please fix it."
   exit 1
}

grep -q "vars.VERSION = \"$xyversion\"" install.bat || {
   echo
   echo "vars.VERSION in install.bat is incorrect. Please fix it."
   exit 1
}

fi # if ! [ "$version" = "dev" ]
#-------------------------------------------------------------------------------

out="luarocks-$version"
rm -rf "$out"
mkdir "$out"

git ls-files | while read i
do
   if [ -f "$i" ]
   then
      dir=`dirname $i`
      mkdir -p "$out/$dir"
      cp "$i" "$out/$dir"
   fi
done

rm -rf "release-unix" "release-windows" "$out.tar.gz" "$out-win32.zip"

mkdir "release-unix"
cp -a "$out" "release-unix"
mkdir "release-windows"
mv "$out" "release-windows/$out-win32"

cd "release-unix/$out"
rm -rf makedist install.bat win32 .github .gitignore appveyor* .appveyor
cd ..
tar czvpf ../"$out.tar.gz" "$out"
rm -f ../"$out.tar.gz.asc"
cd ..
rm -rf "release-unix"

cd "release-windows/$out-win32"
rm -rf makedist Makefile GNUmakefile configure .github .gitignore test appveyor* .appveyor
cd ..
zip -r ../"$out-win32.zip" "$out-win32"
rm -f ../"$out-win32.zip.asc"
cd ..
rm -rf "release-windows"

if [ "$1" = "binary" ]
then
   shift

   ./configure --lua-version=5.4

   make binary
   cd build-binary
   mkdir "$out-linux-x86_64"
   cp luarocks.exe "$out-linux-x86_64/luarocks"
   cp luarocks-admin.exe "$out-linux-x86_64/luarocks-admin"
   zip "../$out-linux-x86_64.zip" "$out-linux-x86_64"/*
   cd ..
   rm -f "$out-linux-x86_64.zip.asc"

   make windows-binary-32
   cd build-windows-binary-i686-w64-mingw32
   mkdir "$out-windows-32"
   cp luarocks.exe "$out-windows-32/luarocks.exe"
   cp luarocks-admin.exe "$out-windows-32/luarocks-admin.exe"
   zip "../$out-windows-32.zip" "$out-windows-32"/*
   cd ..
   rm -f "$out-windows-32.zip.asc"

   make windows-binary-64
   cd build-windows-binary-x86_64-w64-mingw32
   mkdir "$out-windows-64"
   cp luarocks.exe "$out-windows-64/luarocks.exe"
   cp luarocks-admin.exe "$out-windows-64/luarocks-admin.exe"
   zip "../$out-windows-64.zip" "$out-windows-64"/*
   cd ..
   rm -f "$out-windows-64.zip.asc"

fi

if [ "$1" = "sign" ]
then
   shift

   for f in \
      $out-windows-32.zip \
      $out-windows-64.zip \
      $out-linux-x86_64.zip \
      $out-win32.zip \
      $out.tar.gz
   do
      [ -e "$f" -a ! -e "$f.asc" ] && gpg --armor --output "$f.asc" --detach-sign "$f"
   done
fi