' *****************************************************
' PROGRAM:      bpwgen.bac
' PURPOSE:      simple mnemonic password generator
' AUTHOR:               vovchik (Puppy Linux forum)
' DEPENDS:      gcc, bacon (1.0.10), bash
' PLATFORM:     Puppy Linux (actually, any *nix)
' DATE:         13-03-2010
' *****************************************************

OPTION BASE 1

' *********************
' DECLARATIONS
' *********************

CONST maxindex = 26
GLOBAL CharSubs$[maxindex]
GLOBAL myinput$,q$

' *********************
' END DECLARATIONS
' *********************


' *********************
' FUNCTIONS
' *********************

FUNCTION INITVARS()
        q$ = CHR$(34)
        myinput$ = ""
        CharSubs$[1]  = "a@4A"
        CharSubs$[2]  = "bB3d"
        CharSubs$[3]  = "cC["
        CharSubs$[4]  = "dDb)"
        CharSubs$[5]  = "eE3#"
        CharSubs$[6]  = "fF{"
        CharSubs$[7]  = "gG9"
        CharSubs$[8]  = "hHn#"
        CharSubs$[9]  = "iI|!"
        CharSubs$[10] = "jJ]"
        CharSubs$[11] = "kK{"
        CharSubs$[12] = "lL"
        CharSubs$[13] = "mMw"
        CharSubs$[14] = "nN"
        CharSubs$[15] = "o0.O"
        CharSubs$[16] = "pP"
        CharSubs$[17] = "qQ"
        CharSubs$[18] = "rR"
        CharSubs$[19] = "sS$"
        CharSubs$[20] = "tT&7="
        CharSubs$[21] = "uUVv"
        CharSubs$[22] = "vVuU"
        CharSubs$[23] = "wWM"
        CharSubs$[24] = "xX#+"
        CharSubs$[25] = "yY^<"
        CharSubs$[26] = "zZ2"
        RETURN TRUE
END FUNCTION

' --------
FUNCTION INSERTSTR$(STRING oldtxt$, NUMBER mypos, STRING inserttxt$)
' --------
        LOCAL txt$, myresult
        txt$ = CONCAT$(LEFT$(oldtxt$, mypos - 1), inserttxt$)
        txt$ = CONCAT$(txt$, MID$(oldtxt$, mypos + LEN(inserttxt$)))
        myresult$ = LEFT$(txt$, LEN(oldtxt$))
        RETURN myresult$
END FUNCTION

' --------
FUNCTION SHOWHLP()
' --------
        PRINT
        PRINT "Bacon Mnemonic Password Generator v.0.1b"
        PRINT
        PRINT "bpwgen is a simple program to generate mnemonic passwords. It takes"
        PRINT "your word or words and creates a relatively obscure version thereof."
        PRINT
        PRINT "Arguments:     -h or --help (shows this help)"
        PRINT "Usage:         bpwgen ", q$,"your word or phrase", q$
        PRINT "Example:       bpwgen ", q$, "I love my cat", q$, " might yield: ",q$,"i L0ue MY C@t",q$
        PRINT
        RETURN TRUE
END FUNCTION

' --------
FUNCTION GETARGS()
' --------
        LOCAL i
        SPLIT ARGUMENT$ BY " " TO ARGV$ SIZE TotalArgs
        IF LEN(ARGV$[2]) EQ 0 OR EQUAL(ARGV$[2], "-h") OR EQUAL(ARGV$[2], "--help") THEN
                SHOWHLP
                END
        ELSE
                FOR i = 2 TO TotalArgs
                        myinput$ = LCASE$(CONCAT$(myinput$, ARGV$[i], " "))
                NEXT i
        END IF  
        RETURN TRUE
END FUNCTION

' --------
FUNCTION GENPWD(STRING myinput$)
' --------
        LOCAL i, j
        FOR i = 1 TO LEN(myinput$)
                FOR j = 1 TO maxindex
                        IF EQUAL(MID$(myinput$, i, 1), LEFT$(CharSubs$[j], 1)) THEN
                                myinput$ = INSERTSTR$(myinput$, i, MID$(CharSubs$[j], RANDOM(LEN(CharSubs$[j]) + 1) + 1, 1))
                        ELSE
                                myinput$ = myinput$
                        END IF
                NEXT J
        NEXT i
        pwd$ = myinput$
        RETURN TRUE
END FUNCTION

' --------              
FUNCTION SHOWPWD()
' --------
        PRINT pwd$
        RETURN TRUE
END FUNCTION

' *********************
' END FUNCTIONS
' *********************


' *********************
' MAIN
' *********************

SEED NOW
INITVARS
GETARGS
GENPWD(myinput$)
SHOWPWD
END

' *********************
' END MAIN
' *********************