#!/bin/bash

# This is script for the automatically building CryptoPro CSP KC1 and KC2
# xzm modules for Porteus i586 and x86_64.
# Version 2025-05-03

# Copyright 2017 Blaze, Dankov, Russia
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# root check
if [ `whoami` != "root" ]; then
    echo -e "\nOnly root can run this.\n"
    exit 1
fi

PRGNAM=${PRGNAM:-cprocsp}
BUILD=${BUILD:-1}
SOURCE=https://csp.ublaze.ru
VERSION=$(lynx -source "$SOURCE/csp-tar/csp-version.txt")
BOLD=${BOLD:-"\e[1m"}
CYAN=${CYAN:-"\e[96m"}
GREEN=${GREEN:-"\e[92m"}
RED=${RED:-"\e[31m"}
RESET=${RESET:-"\e[0m"}
CWD=$(pwd)
TMPDIR=/tmp/portch
PKG=$TMPDIR/package-$PRGNAM
PKGINFO=$PKG/var/lib/pkgtools/packages
OUTPUT=${OUTPUT:-/tmp}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586
    CONFIG=config.ini
    TARNAME=linux-ia32
    TARBALL=$(echo $TARNAME.tgz) ;;
    x86_64) ARCH=x86_64
    CONFIG=config64.ini
    TARNAME=linux-amd64
    TARBALL=$(echo $TARNAME.tgz) ;;
    *) echo 'Your architecture is not supported' ;;
  esac
fi

cleanup(){
[ -d $TMPDIR ] && rm -rf $TMPDIR
[ -d $PKG ] && rm -rf $PKG
exit	
}

# check if CryptoPro CSP installed in the system
if [[ -f "/etc/opt/cprocsp/release" ]]; then
    MYVER=${MYVER:-`awk '{print $NF}' /etc/opt/cprocsp/release | sed 's|0.0.||;s|.$||'`}
    echo -e " ${BOLD}Checking if CryptoPro CSP is installed ...${RESET}"
    echo -e "[${GREEN}${BOLD}OK${RESET}] CryptoPro CSP is installed"
    echo -e "[${GREEN}${BOLD}OK${RESET}] CryptoPro CSP version: ${GREEN}${BOLD}${MYVER}${RESET}\n" 
fi

if [ "$MYVER" == "$VERSION" ]; then
    echo -e "You have the latest ${GREEN}${BOLD}$MYVER${RESET} version of $PRGNAM"
    sleep 5
    exit
else
    read -p "$(echo -e — Would you like to build CryptoPro CSP ${GREEN}${BOLD}$VERSION${RESET} xzm module? [y/n])" -n 1 -r -s && echo
    if [[ $REPLY =~ ^[Yy]$ ]]; then
        echo "We continue the execution of the $0 script" &>/dev/null
        else
        exit
    fi
fi

rm -rf $PKG
mkdir -p $TMPDIR $PKG
cd $PKG

# set protection class of CryptoPro CSP (kc1 or kc2)
echo -e "— What protection class kc1 or kc2 of CryptoPro CSP do you want?:\n"
echo "1) kc1"
echo "2) kc2"
echo

read class

case "$class" in
1|kc1|KC1)
KCVERSION=${KCVERSION:-kc1}

