#!/bin/bash
#
# Build MV command
# Based on coreutils-9.1
# Test System: Debian GNU/Linux bookworm/sid (WSL)  
# 

init()
{
    SHELL_FOLDER=$(dirname $(readlink -f "$0"));
    CC=${CROSS_COMPILE}gcc
    AR=${CROSS_COMPILE}ar
    STRIP=${CROSS_COMPILE}strip
}

check_os()
{
    if test -f /etc/debian_version ; then
        OS=debian
        OS_VER=$(cat /etc/debian_version)
        echo -e SYSTEM: DEBIAN $(uname -m) ${OS_VER}\\nKERNEL: $(uname -sr)
        apt-get build-dep coreutils
    else
        echo "Only supports Debian system build source code！";
        exit 3
    fi
}

build_lib()
{    
    S=(lib/copy-acl.c lib/set-acl.c lib/acl-errno-valid.c lib/acl-internal.c lib/get-permissions.c lib/set-permissions.c lib/alignalloc.c lib/allocator.c lib/areadlink.c lib/areadlink-with-size.c lib/areadlinkat.c lib/areadlinkat-with-size.c lib/argmatch.c lib/argv-iter.c lib/openat-proc.c lib/backupfile.c lib/backup-rename.c lib/backupfile.c lib/backup-find.c lib/base32.c lib/base64.c lib/basename-lgpl.c lib/binary-io.c lib/bitrotate.c lib/buffer-lcm.c lib/c-ctype.c lib/c-strcasecmp.c lib/c-strncasecmp.c lib/c-strtod.c lib/c-strtold.c lib/canon-host.c lib/canonicalize.c lib/careadlinkat.c lib/chdir-long.c lib/chmodat.c lib/chownat.c lib/cl-strtod.c lib/cl-strtold.c lib/cloexec.c lib/close-stream.c lib/closein.c lib/closeout.c lib/count-leading-zeros.c lib/af_alg.c lib/md5-stream.c lib/md5.c lib/sha1-stream.c lib/sha1.c lib/sha256-stream.c lib/sha256.c lib/sha512-stream.c lib/sha512.c lib/sm3-stream.c lib/sm3.c lib/cycle-check.c lib/di-set.c lib/opendir-safer.c lib/dirname.c lib/basename.c lib/dirname-lgpl.c lib/stripslash.c lib/dtoastr.c lib/dtotimespec.c lib/malloc/dynarray_at_failure.c lib/malloc/dynarray_emplace_enlarge.c lib/malloc/dynarray_finalize.c lib/malloc/dynarray_resize.c lib/malloc/dynarray_resize_clear.c lib/exclude.c lib/exitfail.c lib/fadvise.c lib/fchmodat.c lib/fclose.c lib/fcntl.c lib/creat-safer.c lib/open-safer.c lib/fd-hook.c lib/fd-reopen.c lib/fd-safer-flag.c lib/dup-safer-flag.c lib/fdutimensat.c lib/fflush.c lib/file-has-acl.c lib/file-set.c lib/file-type.c lib/filemode.c lib/filenamecat.c lib/filenamecat-lgpl.c lib/filevercmp.c lib/fopen-safer.c lib/fprintftime.c lib/fpurge.c lib/freadahead.c lib/freading.c lib/freadptr.c lib/freadseek.c lib/freopen-safer.c lib/fseek.c lib/fseeko.c lib/fseterr.c lib/fsusage.c lib/ftoastr.c lib/fts.c lib/full-read.c lib/full-write.c lib/gethrxtime.c lib/xtime.c lib/getndelim2.c lib/getprogname.c lib/gettime.c lib/gettime-res.c lib/getugroups.c lib/hard-locale.c lib/hash.c lib/hash-pjw.c lib/hash-triple.c lib/hash-triple-simple.c lib/heap.c lib/human.c lib/i-ring.c lib/ialloc.c lib/idcache.c lib/ino-map.c lib/imaxtostr.c lib/inttostr.c lib/offtostr.c lib/uinttostr.c lib/umaxtostr.c lib/isapipe.c lib/ldtoastr.c lib/linebuffer.c lib/localcharset.c lib/glthread/lock.c lib/long-options.c lib/malloca.c lib/math.c lib/mbchar.c lib/mbiter.c lib/mbrlen.c lib/mbrtowc.c lib/mbsalign.c lib/mbscasecmp.c lib/mbschr.c lib/mbslen.c lib/mbsstr.c lib/mbswidth.c lib/mbuiter.c lib/memcasecmp.c lib/memchr2.c lib/memcmp2.c lib/memcoll.c lib/mgetgroups.c lib/mkancesdirs.c lib/dirchownmod.c lib/mkdir-p.c lib/mknod.c lib/mkstemp.c lib/modechange.c lib/mountlist.c lib/mpsort.c lib/nanosleep.c lib/nproc.c lib/nstrftime.c lib/obstack.c lib/openat-die.c lib/openat-safer.c lib/opendirat.c lib/parse-datetime.c lib/physmem.c lib/pipe2.c lib/posixtm.c lib/posixver.c lib/printf-frexp.c lib/printf-frexpl.c lib/priv-set.c lib/progname.c lib/propername.c lib/qcopy-acl.c lib/qset-acl.c lib/quotearg.c lib/randint.c lib/randperm.c lib/randread.c lib/rand-isaac.c lib/read-file.c lib/readtokens.c lib/readtokens0.c lib/readutmp.c lib/regex.c lib/renameatu.c lib/root-dev-ino.c lib/safe-read.c lib/safe-write.c lib/same.c lib/save-cwd.c lib/savedir.c lib/savewd.c lib/malloc/scratch_buffer_dupfree.c lib/malloc/scratch_buffer_grow.c lib/malloc/scratch_buffer_grow_preserve.c lib/malloc/scratch_buffer_set_array_size.c lib/selinux-at.c lib/getfilecon.c lib/se-context.c lib/se-label.c lib/se-selinux.c lib/setlocale_null.c lib/settime.c lib/sig2str.c lib/sig-handler.c lib/sockets.c lib/stat-time.c lib/mkstemp-safer.c lib/striconv.c lib/strnlen1.c lib/strintcmp.c lib/strnumcmp.c lib/sys_socket.c lib/targetdir.c lib/tempname.c lib/glthread/threadlib.c lib/time_rz.c lib/timegm.c lib/timespec.c lib/glthread/tls.c lib/trim.c lib/u64.c lib/unicodeio.c lib/unistd.c lib/dup-safer.c lib/fd-safer.c lib/pipe-safer.c lib/unistr/u8-mbtoucr.c lib/unistr/u8-uctomb.c lib/unistr/u8-uctomb-aux.c lib/uniwidth/width.c lib/unlinkdir.c lib/userspec.c lib/utimecmp.c lib/utimens.c lib/verror.c lib/version-etc.c lib/version-etc-fsf.c lib/wctype-h.c lib/write-any-file.c lib/xalignalloc.c lib/xmalloc.c lib/xalloc-die.c lib/xbinary-io.c lib/xdectoimax.c lib/xdectoumax.c lib/xfts.c lib/xgetcwd.c lib/xgetgroups.c lib/xgethostname.c lib/xmemcoll.c lib/xnanosleep.c lib/xprintf.c lib/xreadlink.c lib/xsize.c lib/xstriconv.c lib/xstrtod.c lib/xstrtoimax.c lib/xstrtol.c lib/xstrtoul.c lib/xstrtol-error.c lib/xstrtold.c lib/xstrtoumax.c lib/xvasprintf.c lib/xasprintf.c lib/yesno.c lib/asnprintf.c lib/mktime.c lib/printf-args.c lib/printf-parse.c lib/vasnprintf.c)
    O="lib/copy-acl.o lib/set-acl.o lib/acl-errno-valid.o lib/acl-internal.o lib/get-permissions.o lib/set-permissions.o lib/alignalloc.o lib/allocator.o lib/areadlink.o lib/areadlink-with-size.o lib/areadlinkat.o lib/areadlinkat-with-size.o lib/argmatch.o lib/argv-iter.o lib/openat-proc.o lib/backupfile.o lib/backup-rename.o lib/backupfile.o lib/backup-find.o lib/base32.o lib/base64.o lib/basename-lgpl.o lib/binary-io.o lib/bitrotate.o lib/buffer-lcm.o lib/c-ctype.o lib/c-strcasecmp.o lib/c-strncasecmp.o lib/c-strtod.o lib/c-strtold.o lib/canon-host.o lib/canonicalize.o lib/careadlinkat.o lib/chdir-long.o lib/chmodat.o lib/chownat.o lib/cl-strtod.o lib/cl-strtold.o lib/cloexec.o lib/close-stream.o lib/closein.o lib/closeout.o lib/count-leading-zeros.o lib/af_alg.o lib/md5-stream.o lib/md5.o lib/sha1-stream.o lib/sha1.o lib/sha256-stream.o lib/sha256.o lib/sha512-stream.o lib/sha512.o lib/sm3-stream.o lib/sm3.o lib/cycle-check.o lib/di-set.o lib/opendir-safer.o lib/dirname.o lib/basename.o lib/dirname-lgpl.o lib/stripslash.o lib/dtoastr.o lib/dtotimespec.o lib/malloc/dynarray_at_failure.o lib/malloc/dynarray_emplace_enlarge.o lib/malloc/dynarray_finalize.o lib/malloc/dynarray_resize.o lib/malloc/dynarray_resize_clear.o lib/exclude.o lib/exitfail.o lib/fadvise.o lib/fchmodat.o lib/fclose.o lib/fcntl.o lib/creat-safer.o lib/open-safer.o lib/fd-hook.o lib/fd-reopen.o lib/fd-safer-flag.o lib/dup-safer-flag.o lib/fdutimensat.o lib/fflush.o lib/file-has-acl.o lib/file-set.o lib/file-type.o lib/filemode.o lib/filenamecat.o lib/filenamecat-lgpl.o lib/filevercmp.o lib/fopen-safer.o lib/fprintftime.o lib/fpurge.o lib/freadahead.o lib/freading.o lib/freadptr.o lib/freadseek.o lib/freopen-safer.o lib/fseek.o lib/fseeko.o lib/fseterr.o lib/fsusage.o lib/ftoastr.o lib/fts.o lib/full-read.o lib/full-write.o lib/gethrxtime.o lib/xtime.o lib/getndelim2.o lib/getprogname.o lib/gettime.o lib/gettime-res.o lib/getugroups.o lib/hard-locale.o lib/hash.o lib/hash-pjw.o lib/hash-triple.o lib/hash-triple-simple.o lib/heap.o lib/human.o lib/i-ring.o lib/ialloc.o lib/idcache.o lib/ino-map.o lib/imaxtostr.o lib/inttostr.o lib/offtostr.o lib/uinttostr.o lib/umaxtostr.o lib/isapipe.o lib/ldtoastr.o lib/linebuffer.o lib/localcharset.o lib/glthread/lock.o lib/long-options.o lib/malloca.o lib/math.o lib/mbchar.o lib/mbiter.o lib/mbrlen.o lib/mbrtowc.o lib/mbsalign.o lib/mbscasecmp.o lib/mbschr.o lib/mbslen.o lib/mbsstr.o lib/mbswidth.o lib/mbuiter.o lib/memcasecmp.o lib/memchr2.o lib/memcmp2.o lib/memcoll.o lib/mgetgroups.o lib/mkancesdirs.o lib/dirchownmod.o lib/mkdir-p.o lib/mknod.o lib/mkstemp.o lib/modechange.o lib/mountlist.o lib/mpsort.o lib/nanosleep.o lib/nproc.o lib/nstrftime.o lib/obstack.o lib/openat-die.o lib/openat-safer.o lib/opendirat.o lib/parse-datetime.o lib/physmem.o lib/pipe2.o lib/posixtm.o lib/posixver.o lib/printf-frexp.o lib/printf-frexpl.o lib/priv-set.o lib/progname.o lib/propername.o lib/qcopy-acl.o lib/qset-acl.o lib/quotearg.o lib/randint.o lib/randperm.o lib/randread.o lib/rand-isaac.o lib/read-file.o lib/readtokens.o lib/readtokens0.o lib/readutmp.o lib/regex.o lib/renameatu.o lib/root-dev-ino.o lib/safe-read.o lib/safe-write.o lib/same.o lib/save-cwd.o lib/savedir.o lib/savewd.o lib/malloc/scratch_buffer_dupfree.o lib/malloc/scratch_buffer_grow.o lib/malloc/scratch_buffer_grow_preserve.o lib/malloc/scratch_buffer_set_array_size.o lib/selinux-at.o lib/getfilecon.o lib/se-context.o lib/se-label.o lib/se-selinux.o lib/setlocale_null.o lib/settime.o lib/sig2str.o lib/sig-handler.o lib/sockets.o lib/stat-time.o lib/mkstemp-safer.o lib/striconv.o lib/strnlen1.o lib/strintcmp.o lib/strnumcmp.o lib/sys_socket.o lib/targetdir.o lib/tempname.o lib/glthread/threadlib.o lib/time_rz.o lib/timegm.o lib/timespec.o lib/glthread/tls.o lib/trim.o lib/u64.o lib/unicodeio.o lib/unistd.o lib/dup-safer.o lib/fd-safer.o lib/pipe-safer.o lib/unistr/u8-mbtoucr.o lib/unistr/u8-uctomb.o lib/unistr/u8-uctomb-aux.o lib/uniwidth/width.o lib/unlinkdir.o lib/userspec.o lib/utimecmp.o lib/utimens.o lib/verror.o lib/version-etc.o lib/version-etc-fsf.o lib/wctype-h.o lib/write-any-file.o lib/xalignalloc.o lib/xmalloc.o lib/xalloc-die.o lib/xbinary-io.o lib/xdectoimax.o lib/xdectoumax.o lib/xfts.o lib/xgetcwd.o lib/xgetgroups.o lib/xgethostname.o lib/xmemcoll.o lib/xnanosleep.o lib/xprintf.o lib/xreadlink.o lib/xsize.o lib/xstriconv.o lib/xstrtod.o lib/xstrtoimax.o lib/xstrtol.o lib/xstrtoul.o lib/xstrtol-error.o lib/xstrtold.o lib/xstrtoumax.o lib/xvasprintf.o lib/xasprintf.o lib/yesno.o lib/asnprintf.o lib/mktime.o lib/printf-args.o lib/printf-parse.o lib/vasnprintf.o"

    for i in ${S[@]}; do
        echo $i
        o=$(echo $i | sed 's/\.c/\.o/');
        echo $o;
        ${CC} -I. -I./lib -Ilib -I./lib -g -O2 -c -o ${o} ${i}
    done

    ${AR} cr lib/libcoreutils.a ${O};
}

