From 481b4f6058b617ddfb2a1f7a8e0ec4015db45389 Mon Sep 17 00:00:00 2001 From: Patrick Cao Huu Thien Date: Thu, 17 Oct 2024 00:31:57 +0200 Subject: [PATCH] initial version --- README | 14 ++++++++++ shasumscript | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 README create mode 100755 shasumscript diff --git a/README b/README new file mode 100644 index 0000000..641f07f --- /dev/null +++ b/README @@ -0,0 +1,14 @@ +*shasumscript* is a tool to generate a self-checksum-script. + + +Usage + + shasumscript script.sh + +License + +MIT + +From an idea of LSC https://github.com/dealfonso/lsc + + diff --git a/shasumscript b/shasumscript new file mode 100755 index 0000000..074a205 --- /dev/null +++ b/shasumscript @@ -0,0 +1,74 @@ +#!/bin/sh +# Script to add a SHA256 checksum to a file +# Usage: shasumscript +# Output: _sha256 +# Requires: shasum +# License: MIT +# Author: Patrick Cao Huu Thien + +set -e + +error() { printf "Error: \e[31m%s\e[0m\n" "$*" >&2; } +err() { error "$*"; exit 1; } +usage() { echo "Usage: $(basename "$0") [-V | -h] ";echo " -V: Show version"; echo " -h: Show this help"; } +err_usage() { error "$*"; usage; exit 1; } +step () { err="$?"; c=32; t=OK; test "$err" = 0 || { c=31; t=FAILED; }; printf "* %s \e[%dm%s\e[0m\n" "$*" "$c" "$t"; } + +VERSION="0.0.1" +if test "$1" = '-V'; then echo "shasumscript v$VERSION"; exit; fi +if test "$1" = '-h'; then usage; exit; fi + +file="$1" +test -n "$file" || err_usage "Missing file argument" +test -e "$file" || err "File not found: $file" + +sha="$(shasum -a 256 "$file" | cut -d' ' -f1)" || err "Failed to calculate SHA256 checksum" +step "Checksum" + +newfile="${file}_sha256" + +cat < "$newfile" +#!/bin/sh +# script generated by shasumscript at $(date) +# +# This script self-checkssum it's content and exit on error +# The real script can be found after the separator: +# $sha +# +# License: MIT +# Author: Patrick Cao Huu Thien + +tmpexe="\$(mktemp)" +trap 'rm -f "\$tmpexe"' EXIT + +cat "\$0" | sed -n '/^${sha}/,\$p' | tail -n +2 > "\$tmpexe" +test "\$(shasum -a 256 "\$tmpexe" | cut -d' ' -f1)" = "$sha" || { + echo "Checksum mismatch!" >&2 + exit 1 +} + +printf "Checksum OK"; sleep 1; printf "\r \r" + +sh "\$tmpexe" "\$@" + +exit +$sha +EOT +step "pre-script" + +cat "$file" >> "$newfile" +step "post-script" + +chmod +x "$newfile" +step "make it executable" +echo +cat <