#!/bin/bash
#
# A script for creating release packages. The release packages are create in the home directory.
#
# Create release candidate
# ========================
#
# check every isPremiumEnabled call:
# - every id should be in --errorlist
#   git grep 'isPremiumEnabled[(]"' | sed 's/.*isPremiumEnabled[(]"//' | sed 's/".*//' | sort | uniq > ids1.txt
#   ./cppcheck --errorlist | grep ' id="' | sed 's/.* id="//' | sed 's/".*//' | sort | uniq > ids2.txt
#   diff -y ids1.txt ids2.txt
# - premiumaddon: check coverage.py
#   python3 coverage.py --id ; sort ids-*.txt | uniq > ~/cppcheck/ids3.txt
#   diff -y ids2.txt ids3.txt
#
# Windows installer:
# - ensure latest build was successful
# - ensure cfg files etc are included (win_installer/cppcheck.wxs)
#
# self check, fix critical issues:
# make clean && make CXXFLAGS=-O2 MATCHCOMPILER=yes -j4
# ./cppcheck -D__CPPCHECK__ -D__GNUC__ -DCHECK_INTERNAL -DHAVE_RULES --std=c++11 --library=cppcheck-lib --library=qt --enable=style --inconclusive --inline-suppr --suppress=bitwiseOnBoolean --suppress=shadowFunction --suppress=useStlAlgorithm --suppress=*:externals/picojson.h --suppress=functionConst --suppress=functionStatic --xml cli gui/*.cpp lib 2> selfcheck.xml
#
# Generate lib/checkers.cpp (TODO the premium checkers should not be statically coded)
# cd ~/cppchecksolutions/cppcheck && python3 tools/get_checkers.py > lib/checkers.cpp
#
# Update translations
#   lupdate gui.pro
#
# Update copyright year
# git diff 2.8 -- */*.cpp */*.h | grep '^diff --git a/' | sed 's|.* b/||' | xargs sed -i 's/Copyright (C) 2007-20[12]./Copyright (C) 2007-2022/'
# git diff | grep '^diff --git a/'
#
# Make sure "cppcheck --errorlist" works:
# make clean && make -j4 && ./cppcheck --errorlist > errlist.xml && xmllint --noout errlist.xml
#
# Update AUTHORS using output from:
# git log --format='%aN' 2.7..HEAD | sort -u > AUTHORS2 && diff -y AUTHORS AUTHORS2 | less
#
# Create 2.8.x branch
# git checkout -b 2.8.x ; git push -u origin 2.8.x
#
# Release notes:
# - ensure safety critical issues are listed properly
# - empty the releasenotes.txt in main branch
#
# Update version numbers in:
# sed -i -r "s/version 2[.][0-9]+([.]99)*/version 2.13.0/" cli/main.cpp
# sed -i -r "s|2[.][0-9]+([.]99)*|2.13.0|" cmake/versions.cmake # version must have 3 parts.
# sed -i -r "s/CPPCHECK_MINOR_VERSION [0-9]+/CPPCHECK_MINOR_VERSION 13/" lib/version.h
# sed -i -r "s/2[.][0-9]+([.]99)*( dev)*/2.13.0/" win_installer/productInfo.wxi
# sed -i -r "s/subtitle: Version 2\.[0-9]+.*/subtitle: Version 2.13/" man/*.md
# Ensure that "-rc1" is added in productInfo.wxi and lib/version.h
# Verify:
# grep '\.99' */*.[ch]* && grep '[0-9][0-9] dev' */*.[ch]*
# egrep "2\.[0-9]+" */*.h */*.cpp man/*.md | grep -v "test/test" | less
# git commit -a -m "2.8: Set versions"
#
# Build and test the windows installer
#
# Update the Makefile:
# make dmake && ./dmake --release
# Uppdatera CI så att dmake körs med --release
# git commit -a -m "2.8: Updated Makefile"
#
# Ensure that CI is happy
#
# Tag:
# git tag 2.8-rc1
# git push --tags
#
# Release
# =======
#
# Remove "-rc1" from versions. Test: git grep "\-rc[0-9]"
#
# Create a release folder on sourceforge:
# https://sourceforge.net/projects/cppcheck/files/cppcheck/
#
# git tag 2.8 ; git push --tags
# ./createrelease 2.8
#
# copy msi from release-windows, install and test cppcheck
# copy manual from build-manual
#
# Update download link on index.php main page
#
# Write Changelog
# git log 2.11..2.12 > Changelog
# <edit>
#
# Trac:
# 1. Create ticket "2.12 safety cosmetic changes"
# 2. Check priorities for all tickets in milestone. Should be: safety-*
# 3. Create new milestone
# 4. Close old milestone
#
# write a news
#
# save "cppcheck --doc" output on wiki
#
# compile new democlient:
# ssh -t danielmarjamaki,cppcheck@shell.sourceforge.net create
# ./build-cppcheck.sh
#
# run daca with new release
# 1. edit tools/donate-cpu-server.py. Update OLD_VERSION and SERVER_VERSION
# 2. scp -i ~/.ssh/osuosl_id_rsa tools/donate-cpu-server.py danielmarjamaki@cppcheck1.osuosl.org:/var/daca@home/
#
# Backup:
# * trac: cd /var && nice tar -cJf trac.tar.xz trac-cppcheck
# * git: git checkout -f && git checkout main && git pull && tar -cJf git.tar.xz .git
# * Changelog
# * ci status: screenshot(s) showing that all tests pass for tagged commit

# Folder/tag to use
folder=$1
tag=$folder.0

# Name of release
releasename=cppcheck-$tag

set -e

cd ~/cppcheck

git checkout $tag

rm -rf upload
mkdir -p upload

make clean

# Create archives..
git archive --format=tar --prefix=$releasename/ $tag | gzip > upload/$releasename.tar.gz
git archive --format=tar --prefix=$releasename/ $tag | bzip2 > upload/$releasename.tar.bz2
git archive --format=zip -9 --prefix=$releasename/ $tag > upload/$releasename.zip
cd upload
scp $releasename.* danielmarjamaki,cppcheck@frs.sourceforge.net:/home/frs/project/c/cp/cppcheck/cppcheck/$folder/
rm $releasename.*
cd ..

# Generate version.txt
make -j12
rm -f cppcheck.cfg
./cppcheck --version > upload/version.txt

cd ~/cppcheck/upload
scp version.txt danielmarjamaki,cppcheck@web.sourceforge.net:htdocs/

cd ~/cppcheck
rm -rf upload

# Local cppcheck binary
mkdir -p ~/.cppcheck/$tag
cd ~/.cppcheck/$tag
cp -R ~/cppcheck/cfg .
cp -R ~/cppcheck/addons .
cp -R ~/cppcheck/platforms .
cd ~/cppcheck
make clean ; make -j12 FILESDIR=~/.cppcheck/$tag MATCHCOMPILER=yes CXXFLAGS=-O2
mv cppcheck ~/.cppcheck/cppcheck-$tag

git checkout main
