1
0
mirror of https://github.com/deajan/obackup.git synced 2024-11-14 19:53:42 +01:00
obackup/dev/merge.sh

205 lines
5.2 KiB
Bash
Raw Permalink Normal View History

2015-11-12 01:26:38 +01:00
#!/usr/bin/env bash
## MERGE 2020031501
2016-08-06 16:14:07 +02:00
2016-08-06 15:15:11 +02:00
## Merges ofunctions.sh and n_program.sh into program.sh
## Adds installer
2015-11-12 01:26:38 +01:00
2018-10-02 21:19:52 +02:00
PROGRAM=merge
INSTANCE_ID=dev
function Usage {
echo "Merges ofunctions.sh and n_program.sh into debug_program.sh and ../program.sh"
echo "Usage"
echo "$0 osync|obackup|pmocr"
}
2016-12-20 21:19:31 +01:00
function __PREPROCESSOR_Merge {
2018-10-02 21:19:52 +02:00
local nPROGRAM="$1"
2015-11-12 01:26:38 +01:00
2018-10-02 21:19:52 +02:00
if [ -f "$nPROGRAM" ]; then
Logger "$nPROGRAM is not found in local path." "CRITICAL"
2016-12-20 21:19:31 +01:00
exit 1
fi
2016-08-30 18:28:27 +02:00
2018-10-02 21:19:52 +02:00
VERSION=$(grep "PROGRAM_VERSION=" n_$nPROGRAM.sh)
VERSION=${VERSION#*=}
__PREPROCESSOR_Constants
__PREPROCESSOR_Unexpand "n_$nPROGRAM.sh" "debug_$nPROGRAM.sh"
2015-11-12 01:26:38 +01:00
2016-12-20 21:19:31 +01:00
for subset in "${__PREPROCESSOR_SUBSETS[@]}"; do
2018-10-02 21:19:52 +02:00
__PREPROCESSOR_MergeSubset "$subset" "${subset//SUBSET/SUBSET END}" "ofunctions.sh" "debug_$nPROGRAM.sh"
2016-12-20 21:19:31 +01:00
done
2015-11-12 01:26:38 +01:00
2018-10-02 21:19:52 +02:00
__PREPROCESSOR_CleanDebug "debug_$nPROGRAM.sh" "../$nPROGRAM.sh"
2016-12-20 21:19:31 +01:00
}
function __PREPROCESSOR_Constants {
PARANOIA_DEBUG_LINE="#__WITH_PARANOIA_DEBUG"
PARANOIA_DEBUG_BEGIN="#__BEGIN_WITH_PARANOIA_DEBUG"
PARANOIA_DEBUG_END="#__END_WITH_PARANOIA_DEBUG"
__PREPROCESSOR_SUBSETS=(
'#### OFUNCTIONS FULL SUBSET ####'
'#### OFUNCTIONS MINI SUBSET ####'
2018-10-02 21:19:52 +02:00
'#### OFUNCTIONS MICRO SUBSET ####'
'#### PoorMansRandomGenerator SUBSET ####'
2016-12-20 21:19:31 +01:00
'#### _OFUNCTIONS_BOOTSTRAP SUBSET ####'
2019-02-26 12:09:33 +01:00
'#### RUN_DIR SUBSET ####'
2016-12-20 21:19:31 +01:00
'#### DEBUG SUBSET ####'
'#### TrapError SUBSET ####'
'#### RemoteLogger SUBSET ####'
2018-07-30 15:36:31 +02:00
'#### Logger SUBSET ####'
2016-12-20 21:19:31 +01:00
'#### GetLocalOS SUBSET ####'
'#### IsInteger SUBSET ####'
'#### UrlEncode SUBSET ####'
'#### HumanToNumeric SUBSET ####'
'#### ArrayContains SUBSET ####'
2017-04-11 13:28:32 +02:00
'#### VerComp SUBSET ####'
2017-02-09 12:13:36 +01:00
'#### GetConfFileValue SUBSET ####'
'#### SetConfFileValue SUBSET ####'
2018-03-25 14:16:31 +02:00
'#### CheckRFC822 SUBSET ####'
2018-10-02 21:19:52 +02:00
'#### CleanUp SUBSET ####'
'#### GenericTrapQuit SUBSET ####'
'#### FileMove SUBSET ####'
2016-12-20 21:19:31 +01:00
)
}
function __PREPROCESSOR_Unexpand {
local source="${1}"
local destination="${2}"
unexpand "$source" > "$destination"
2016-08-30 18:28:27 +02:00
if [ $? != 0 ]; then
2019-02-26 12:09:33 +01:00
Logger "Cannot unexpand [$source] to [$destination]." "CRITICAL"
2016-08-30 18:28:27 +02:00
exit 1
fi
2015-11-12 01:26:38 +01:00
}
2016-12-20 21:19:31 +01:00
function __PREPROCESSOR_MergeSubset {
local subsetBegin="${1}"
local subsetEnd="${2}"
local subsetFile="${3}"
local mergedFile="${4}"
sed -n "/$subsetBegin/,/$subsetEnd/p" "$subsetFile" > "$subsetFile.$subsetBegin"
2016-08-30 18:28:27 +02:00
if [ $? != 0 ]; then
2019-02-26 12:09:33 +01:00
Logger "Cannot sed subset [$subsetBegin -- $subsetEnd] in [$subsetFile]." "CRTICIAL"
2016-08-30 18:28:27 +02:00
exit 1
fi
2016-12-20 21:19:31 +01:00
sed "/include $subsetBegin/r $subsetFile.$subsetBegin" "$mergedFile" | grep -v -E "$subsetBegin\$|$subsetEnd\$" > "$mergedFile.tmp"
2016-08-30 18:28:27 +02:00
if [ $? != 0 ]; then
2019-02-26 12:09:33 +01:00
Logger "Cannot add subset [$subsetBegin] to [$mergedFile]." "CRITICAL"
2016-08-30 18:28:27 +02:00
exit 1
fi
2016-12-20 21:19:31 +01:00
rm -f "$subsetFile.$subsetBegin"
2016-08-30 18:28:27 +02:00
if [ $? != 0 ]; then
2019-02-26 12:09:33 +01:00
Logger "Cannot remove temporary subset [$subsetFile.$subsetBegin]." "CRITICAL"
2016-08-30 18:28:27 +02:00
exit 1
fi
2016-12-20 21:19:31 +01:00
rm -f "$mergedFile"
2016-08-30 18:28:27 +02:00
if [ $? != 0 ]; then
2019-02-26 12:09:33 +01:00
Logger "Cannot remove merged original file [$mergedFile]." "CRITICAL"
2016-08-30 18:28:27 +02:00
exit 1
fi
2016-12-20 21:19:31 +01:00
mv "$mergedFile.tmp" "$mergedFile"
if [ $? != 0 ]; then
2019-02-26 12:09:33 +01:00
Logger "Cannot move merged tmp file to original [$mergedFile]." "CRITICAL"
2016-12-20 21:19:31 +01:00
exit 1
fi
2016-08-06 15:15:11 +02:00
}
2016-12-20 21:19:31 +01:00
function __PREPROCESSOR_CleanDebug {
2018-10-02 21:19:52 +02:00
local source="${1}"
local destination="${2:-$source}"
2018-10-02 21:19:52 +02:00
sed '/'$PARANOIA_DEBUG_BEGIN'/,/'$PARANOIA_DEBUG_END'/d' "$source" | grep -v "$PARANOIA_DEBUG_LINE" > "$destination.tmp"
2016-08-30 18:28:27 +02:00
if [ $? != 0 ]; then
2019-02-26 12:09:33 +01:00
Logger "Cannot remove PARANOIA_DEBUG code from standard build." "CRITICAL"
2016-08-30 18:28:27 +02:00
exit 1
2018-10-02 21:19:52 +02:00
else
mv -f "$destination.tmp" "$destination"
if [ $? -ne 0 ]; then
2019-02-26 12:09:33 +01:00
Logger "Cannot move [$destination.tmp] to [$destination]." "CRITICAL"
2018-10-02 21:19:52 +02:00
exit 1
fi
2016-08-30 18:28:27 +02:00
fi
2018-10-02 21:19:52 +02:00
chmod +x "$source"
2016-08-30 18:28:27 +02:00
if [ $? != 0 ]; then
2019-02-26 12:09:33 +01:00
Logger "Cannot chmod [$source]." "CRITICAL"
2016-08-30 18:28:27 +02:00
exit 1
2016-12-20 21:19:31 +01:00
else
2019-02-26 12:09:33 +01:00
Logger "Prepared [$source]." "NOTICE"
2016-12-20 21:19:31 +01:00
fi
2018-10-02 21:19:52 +02:00
if [ "$source" != "$destination" ]; then
chmod +x "$destination"
if [ $? != 0 ]; then
2019-02-26 12:09:33 +01:00
Logger "Cannot chmod [$destination]." "CRITICAL"
2018-10-02 21:19:52 +02:00
exit 1
else
2019-02-26 12:09:33 +01:00
Logger "Prepared [$destination]." "NOTICE"
2018-10-02 21:19:52 +02:00
fi
2016-08-30 18:28:27 +02:00
fi
2015-11-12 01:26:38 +01:00
}
2016-12-20 21:19:31 +01:00
function __PREPROCESSOR_CopyCommons {
2018-10-02 21:19:52 +02:00
local nPROGRAM="$1"
2018-10-02 21:19:52 +02:00
sed "s/\[prgname\]/$nPROGRAM/g" common_install.sh > ../install.sh
2016-08-30 18:28:27 +02:00
if [ $? != 0 ]; then
2019-02-26 12:09:33 +01:00
Logger "Cannot assemble install." "CRITICAL"
2016-08-30 18:28:27 +02:00
exit 1
fi
2016-12-20 21:19:31 +01:00
for subset in "${__PREPROCESSOR_SUBSETS[@]}"; do
2017-03-14 22:15:13 +01:00
__PREPROCESSOR_MergeSubset "$subset" "${subset//SUBSET/SUBSET END}" "ofunctions.sh" "../install.sh"
2016-12-20 21:19:31 +01:00
done
2018-10-02 21:19:52 +02:00
__PREPROCESSOR_CleanDebug "../install.sh"
2016-08-06 16:14:07 +02:00
if [ -f "common_batch.sh" ]; then
2018-10-02 21:19:52 +02:00
sed "s/\[prgname\]/$nPROGRAM/g" common_batch.sh > ../$nPROGRAM-batch.sh
2016-08-30 18:28:27 +02:00
if [ $? != 0 ]; then
2019-02-26 12:09:33 +01:00
Logger "Cannot assemble batch runner." "CRITICAL"
2016-08-30 18:28:27 +02:00
exit 1
fi
2018-10-02 21:19:52 +02:00
for subset in "${__PREPROCESSOR_SUBSETS[@]}"; do
__PREPROCESSOR_MergeSubset "$subset" "${subset//SUBSET/SUBSET END}" "ofunctions.sh" "../$nPROGRAM-batch.sh"
done
__PREPROCESSOR_CleanDebug "../$nPROGRAM-batch.sh"
2016-08-30 18:28:27 +02:00
fi
2015-11-12 01:26:38 +01:00
}
2016-12-20 21:19:31 +01:00
# If sourced don't do anything
if [ "$(basename $0)" == "merge.sh" ]; then
2018-10-02 21:19:52 +02:00
source "./ofunctions.sh"
if [ $? != 0 ]; then
echo "Please run $0 in dev directory with ofunctions.sh"
exit 1
fi
trap GenericTrapQuit TERM EXIT HUP QUIT
2018-10-02 21:19:52 +02:00
if [ "$1" == "osync" ]; then
__PREPROCESSOR_Merge osync
__PREPROCESSOR_CopyCommons osync
elif [ "$1" == "obackup" ]; then
__PREPROCESSOR_Merge obackup
__PREPROCESSOR_CopyCommons obackup
elif [ "$1" == "pmocr" ]; then
__PREPROCESSOR_Merge pmocr
__PREPROCESSOR_CopyCommons pmocr
else
echo "No valid program given."
2018-07-30 15:36:31 +02:00
Usage
exit 1
fi
2016-08-30 18:28:27 +02:00
fi