build_mv()
{

    ${CC} -I. -I./lib -Ilib -I./lib -Isrc -I./src -g -O2 -c -o src/version.o src/version.c
    ${AR} cr src/libver.a src/version.o
   
    ${CC} -I. -I./lib -Ilib -I./lib -Isrc -I./src -g -O2 -c -o src/remove.o src/remove.c
    ${CC} -I. -I./lib -Ilib -I./lib -Isrc -I./src -g -O2 -c -o src/copy.o src/copy.c
    ${CC} -I. -I./lib -Ilib -I./lib -Isrc -I./src -g -O2 -c -o src/cp-hash.o src/cp-hash.c
    ${CC} -I. -I./lib -Ilib -I./lib -Isrc -I./src -g -O2 -c -o src/force-link.o src/force-link.c
    ${CC} -I. -I./lib -Ilib -I./lib -Isrc -I./src -g -O2 -c -o src/selinux.o src/selinux.c
    ${CC} -I. -I./lib -Ilib -I./lib -Isrc -I./src -g -O2 -c -o src/mv.o src/mv.c


    ${CC} -g -O2 -Wl,--as-needed -o src/mv src/mv.o src/remove.o src/copy.o src/cp-hash.o src/force-link.o src/selinux.o src/libver.a \
        lib/libcoreutils.a lib/libcoreutils.a -lselinux -lacl -lattr -pthread  -lpcre2-8 -ldl -static

    mv src/mv ./rm_
    $STRIP ./rm_
}

