about summary refs log tree commit diff stats
path: root/home-manager/packages/scripts/apps/con2pdf
blob: 7d23fe827b8d64c32f3921119c6bc5dda92b40bf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#! /bin/sh
# shellcheck disable=SC2086
# shellcheck source=/dev/null
. ~/.local/lib/shell/lib


# Vars
counter=0;
a_set=false;
pw=$(pwd)

usage() {
    cat << EOF
    This is a help doc!
    -p for path
    -h for help
    -n for name
    -g gen pdf dir
    -r for number of pages per pdf
EOF
exit 0;
}

# Ops
while getopts ":hp:n:gr:" flag;do
    case "$flag" in
        p)
            Command_path=$OPTARG
            ;;
        n)
            name=$OPTARG
            ;;
        g)
            gen=true
            ;;
        r)
            pages=$OPTARG
            ;;
        h)
            usage;;
        *)
            usage;;

        esac
    done
[ -z "$pages" ] && die "No pages set"


# Scan
if [ -z $Command_path ];then
    tm=$(mktemp -d)
    cd $tm || die "Bug"

    for i in $(seq $pages);do
        scanimage --format=tiff --progress \
            --source ADF --device="airscan:w1:Brother DCP-9022CDW" \
            --batch=%d.tif --batch-increment=$pages --batch-start=$i
        [ $? -ne 0 ] && scanimage --format=tiff --progress \
            --source ADF --device="airscan:w1:Brother DCP-9022CDW" \
            --batch=%d.tif --batch-increment=$pages --batch-start=$i
        if [ $pages -ge 2 ];then
            msg "Finished first turn, please change side!";
            readp "Ready to continue?" noop
        fi
    done
else
    tm=$Command_path;
fi
cd $pw || die "Bug"

# mk pdf
if [ "$gen" = true ];then
    mkdir pdfs
    cd pdfs/  || die "Bug"
fi

while IFS= read -r i;do

    if [ $pages -ge 2 ];then
        a_set=false;
        if [ -z "$a" ];then
            a="$i"
            a_set=true;
            : $((counter += 1))
        fi

        if [ -n "$a" ] && ! [ "$a_set" = true ];then
            convert "$a" "$i" -compress jpeg -quality 70 "$name"_"$counter".pdf
            a=
        fi
    else
        : $((counter += 1))
        convert "$i" -compress jpeg -quality 70 "$name"_"$counter".pdf
    fi


done < "$(tmp "ls $tm | sort -n | awk -v a=$tm -v c='/' '{printf a} {printf c} {print \$0}' ")"


rm -r $tm
if [ -d /tmp/LIB_FILE_TEMP_DIR/ ];then rm -r /tmp/LIB_FILE_TEMP_DIR/; fi