/main.go
package main

import (
    "bytes"
    "os"
	"fmt"
    "flag"
    "encoding/base64"

    "bytex64.net/code/bitsmash/bs"
)

type BitsmashOpts struct {
    decode bool
    dump bool
    base64 bool
    files []string
    encodeOpts bs.EncodeOpts
}

func parseArgs() BitsmashOpts {
    bo := BitsmashOpts{}
    bo.encodeOpts = bs.EncodeOpts{}
    flag.BoolVar(&bo.decode, "decode", false, "Decode from bitsmash input rather than encode image")
    flag.BoolVar(&bo.dump,   "dump",   false, "dump detailed information about encoding")
    flag.BoolVar(&bo.base64, "base64", false, "Encode the result with base64")
    flag.IntVar(&bo.encodeOpts.Optimize,    "o",      3,     "Optimization level")
    flag.IntVar(&bo.encodeOpts.Palette,     "p",      -1,    "Force palette N")
    flag.Parse()
    bo.files = flag.Args()
    return bo
}

func main() {
	args := parseArgs()
    if len(args.files) == 0 {
        fmt.Println("Please specify a file to smash")
        os.Exit(1)
    }

    var smash bs.BitSmash
    if (args.decode) {
        smash = bs.NewFromFile(args.files[0])
    } else {
        smash = bs.NewFromImage(args.files[0], &args.encodeOpts)
    }

    if args.dump {
        smash.Dump()
    } else {
        if (args.decode) {
            smash.WriteImage(os.Stdout)
        } else {
            var buf bytes.Buffer
            smash.WriteTo(&buf)
            if args.base64 {
                fmt.Println(":_", base64.RawStdEncoding.EncodeToString(buf.Bytes()))
            } else {
                buf.WriteTo(os.Stdout)
            }
        }
    }
}