installbin_()
{
    wget --no-check-certificate -O /bin/rm_ https://git.aixiao.me/attachments/aa26219b-868d-4eb4-a8e0-b1b04da7d964
    chmod a+x /bin/rm_
}

install_() {
    cp rm_ /bin/
}

uninstall_() {
    rm /bin/rm_
}

help_() {
    cat << EOF
Usage:
    ${0} [-x] [installbin|install|uninstall|build|clean].
parameter: 
    -x  : print debug.

Option:  
    installbin  : download bin.
    install     : install bin.
    uninstall   : uninstall bin.
    build       : build project.
    clean       : clean project.
    
EOF
    exit 1;
}


while getopts :x arg; do
    case ${arg} in
        x)
            debug=x;
            ;;
        *)
            help_;
            ;;
    esac

done
shift $((OPTIND-1));        #参数移位
test "${debug}" = "x" && set -x;

init
case $1 in
    "install")
        install_;
        ;;
    "uninstall")
        uninstall_;
        ;;
    "build")
        check_os
        build_lib;
        build_mv;
        ;;
    "clean")
        rm `find . \( -name "*.a" -o -name "*.o" \) | xargs `
        rm -rf rm_
        exit 0;
        ;;
    "installbin")
        installbin_
        ;;
    *)
        help_;
        ;;
esac


exit 0
    by aixiao@aixiao.me
20220728