if [[ "$ARCH" == i686 || "$ARCH" == i586 ]]; then
    wget -q --show-progress $SOURCE/csp-tar/$ARCH/$TARBALL
    tar xvf $TARBALL && rm -f $TARBALL
    rm -f $PKG/$TARNAME/{accord_random-*src,cprocsp-drv*,cprocsp-ipsec*,cprocsp-nginx*,cprocsp-pki*,lsb-cprocsp-devel*,lsb-cprocsp-rdr-sobol*,sobol*}.rpm
    mv $PKG/$TARNAME/* $PKG && rm -rf $PKG/$TARNAME
    for i in `find . -type f ! -name 'lsb-cprocsp-kc2*' | fgrep .rpm | sort`; do rpm2cpio $i | cpio -idmv &>/dev/null; done
    rm -f {*.rpm,*.sh,*.ini,*.txt,cpverify,install.desktop}
    wget -qO $PKG/etc/opt/cprocsp/$CONFIG $SOURCE/csp-config/$ARCH/$KCVERSION/$CONFIG

elif [[ "$ARCH" == x86_64 ]]; then
    wget -q --show-progress $SOURCE/csp-tar/$ARCH/$TARBALL
    tar xvf $TARBALL && rm -f $TARBALL
    rm -f $PKG/$TARNAME/{accord_random-*src,cprocsp-drv*,cprocsp-ipsec*,cprocsp-nginx*,cprocsp-pki*,lsb-cprocsp-devel*,lsb-cprocsp-rdr-sobol*,sobol*}.rpm
    mv $PKG/$TARNAME/* $PKG && rm -rf $PKG/$TARNAME
    for i in `find . -type f ! -name 'lsb-cprocsp-kc2*' | fgrep .rpm | sort`; do rpm2cpio $i | cpio -idmv &>/dev/null; done
    rm -f {*.rpm,*.sh,*.ini,*.txt,cpverify,install.desktop}
    wget -qO $PKG/etc/opt/cprocsp/$CONFIG $SOURCE/csp-config/$ARCH/$KCVERSION/$CONFIG

### fake Slackware type package info: super dumb version
mkdir -p $PKGINFO
echo "PACKAGE NAME: $PRGNAM-$KCVERSION-$VERSION-$ARCH-$BUILD" > $PKGINFO/$PRGNAM-$KCVERSION-$VERSION-$ARCH-$BUILD

cat >> $PKGINFO/$PRGNAM-$KCVERSION-$VERSION-$ARCH-$BUILD << EOM
PACKAGE DESCRIPTION:
cprocsp: CryptoPro CSP is a crypto provider with functions for working 
cprocsp: with digital signatures, data encryption tools, for creating
cprocsp: secure communication channels and for performing
cprocsp: user authentication.
cprocsp:
cprocsp:
cprocsp:
cprocsp: CryptoPro CSP home: https://www.cryptopro.ru/
cprocsp:
FILE LIST:
EOM

    find * | grep -v var >> $PKGINFO/$PRGNAM-$KCVERSION-$VERSION-$ARCH-$BUILD
    dir2xzm $PKG $OUTPUT/$PRGNAM-$KCVERSION-$VERSION-$ARCH-$BUILD.xzm

# check on CryptoPro CSP xzm file exists in /tmp
    if [ -f "$OUTPUT/$PRGNAM-$KCVERSION-$VERSION-$ARCH-$BUILD.xzm" ]; then
        echo -e "\n${BOLD}Your $PRGNAM module is at: ${GREEN}${BOLD}$OUTPUT/$PRGNAM-$KCVERSION-$VERSION-$ARCH-$BUILD.xzm${RESET}\n${BOLD}Please copy it to your modules folder to survive a reboot.${RESET}\n"
    else
        echo -e "\n${RED}${BOLD}Faile. Your $PRGNAM module is not built.${RESET}\n"
    fi
    cleanup
fi
;;

2|kc2|KC2)
KCVERSION=${KCVERSION:-kc2}

if [[ "$ARCH" == i686 || "$ARCH" == i586 ]]; then
    wget -q --show-progress $SOURCE/csp-tar/$ARCH/$TARBALL
    tar xvf $TARBALL && rm -f $TARBALL
    rm -f $PKG/$TARNAME/{accord_random-*src,cprocsp-drv*,cprocsp-ipsec*,cprocsp-pki*,cprocsp-nginx*,lsb-cprocsp-devel*,lsb-cprocsp-rdr-sobol*,sobol*}.rpm
    mv $PKG/$TARNAME/* $PKG && rm -rf $PKG/$TARNAME
    for i in `find . -type f ! -name 'lsb-cprocsp-kc1*' | fgrep .rpm | sort`; do rpm2cpio $i | cpio -idmv &>/dev/null; done
    rm -f {*.rpm,*.sh,*.ini,*.txt,cpverify,install.desktop}
    wget -qO $PKG/etc/opt/cprocsp/$CONFIG $SOURCE/csp-config/$ARCH/$KCVERSION/$CONFIG

elif [[ "$ARCH" == x86_64 ]]; then
    wget -q --show-progress $SOURCE/csp-tar/$ARCH/$TARBALL
    tar xvf $TARBALL && rm -f $TARBALL
    rm -f $PKG/$TARNAME/{accord_random-*src,cprocsp-drv*,cprocsp-ipsec*,cprocsp-pki*,cprocsp-nginx*,lsb-cprocsp-devel*,lsb-cprocsp-rdr-sobol*,sobol*}.rpm
    mv $PKG/$TARNAME/* $PKG && rm -rf $PKG/$TARNAME
    for i in `find . -type f ! -name 'lsb-cprocsp-kc1*' | fgrep .rpm | sort`; do rpm2cpio $i | cpio -idmv &>/dev/null; done
    rm -f {*.rpm,*.sh,*.ini,*.txt,cpverify,install.desktop}
    wget -qO $PKG/etc/opt/cprocsp/$CONFIG $SOURCE/csp-config/$ARCH/$KCVERSION/$CONFIG

### fake Slackware type package info: super dumb version
mkdir -p $PKGINFO
echo "PACKAGE NAME: $PRGNAM-$KCVERSION-$VERSION-$ARCH-$BUILD" > $PKGINFO/$PRGNAM-$KCVERSION-$VERSION-$ARCH-$BUILD

cat >> $PKGINFO/$PRGNAM-$KCVERSION-$VERSION-$ARCH-$BUILD << EOM
PACKAGE DESCRIPTION:
cprocsp: CryptoPro CSP is a crypto provider with functions for working 
cprocsp: with digital signatures, data encryption tools, for creating
cprocsp: secure communication channels and for performing
cprocsp: user authentication.
cprocsp:
cprocsp:
cprocsp:
cprocsp: CryptoPro CSP home: https://www.cryptopro.ru/
cprocsp:
FILE LIST:
EOM

    find * | grep -v var >> $PKGINFO/$PRGNAM-$KCVERSION-$VERSION-$ARCH-$BUILD
    dir2xzm $PKG $OUTPUT/$PRGNAM-$KCVERSION-$VERSION-$ARCH-$BUILD.xzm

# check on CryptoPro CSP xzm file exists in /tmp
    if [ -f "$OUTPUT/$PRGNAM-$KCVERSION-$VERSION-$ARCH-$BUILD.xzm" ]; then
        echo -e "\n${BOLD}Your $PRGNAM module is at: ${GREEN}${BOLD}$OUTPUT/$PRGNAM-$KCVERSION-$VERSION-$ARCH-$BUILD.xzm${RESET}\n${BOLD}Please copy it to your modules folder to survive a reboot.${RESET}\n"
    else
        echo -e "\n${RED}${BOLD}Faile. Your $PRGNAM module is not built.${RESET}\n"
    fi
    cleanup
fi
;;
*) echo 'Please enter the correct option' ;;
esac